Apache Lucene (full-text search engine)-search (Article 2 ),

Source: Internet
Author: User

Apache Lucene (full-text search engine)-search (Article 2 ),
Directory

1. Apache Lucene (full-text search engine)-index creation (Article 1): http://www.cnblogs.com/hanyinglong/p/5387816.html

2. Apache Lucene (full-text search engine)-search (Article 2): http://www.cnblogs.com/hanyinglong/p/5391269.html

3. Apache Lucene (full-text search engine)-word divider (Article 3 ):

This project Demo has been uploaded GitHub, welcome to everyone fork download learning: https://github.com/kencery/Lucene_Compass (the project has a very detailed comment)

1. Search Ideas

A. When you search for a Document, first search for the Document in the vocabulary to obtain a list of Document numbers that meet the criteria, and then retrieve the data (Document) from the index library based on the Document numbers ).

B. How Lucene implements search

B .1 first, convert the string to be queried into a Query object. to convert a Query string to a Query string, use QueryParser and MultiFieldQueryParser. The Query string must also be obtained by the Analyzer ), the Analyzer used for search is required to be consistent with the Analyzer used for index creation. Otherwise, the correct results may not be found.

B .2 call indexSearcher. the return value of this method is TopDocs. It is an object that contains multiple information of the result. totalHits indicates the total number of records, the ScoreDoc array is an object that represents the correlation score, document number, and other information of a result.

B .3 retrieves the list of required Document numbers, and calls Document doc=indexSearcher.doc (docId) to retrieve the Document data corresponding to the specified number.

2. Understand the core Search Class

A. public class org. apache. lucene. search. IndexSearcher

A.1 IndexSearch is used to search on an indexed index. It provides many search methods, some of which are implemented in the abstract base class Searcher.

A.2 it can only open one index in read-only mode, so multiple IndexSearcher instances can operate on one index.

A.3 after IndexSearcher is used, you need to release the resource by calling indexSearcher. getIndexReader (). close ();

B. public final class org. apache. lucene. index. Term

B .1 Term is the basic unit of search. A Term object consists of two Sting fields, including the field name and field value.

B .2 when searching, you can create a Term object and use it with TermQuery. The first parameter indicates which Field of the document to search, the second parameter represents the keyword to be queried.

Query query = new TermQuery (new Term ("content", "lucene "));

TopDocs topDocs = indexSearcher. search (query, 100); // return the first n results.

B .3 this Code uses Lucene to find all documents containing the word lucene in the content field. Because the TermQuery object inherits from its abstract parent Query Class, you can use the Query type on the left of the equation.

C. public abstract class org. apache. lucene. search. Query

C.1 Query is an abstract class. The purpose of this class is to encapsulate the Query string entered by the user into a Query that Lucene can recognize.

C.2 Query contains many sub-categories (you must use a series of sub-classes to express the specific search requirements, which are described in the four modules). For details, press Ctrl + T to view

D. public class org. apache. lucene. search. TermQuery

D.1 TermQuery is a subclass of the abstract class Query. It is also the most basic Query Class supported by Lucene.

D.2 generate a TermQuery object completed by the following statement (its constructor only accepts one parameter, which is a Term object ).

TermQuery query = new TermQuery (new Term ("content", "lucene "));

E. public class org. apache. lucene. search. TopDocs

E.1 TopDocs is used to save the search results. The idea is to point to the first N search results of matching search conditions, it is a simple pointer container pointing to the document output by their search results.

E.2 based on performance considerations, the TopDocs instance does not load all documents that match the query from the index, instead, a small part is displayed to the user each time based on the conditions you give (paging principle ).

F. QueryParser and MultiFieldQueryParser

F.1 QueryParser Query analyzer processes the Query conditions entered by the user and converts the non-format words entered by the user into Query objects available in the background.

F.2 MultiFieldQueryParser is a subclass of QueryParser. Compared with the parent class, MultiFieldQueryParser can be searched in multiple attributes.

3. General Implementation of keyword search

A. code to open source on github, address: https://github.com/kencery/Lucene_Compass

