[Tutorial 3 of ipve4.8] Search

Source: Internet
Author: User
Tags java keywords


1. Key Categories

The Lucene search process involves the following main classes:

(1) indexsearcher: class for running the search () method

(2) indexreader: reads index files and provides search interfaces for indexsearcher.

(3) query and its subclass: query object, number of important shards of the search () method

(4) queryparser: the query object is generated based on the search words entered by the user.

(5) topdocs: The first n documents returned by the search () method

(6) scoredocs: Provides the reply query interface for the search results in topdocs.


2. Key Search Steps

(1) Create indexreader

(2) Use indexreader to create indexsearcher

(3) Use queryparser to generate a query object based on Keyword Search

(4) Call indexsearcher. Search () with query as the number of shards to run the search

(5) traverse and process the results with topdocs and scoredocs

The sample code is as follows:

// (1) Create indexreaderdirectory indexdir2 = fsdirectory. open (indexdir); indexreader IR = directoryreader. open (indexdir2); // (2) use indexreader to create indexsearcherindexsearcher searcher = new indexsearcher (IR); // (3) Search for keyword, use queryparser to generate the query object queryparser parser = new queryparser (version. inclue_48, "contents", new simpleanalyzer (version. required e_48); query = NULL; try {query = parser. parse (TERM);} catch (parseexception e) {e. printstacktrace ();} // (4) Call indexsearcher by using query as the limit. search (), run the search topdocs docs = searcher. search (query, 30); // (5) traverse the result with topdocs and scoredocs and process scoredoc [] hits = docs. scoredocs; system. out. println (hits. length); For (scoredoc hit: HITS) {system. out. println ("Doc:" + hit.doc + "score:" + hit. score );}

3. About indexreader

(1) indexreader does not provide constructors. Therefore, you must use the directoryreader. open () method to create an indexreader.

(2) Creating an indexreader requires a large amount of system overhead. Therefore, it is best to use an indexreader repeatedly during all searches. It is recommended to open a new indexreader only when necessary.

(3) When creating indexreader, it will search for the existing index snapshot. If you need to search for the change information in the index, you must open a new reader. Fortunately, the indexreader. Reopen method is an effective method to obtain the new indexreader. It can use the current reader to obtain all the changes in the index even if it consumes less system resources. [The new version number is obsolete. The alternative to be confirmed]


4. Subclass of queryparser and query

For a search, the core statement is:

searcher.search(query, 10);
In this case, the most important shard number is a qeury object. There are two methods to construct a query object: [search for Java keywords in the contents domain as an example]

(1) Use a subclass of query, such as booleanquery, constantscorequery, disjunctionmaxquery, filteredquery, matchalldocsquery, multiphrasequery, multitermquery, phrasequery, spanquery, and termquery to instantiate an external interface:

searcher.search( new TermQuery(new Term("contents","java")), 10);

The following statement structure is clearer

Term term= new Term("contents","java");TermQuery tq = new TermQuery(term);searcher.search(tq , 10);

In addition, search for documents containing Java in the contents domain.

(2) Use the parse () method of queryparser to explain the Input key words in the search and return the query object.

QueryParser parser = new QueryParser(Version.LUCENE_48, "contents",new SimpleAnalyzer(Version.LUCENE_48));Query query = null;try {query = parser.parse("java");} catch (ParseException e) {e.printStackTrace();}TopDocs docs = searcher.search(query, 10);
The preceding statement creates a queryparser, whose default search domain is contents, and converts the search vocabulary to a query object.

Suppose that the default search domain of queryparser is all? How to specify a query search domain?

For more details about queryparser and query subclass, refer

Lucene4.8 tutorial 6 queryparser and query subclass: how to generate a query object http://blog.csdn.net/jediael_lu/article/details/33288793




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.