lucene7.1.0 (ii) Helloworld_ search engine

Source: Internet
Author: User
Package Com.ljl.lucene.demo.helloworld;
Import Org.apache.lucene.analysis.standard.StandardAnalyzer;
Import org.apache.lucene.document.Document;
Import Org.apache.lucene.document.Field;
Import Org.apache.lucene.document.TextField;
Import Org.apache.lucene.index.DirectoryReader;
Import Org.apache.lucene.index.IndexWriter;
Import Org.apache.lucene.index.IndexWriterConfig;
Import Org.apache.lucene.index.Term;
Import Org.apache.lucene.search.IndexSearcher;
Import Org.apache.lucene.search.Query;
Import Org.apache.lucene.search.TermQuery;
Import Org.apache.lucene.search.TopDocs;

Import Org.apache.lucene.store.RAMDirectory;

Import java.io.IOException; /** * Simplest Dmeo * * @author Jialen * @create 2017-10-21 13:54 **/public class Helloworldlucene {public static VO
        ID Main (string[] args) throws IOException {long starttime = System.currenttimemillis ();
        SYSTEM.OUT.PRINTLN ("***************** Retrieval Begins **********************");
   Creates a memory directory object, so the index generated here is not placed on disk, but in memory.     Directory type in public indexwriter (directory D, Analyzer A, Boolean create)//, and in the Lucene tool there are two subclasses, respectively Ramdirectory and Fsdirectory//These two directory degrees can be indexed as the storage path//ramdirectory is stored in an area of memory, Fsdirectory is stored in the file system disk//Although add do
        The cument process is the same as using fsddirectory, but because it is an area in memory, if the memory in the Ramdirectory is not written to disk, the contents will disappear when the virtual machine exits.
        You need to transfer the contents of the ramdirectory to fsdirectory at once.
            Ramdirectory directory = new Ramdirectory (); /* When creating a IndexWriter instance, set its related configuration by indexwriterconfig: detail: http://blog.itpub.net/28624388/viewspace-7
             66134/* Public Indexwriterconfig (Analyzer Analyzer) * Analyzer: Word breaker Object *
              * StandardAnalyzer is a built-in "standard analyzer" in Lucene, and can do the following: to the original sentence according to the blanks in the word, all uppercase letters can be converted to lowercase letters Can remove some useless words, such as "is", "the", "are" and other words, also removed all punctuation/indexwriterconfig writerconfig = new indexwriterconf
            IG (New StandardAnalyzer ());
            /* *indexwriter is used to update or create an index.
             It is not used to read indexes. * Create an index write object that can either write the index to disk or write to memory. Parameter Description: * Public indexwriter (Directory directory, indexwriterconfig conf) * Directory: Catalog objects, can also be FSD Irectory Disk Directory Object * conf: Write Object control/indexwriter writer = new IndexWriter (Directory, write
        Rconfig); Create Document object, the index created in Lucene can be seen as a table in the database,//The table can have fields, add content to the inside can be added to the query according to the field////The Doc object created below adds three fields, respectively, NA
            me,sex,dosomething, Document doc = new document (); /* Parameter Description public field (string name, string value, FieldType type) * Name: Field name * Valu E: Value of the field store: * textfield.type_stored: Store field value/Doc.add (new Field ("Name", "Lin Zhengle")
        , textfield.type_stored));
        Doc.add (New Field ("Address", "Shanghai, China", textfield.type_stored);
        Doc.add (New Field ("Dosometing", "I am Learning Lucene", textfield.type_stored)); Writer.adddocument (doc); Writer.close (); This can be turned off early because the dictory has nothing to do with IndexWriter after it has been written to memory//Because the index is in memory, so it should be tested immediately after it has been stored, otherwise it will not be retrieved after the application is closed/created index
        Searcher retrieves the index object in which to pass the memory directory objects written above Directoryreader Ireader = directoryreader.open (directory);
        Indexsearcher searcher = new Indexsearcher (Ireader); Encapsulates a term combination object based on the search keyword and then encapsulates the query object//dosometing is the field defined above, Lucene is the keyword to retrieve//termquery is the most basic atom in a Lucene query
        Query, from its name term we can see that it can only query for one field. Term This class is the lowest unit of search. It is similar to field in the indexing process.
        Set up a search unit, SearchType represents the filed to search for, Searchkey represents the keyword query query = new Termquery (New Term ("Dosometing", "Lucene"));
        Query query = new Termquery (New Term ("Address", "Shanghai, China");

        Query query = new Termquery (new Term ("name", "Cheng")); Topdocs the top N search results for a matching search condition.
        It is a simple container for pointers to documents that point to the output of their search results.
        Fields of the Topdocs class://scoredoc[] Scoredocs--the top query.

        int Totalhits--The total number of queries hit. To the index directory query, return is the Topdocs object, which is stored in the above docuMent Document Object Topdocs rs = searcher.search (query, 100);
        Long endtime = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Total spent" + (Endtime-starttime) + "milliseconds, retrieve" + Rs.totalhits + "record.
        "); for (int i = 0; i < rs.scoreDocs.length i++) {//Rs.scoredocs[i].doc is to get the flag bit ID in the index, start recording doc from 0
            Ument Firsthit = Searcher.doc (Rs.scoredocs[i].doc);
            System.out.println ("Name:" + Firsthit.getfield ("name"). StringValue ());
            SYSTEM.OUT.PRINTLN ("Address:" + Firsthit.getfield ("Address"). StringValue ());
        System.out.println ("dosomething:" + Firsthit.getfield ("dosometing"). StringValue ());
        } writer.close ();
        Directory.close ();
    SYSTEM.OUT.PRINTLN ("***************** Retrieval End **********************");
 }
}

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.