Lucene Full-text Search multiple query retrieval simple case __lucene

Source: Internet
Author: User
Tags create index

Write a variety of query retrieval methods for Lucene Full-text search engine

Here is the use of English participle, wrote a separate article on Chinese participle and highlighting, as follows:
Lucene Full-text search of Chinese participle and highlight, click to enter

Also share a Lucene index Library viewer, click to enter

Lucene website http://lucene.apache.org here to write link content

First you need to introduce Lucene's Maven dependencies, as follows:

<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactid>lucene-core</ artifactid>
    <version>5.3.1</version>
</dependency>

<dependency>
    <groupId>org.apache.lucene</groupId>
    <artifactId>lucene-queryparser</artifactId>
    <version>5.3.1</version>
</dependency>

<dependency>
    <groupId> org.apache.lucene</groupid>
    <artifactId>lucene-analyzers-common</artifactId>
    < Version>5.3.1</version>
</dependency>

I'll just post the code below, as follows:

Package Lucenedemo;
Import java.io.IOException;
Import java.nio.file.Paths;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;
Import Org.apache.lucene.analysis.Analyzer;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.document.IntField;
Import Org.apache.lucene.document.TextField;
Import Org.apache.lucene.index.DirectoryReader;
Import Org.apache.lucene.index.IndexReader;
Import Org.apache.lucene.index.IndexWriter;
Import Org.apache.lucene.index.IndexWriterConfig;
Import Org.apache.lucene.index.Term;
Import Org.apache.lucene.queryparser.classic.QueryParser;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.Query;
Import Org.apache.lucene.search.ScoreDoc;
Import Org.apache.lucene.search.TermQuery;
Import Org.apache.lucene.search.TopDocs;
Import Org.apache.lucene.store.Directory; import org. apache.lucene.store.FSDirectory;
Import Org.apache.lucene.search.TermRangeQuery;
Import Org.apache.lucene.util.BytesRef;
Import Org.apache.lucene.search.NumericRangeQuery;
Import Org.apache.lucene.search.PrefixQuery;
Import Org.apache.lucene.search.BooleanClause;

