Getting started with Lucene-how to write a Lucene program

Source: Internet
Author: User


Lucene Version: 7.1

Key points for using Lucene

  1. Create a document, add a file (Field);
  2. Add documents to IndexWriter;
  3. Use Queryparser.parse () to build the query content;
  4. Use the search () method of indexsearcher to make inquiries;

First, the basic process of creating an index

Open a Directory, storing index files
Fsdirectory refers to a folder that can be stored in a cache ramdirectory
Indexpath: File path
Directory dir = Fsdirectory.open (Paths.get (Indexpath));
Instantiate Analyzer, working with text files
StandardAnalyzer uses the Unicode text segmentation algorithm to convert the symbols to lowercase, filtering out the usual terms
Different types of analyzer are required for different languages, see: https://lucene.apache.org/core/7_1_0/analyzers-common/overview-summary.html
Analyzer Analyzer = new StandardAnalyzer ();
Index configuration Content
Indexwriterconfig IWC = new Indexwriterconfig (analyzer);
Create,append,create_or_append
Iwc.setopenmode (openmode.create);
Instantiate IndexWriter
IndexWriter writer = new IndexWriter (dir, IWC);
Instantiate document, which indicates the text content of the file and the creation time and location information, etc.
Document doc = new document ();
"Path": Index field
Doc.add (New Stringfield ("Path", file.tostring (), Field.Store.YES));
Doc.add (New Longpoint ("Modified", lastmodified));
Doc.add (New TextField ("Contents", New BufferedReader (new InputStreamReader (Stream, standardcharsets.utf_8)));
Add to IndexWriter
Writer.adddocument (DOC);
Shut down
Writer.close ();


Second, the search basic process

Indexreader reader = Directoryreader.open (Fsdirectory.open (Paths.get (index)));
Indexsearcher searcher = new Indexsearcher (reader);
Analyzer Analyzer = new StandardAnalyzer ();
Indexed fields
Queryparser parser = new Queryparser ("contents", analyzer);
Query results
Query query = parser.parse ("123456");
Topdocs results = searcher.search (query, 5 * hitsperpage);
Scoredoc[] hits = Results.scoredocs;

Getting started with Lucene-how to write a Lucene program

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.