1/** 2 * Test search 3 * @ throws Exception 4 */5 @ Test 6 public void testSearch () throws Exception {7 // 1 Search Condition 8 String queryCondition = "lucene"; 9 // 2 execute the search (lucene) 10 List <Article> articles = new ArrayList <Article> (); 11 // -------------------------- search code --------------------------- 12 Directory directory = FSDirectory. open (Paths. get (". /indexDir/"); // index library directory 13 Analyzer analyzer = new StandardAnalyzer (); // tokenizer 14 15 // 2.1 converts the Query string into a Query object (only queried in title) 16 QueryParser queryParser = new QueryParser ("title", analyzer ); 17 Query query = queryParser. parse (queryCondition); 18 19 // 2.2. The search result is 20 IndexReader indexReader = DirectoryReader. open (directory); 21 IndexSearcher indexSearcher = new IndexSearcher (indexReader); 22 TopDocs topDocs = indexSearcher. search (query, 100); // return the first n results of the query 23 24 Integer count = topDocs. totalHits; // Summary Result quantity 25 ScoreDoc [] scoreDocs = topDocs. scoreDocs; // return the first N results 26 27 // 2.3 processing result 28 for (int I = 0; I <scoreDocs. length; I ++) {29 ScoreDoc scoreDoc = scoreDocs [I]; 30 int docId=scoreDoc.doc; 31 System. out. println ("score:" + scoreDoc. score + ", internal number:" + docId); 32 // extract the true Document data according to the internal number 33 Document doc1_indexsearcher.doc (docId ); 34 // convert the document to Article35 Article article = new Article (Integer. parseInt (doc. get ("id"), doc. get ("title"), doc. get ("content"); 36 articles. add (article); 37} 38 // -------------------------------------------------------------- 39 // 3 the console displays the result 40 System. err. println ("Summary Results:" + count); 41 for (Article article: articles) {42 System. out. println ("query result:" + article); 43} 44 indexSearcher. getIndexReader (). close (); 45}
4. Descriptions of built-in Query objects

A. BooleanQuery Boolean search, corresponding query statement:

A.1 BooleanQuery is a kind of Query that is often used during actual development. It is actually a combination of Query, in use, you can add various Query objects and indicate the logical relationships between them.

A.2 BooleanQuery can be nested (BooleanQuery is a Boolean clause container)

(1) One BooleanQuery can be a Condition Clause of another BooleanQuery.

(2) The number of Boolean clauses cannot exceed 1024.

A.3 public void add (Query query, BooleanClause. occur occur), BooleanClause indicates the class of the Boolean query clause relationship, including: BooleanClause. occur. MUST, BooleanClause. occur. MUST_NOT, BooleanClause. occur. it contains the following six combinations.

(1) MUST and MUST: Get the intersection of the two query clauses.

(2) MUST and MUST_NOT: indicates that the query results cannot contain the search results of the query clause corresponding to MUST_NOT.

(3) MUST_NOT and MUST_NOT: meaningless. No results are returned.

(4) shoshould and MUST: meaningless. The result is the retrieval result of the MUST clause.

(5) shold and MUST_NOT: The functions are the same as those of MUST, which is equivalent to the retrieval results of MUST and MUST_NOT.

(6) The relationship between shocould and shocould: "or". The final search result is the union of all search clauses.

Query query1 = new MatchAllDocsQuery ();

Query query2 = NumericRangeQuery. newIntRange ("id", 5, 15, true, false );

BooleanQuery. add (query1, Occur. MUST );

BooleanQuery. add (query2, Occur. MUST_NOT );

The corresponding query string is: + *: *-id: [5 TO 15}

B. NumericRangeQuery range search

B .1 NumericRangeQuery. newIntRange (String field, Integer min, Integer max, boolean minInclusive, boolean maxcompute SIVE); two parameters after the boolean parameter indicate whether two critical values can be added to the search.

For example:Query query = NumericRangeQuery. newIntRange ("id", 5, 15, true, false );

Corresponding query statement: the corresponding query string is: id: [5 TO 15}

C. PrefixQuery prefix query: query the information starting with a field

Term term = new Term ("content", "this is ");

PrefixQuery query = new PrefixQuery (term );

Corresponding query statement: content: This is *

D. PhraseQuery phrase Query

D.1 PhraseQuery provides a "slope" parameter to indicate the number of irrelevant words that can be inserted between two words of a phrase.

PhraseQuery phraseQuery = new PhraseQuery ();

PhraseQuery. add (new Term ("title", "lucene "));

PhraseQuery. add (new Term ("title", "Framework "));

PhraseQuery. setSlop (5); // The maximum interval between two parameters is 5.

Corresponding query statement: title: "lucene framework "~ 5

E. FuzzyQuery fuzzy query (FuzzyQuery (Term term, int maxEdits ))

E.1 minimum similarity of maxEdits. The default value is 2. The smaller the number, the smaller the query result. The value range is 0, 1, and 2. If the value is 0, it is equivalent to TermQuery.

Query query = new FuzzyQuery (new Term ("title", "Hangzhou X"), 2 );

Corresponding query statement: title: Required x ~ 2

F. WildcardQuery wildcard search

F.1 * represents 0 to multiple characters ,? Represents a single character.

Query query = new WildcardQuery (new Term ("title", "lu * n? "));

The corresponding query statement is: title: lu * n?

G. MatchAllDocsQuery search all

Query query = new MatchAllDocsQuery ();

The corresponding query statement is :*:*

5. sorting, filtering, and highlighting

A. the remarks in the demo are already very detailed. If you want to learn more, please go to github to download the project and check it yourself (there are detailed remarks ).

B. GitHub address: https://github.com/kencery/Lucene_Compass/tree/master/Lucene_5.5

C. The corresponding branch of sorting is paie_seven. The corresponding branch of filtering is paie_eight. The branch of highlighting is paie_six.

 

I hope you can learn something from the middle. If you have any questions, please leave a message or go to GitHub to see the javasedemo or add my QQ. We will discuss it together.

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.