Import Org.apache.lucene.search.BooleanQuery;
     /** * Full-Text Search case * @author Mszhou/public class Lucenedemo {/** * Demo writes data to index file * In the project we will periodically write the data to the index file. * Then search in the index file, which is much more efficient than the search in the database, * and retrieval is far more powerful than the fuzzy matching of the database, which is the great thing about search tools * Note that if you write the index regularly in your project, you need to judge the Whether the data exists, the existence is modified, there is no further new * * @param lucenedir This parameter represents the directory in which the index is stored/public static void Writeindex (String lucen
            Edir) {IndexWriter writer = null;//Declaration CREATE INDEX writer try{//CREATE index directory where parameter Lucenedir represents index storage directory

            Directory Dir=fsdirectory.open (Paths.get (Lucenedir)); Create standard word breaker (commonly used in English, not suitable for Chinese use) Analyzer analyzer=new standardanalyzer (); Standard word breaker Indexwriterconfig iwc=newIndexwriterconfig (analyzer);


            Create an index writer instance and configure the related configuration via Indexwriterconfig writer=new IndexWriter (dir, IWC); = = = Here make some data for demo, or directly read the data in the database start ======== List <Map<String,String>> list=new arraylist<map&lt ;

            String,string>> ();
            False data 1 map<string,string> map1 =new hashmap<string, string> ();
            Map1.put ("title", "Weejij dasa");//Represents the Title field Map1.put ("Centent", "Hdaiads SD s G r44e r HGG F HD 4");

            List.add (MAP1);
            False Data 2 map<string,string> map2 =new hashmap<string, string> ();    
            Map2.put ("title", "Demo12 i33s ss");//Represents the Title field Map2.put ("Centent", "Hdaiads SD s G r44e r HGG F HD 4");

            List.add (MAP2);
            False data 1 map<string,string> map3 =new hashmap<string, string> (); Map3.put ("title", "Demo 12i131s ss");//Represents the Title field Map3.put ("CeNtent "," Hdaiads SD s G r44e r HGG F HD 4);//Represents the Title field List.add (MAP3); = = = Here make some data for demonstration, can also directly read data in the database end ========//traverse data, and write data to the index for (int i=0; i<list.size (); i++)
                {//= = = Get data to start = = map<string,string> Mapcen=list.get (i); string title = Mapcen.get ("title"). ToString ();//Get title String centent = Mapcen.get ("Centent"). ToString ();
                Get content/= = = Get Data End = =//========= Write data to index start =================
                Create a Document object, document represents the data structure unit of the Lucene index document Doc=new document ();

                Add ID data, and the last Field.Store.YES indicates that the field is stored Doc.add (new Intfield ("id", I, Field.Store.YES));
                 /* Add caption data, the last Field.Store.YES means store the field * Note: If you need to retrieve the field, it is recommended that you use the TextField type and set to Field.Store.YES * * Doc.add (new TextField ("title", title, Field.Store.YES));

                Adds the title content data, and the last Field.Store.NO indicates that the field is not stored Doc.add (new TextField ("Centent", Centent, Field.Store.NO)); 
                Write index writer.adddocument (DOC); ========= writes data to index end =================}}catch (Exception e) {System.out.println ("An exception has occurred!")
            ");
        E.printstacktrace ();
            }finally{try {writer.close ();//Close the stream, make sure to close the catch (IOException e) { /** * Demonstrates a search for a specific item * @param lucenedir This parameter represents the directory that the index holds * @param field represents the item that needs to be searched (that is, the property character at write time
        Segment name) * @param qu keyword to retrieve/public static void Testtermquery (String lucenedir,string field, string qu) { Indexreader reader = null;//Declare index reader try{//CREATE index directory, where parameter Lucenedir indicates index to repository directory directory
            Dir=fsdirectory.open (Paths.get (Lucenedir));
            Create an index reader Reader=directoryreader.open (dir); //Create an index searcher Indexsearcher is=new indexsearcher (reader);
            Creates a query, the parameter field represents the property field, qu The keyword that is retrieved Term t=new Term (field,qu);

            Query query=new termquery (t);

            The first 10 matching data is queried here, and the second parameter is 10 for the top 10 topdocs hits=is.search (query, 10);

            Print SYSTEM.OUT.PRINTLN ("Retrieve Document fields + Field +", Match "+qu+", Query to "+hits.totalhits+");
                Traverses the queried data for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); String id = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ()//Get ID string title = Doc.get (" title
                "==null" "" "":d Oc.get ("title"). ToString ()///Get title//due to write, the Field.Store.NO is set to indicate that the field is not stored, so it is not read here String centent = Doc.get ("centent") ==null "":d oc.tostring ();//get content//print data SYSTEM.O
            UT.PRINTLN ("id" is: "+id+", the title is: "+title);" }}catch (Exception e) {System.out.println ("An exception has occurred!");
        E.printstacktrace ();
            }finally{try {reader.close ();//Close the stream, make sure to close the catch (IOException e) { /** * Demo Parse query expression (here demonstrates Query Title field property) * @param lucenedir This parameter represents the directory that the index holds * @param titlest  R need to retrieve the keyword */public static void Testqueryparser (String lucenedir,string titlestr) {Indexreader reader = null;//declares the index reader try{//create the index directory where the parameter Lucenedir represents the index storage directory directory Dir=fsdirectory.open (Path
            S.get (Lucenedir));
            Create an index reader Reader=directoryreader.open (dir);

            Create an index searcher Indexsearcher is=new indexsearcher (reader); Create standard word breaker (commonly used in English, not suitable for Chinese use) Analyzer analyzer=new standardanalyzer ();
            Standard word breaker//Create Queryparser query, Query Title property field Queryparser parser=new queryparser ("title", analyzer);

      Set up query Keywords Query=parser.parse (TITLESTR);      The first 10 matching data is queried here, and the second parameter is 10 for the top 10 topdocs hits=is.search (query, 10);

            Print information System.out.println ("Match" +titlestr+, query to "+hits.totalhits+");
                Traverses the queried data for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); String id = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ()//Get ID string title = Doc.get (" title
                "==null" "" "":d Oc.get ("title"). ToString ()///Get title//due to write, the Field.Store.NO is set to indicate that the field is not stored, so it is not read here String centent = Doc.get ("Centent"). toString ();//get content//Print data System.out.println ("
            ID is: "+id+", the title is: "+title";
            }}catch (Exception e) {System.out.println ("An exception occurred!");
        E.printstacktrace ();
            }finally{try {reader.close ();//Close the stream, make sure to close the catch (IOException e) { }}/** * Demo specified item scopeSearch * @param lucenedir This parameter represents the directory in which the index is stored/public static void Testtermrangequery (String lucenedir) {Inde Xreader reader = null;//Declare index reader try{//CREATE index directory, where parameter Lucenedir represents index repository Directory directory Dir=fsdir
            Ectory.open (Paths.get (Lucenedir));
            Create an index reader Reader=directoryreader.open (dir);

            Create an index searcher Indexsearcher is=new indexsearcher (reader); * * Create Termrangequery query, query Title property field from a to e range * generally for English, the ASC code range query has a little meaning, * Chinese characters into
             Line ASC code value comparison does not make much sense, * General Digital range Query Numericrangequery used a little more, * such as price, age, amount, quantity and so on are related to numbers, the number of query demand is also very common
             * * The following two Boolean values are used to control whether two or more upper and lower boundary values are included. * * Termrangequery query=new termrangequery ("title", New Bytesref ("a". GetBytes ()), New Bytesref ("E". GetBytes ())

            , true, true);

            The first 10 matching data is queried here, and the second parameter is 10 for the top 10 topdocs hits=is.search (query, 10); Iterate over the number of queriesAccording to the for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); String id = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ();//Get id string title = Doc.get (" title ") ==null ? "":d Oc.get ("title"). ToString ()//Get title//Print data SYSTEM.OUT.PRINTLN ("ID is:" +id
            + ", the title is:" +title);
            }}catch (Exception e) {System.out.println ("An exception occurred!");
        E.printstacktrace ();
            }finally{try {reader.close ();//Close the stream, make sure to close the catch (IOException e) { /** * Demo Specifies a range of numbers * @param lucenedir This parameter represents the directory in which the index is stored */public static void T Estnumericrangequery (String lucenedir) {Indexreader reader = null;//declares index reader try{//CREATE INDEX directory.
            Here the parameter Lucenedir represents the index repository directory Dir=fsdirectory.open (Paths.get (Lucenedir)); Create an index reader reAder=directoryreader.open (dir);

            Create an index searcher Indexsearcher is=new indexsearcher (reader);
             * * Create Numericrangequery query, Query ID property field from 1 to 2 * * After two Boolean values to control whether or not to contain two upper and lower boundary values.

            * * numericrangequery<integer> query=numericrangequery.newintrange ("id", 1, 2, true, true);

            The first 10 matching data is queried here, and the second parameter is 10 for the top 10 topdocs hits=is.search (query, 10);
                Traverses the queried data for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); String id = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ()//Get ID string title = Doc.get (" title "==null" "" ":d Oc.get (" title "). ToString ()//Get title//Print Data System.out.println ("
            ID is: "+id+", the title is: "+title";
            }}catch (Exception e) {System.out.println ("An exception occurred!");
        E.printstacktrace ();
    }finally{        try {reader.close ();//close stream, be sure to close} catch (IOException e) {}} /** * Demo Specifies the start of the string search * @param lucenedir This parameter represents the directory in which the index is stored/public static void Testprefixquery ( 
            String lucenedir) {Indexreader reader = null;//declares index reader try{//CREATE index directory where parameter Lucenedir represents index storage directory
            Directory Dir=fsdirectory.open (Paths.get (Lucenedir));
            Create an index reader Reader=directoryreader.open (dir);

            Create an index searcher Indexsearcher is=new indexsearcher (reader); * * Create numericrangequery query, * Query Title property field Word Index with W start data * * * prefixquery

            Query=new Prefixquery (New Term ("title", "W"));

            The first 10 matching data is queried here, and the second parameter is 10 for the top 10 topdocs hits=is.search (query, 10);
                Traverses the queried data for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); Stringid = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ()//get ID String title = doc.get (" title ") ==null?" ":d OC . Get ("title"). ToString ()//Gets the title//print data SYSTEM.OUT.PRINTLN ("ID is:" +id+, title is
            : "+title);
            }}catch (Exception e) {System.out.println ("An exception occurred!");
        E.printstacktrace ();
            }finally{try {reader.close ();//Close the stream, make sure to close the catch (IOException e) { /** * Demo Multi-condition query * @param lucenedir This parameter represents the directory in which the index is stored * * * public static void Testboolea Nquery (String lucenedir) {Indexreader reader = null;//Declaration Index Reader try{//CREATE index directory, where parameter Lucenedir table
            Index Storage Directory directory Dir=fsdirectory.open (Paths.get (Lucenedir));
            Create an index reader Reader=directoryreader.open (dir);

            Create an index searcher Indexsearcher is=new indexsearcher (reader); ========== Create multiple query barsThe pieces begin =============== Booleanquery.builder booleanquery=new booleanquery.builder (); 
             * * Condition 1, * Create the Numericrangequery query, Query ID property field from 1 to 2 in the range * Two Boolean values to control whether or not to contain two upper and lower boundary values. */numericrangequery<integer> Query1=numericrangequery.newintrange ("id", 1, 2, true, True)
            ; * * Add query criteria 1 to the combo Finder * BooleanClause.Occur.MUST with (equivalent to and) * BOOLEANCLAUSE.OCCUR.S Hould means or (equivalent to or) * BooleanClause.Occur.MUST_NOT means not containing (equivalent to not)/Booleanquery.add (

            Query1,booleanclause.occur.must);
            * * Condition 2, * Create Numericrangequery query, * Query Title property field Word Index with 12 start of data * *
            Prefixquery query2=new prefixquery (New Term ("title", "12")); * * Add query criteria 2 to the combo Finder * BooleanClause.Occur.MUST with (equivalent to and) * BOOLEANCLAUSE.OCCUR.S
Hould denotes or (equivalent to or)             * BooleanClause.Occur.MUST_NOT means not including (equivalent to not)/Booleanquery.add (Query2,booleanclau Se.
            Occur.must); ========== Create multiple query criteria end ===============/* Here Query the first 10 matching data, the second parameter is 10 for the first 10 * Here query retrieve table

            The data that satisfies condition 1 and satisfies condition 2 at the same time * * Topdocs Hits=is.search (Booleanquery.build (), 10);
                Traverses the queried data for (Scoredoc scoreDoc:hits.scoreDocs) {Document doc=is.doc (scoredoc.doc); String id = doc.get ("id") ==null "" ":d Oc.get (" id "). toString ()//Get ID string title = Doc.get (" title "==null" "" ":d Oc.get (" title "). ToString ()//Get title//Print Data System.out.println ("
            ID is: "+id+", the title is: "+title";
            }}catch (Exception e) {System.out.println ("An exception occurred!");
        E.printstacktrace (); }finally{try {reader.close ();//Close the stream, be sure to close the} catch (Ioexception e) {}}}/** * Test method above * @param args * @throws Exception/PU

        Blic static void Main (string[] args) throws Exception {//Representing index directory String lucenedir = "D://lucene";

        Test the method to write data to the index file//writeindex (lucenedir); Test Retrieve property field title, search keyword for demo//testtermquery (Lucenedir, "title", "De

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.