Case Development Based on lucene: search index and lucene Index

Source: Internet
Author: User

Case Development Based on lucene: search index and lucene Index

Reprinted please indicate the source: http://blog.csdn.net/xiaojimanman/article/details/42884921

The index data in this example comes from the index created in the previous blog. The index contains two documents, each of which contains two domain names and content.


Index Search demo

Let's look at a simple index search demo program before introducing it.

/*** @ Description: Index Search demo */package com. lulei. lucene. study; import java. io. file; import org. apache. lucene. analysis. analyzer; import org. apache. lucene. analysis. standard. standardAnalyzer; import org.apache.e.doc ument. document; import org. apache. lucene. index. directoryReader; import org. apache. lucene. queryparser. classic. queryParser; import org. apache. lucene. search. indexSearcher; import org. apache. luce Ne. search. query; import org. apache. lucene. search. topDocs; import org. apache. lucene. store. directory; import org. apache. lucene. store. FSDirectory; import org. apache. lucene. util. version; public class SearchIndex {public static void main (String [] args) {Directory directory = null; try {// index hard disk storage path directory = FSDirectory. open (new File ("D: // study/index/testindex"); // read the index DirectoryReader dReader = DirectoryRead Er. open (directory); // create an index search object IndexSearcher searcher = new IndexSearcher (dReader); // specify the Word Segmentation technology. The language processing module used here must be consistent with the index creation process, otherwise, the search result is not ideal. Analyzer analyzer = new StandardAnalyzer (Version. required e_43); // create a query. The search term is "Space Vector" QueryParser parse = new QueryParser (Version. required e_43, "content", analyzer); Query query = parse. parse ("Space Vector"); // retrieve the index to obtain the top 10 matching records TopDocs topDocs = searcher. search (query, 10); if (topDocs! = Null) {System. out. println ("found in total" + topDocs. totalHits + "qualified records"); // loop output Record Content for (int I = 0; I <topDocs. scoreDocs. length; I ++) {Document doc = searcher.doc(topdocs.scoredocs? I =.doc); System. out. println ("section" + (I + 1) + "contains -- \ tname: \ t" + doc. get ("name") + "\ tcontent: \ t" + doc. get ("content") ;}/// close resource dReader. close (); directory. close ();} catch (Exception e) {e. printStackTrace ();}}}


The preceding demo program runs as follows:



We can see that the search result of the keyword "Space Vector" is the second document in the index, and the corresponding domain value can be obtained through the document. get method.


Search index core class

In the above index search process, several core classes are used:Directory,DirectoryReader,IndexSearcher,Analyzer,Query,TopDocsThe core classes introduced in the previous article will not be further introduced here. Here we will introduce the classes that have not been introduced before.


DirectoryReader

DirectoryReader is used to read indexes. The created dReader object is used to create an IndexSearcher object for search.

IndexSearcher

IndexSearcher is used for index search. This class discloses several search methods, which are the central link connecting to the index. You can regard the IndexSearcher class as a class that opens the index in read-only mode, more in-depth content about IndexSearcher will be introduced in the blog below.

Query

Lucene contains many specific Query subclass, including TermQuery, BooleanQuery, PhraseQuery, PrefixQuery, PhrasePrefixQuery, TermRangQuery, NumericRangeQuery, FilteredQuery, and SpanQuery, these subclasses will also be described in a later blog.

TopDocs

The TopDocs class is a simple pointer container, which generally refers to the search results of the Top N rankings. The search results are the documents matching the query conditions.


So far, we can use Lucene to implement a simple index creation and search example. In the following blogs, I will introduce the Word Segmentation technology, Query sub-classes, and IndexSearcher search APIs one by one. These parts can be understood through source code reading.

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.