Lucene: Query

Source: Internet
Author: User

Lucene Series

Lucene supports multiple search methods. You can select different methods as needed.

1. Search for entries (search for a single keyword)

The main object is termquery.

The call method is as follows:

1 Term term = new term (field name, search keyword); 2 query = new termquery (TERM); 3 hits = searcher. Search (query );

 

2. Combined search (multiple keywords can be combined for search)

The main object is booleanquery.

The call method is as follows:

1 Term term1 = new term (field name, search keyword); 2 termquery query1 = new termquery (term1); 3 4 term term2 = new term (field name, search keyword ); 5 termquery query2 = new termquery (term2); 6 7 booleanquery = new booleanquery (); 8 booleanquery. add (query1, parameter) 9 booleanquery. add (query2, parameter) 10 11 hits = searcher. search (booleanquery );

 

The core of this method is the add method of booleanquery. The second parameter has three optional values, which correspond to logical or non-relational values. The parameters are as follows:

Booleanclause. occur. Must must contain, similar to logical operations and

Booleanclause. occur. Must _ Not must not be included, similar to the non-

Booleanclause. occur. shocould include, similar to logical operations or

The combination of the three is incredibly useful.

 

 

3. Range search (allows you to search for keyword results within a specified range)

The main object is rangequery.

The call method is as follows:

1 Term term1 = new term (field name, start value); 2 term term2 = new term (field name, end value); 3 rangequery = new rangequery (term1, term2, parameter ); 4 hits = searcher. search (rangequery); 5

The parameters in this method are of the boolean type, indicating whether the boundary is included.

True contains the Boundary

False does not contain boundaries

 

4. prefix search (results with matching start position)

The main object is prefixquery.

The call method is as follows:

1 Term term = new term (field name, search keyword); 2 prefixquery = new prefixquery (TERM); 3 hits = searcher. Search (prefixquery );

 

 

5. phrase search (search based on the combination of fragmented phrases into new phrases)

The main object is phrasequery.

The call method is as follows:

1 Term term1 = new term (field name, search keyword); 2 term term2 = new term (field name, search keyword); 3 4 phrasequery = new phrasequery (); 5 phrasequery. setslop (parameter); 6 phrasequery. add (term1); 7 phrasequery. add (term2); 8 hits = searcher. search (phrasequery );

The setslop parameter sets the maximum allowed interval between two keywords.

 

 

6. Multi-phrase search (specify a prefix keyword first, and then add other keywords to this keyword to form a word for search)

Main object: multiphrasequery

The call method is as follows:

1 Term term = new term (field name, pre-Keyword); 2 term term1 = new term (field name, search keyword); 3 Term term2 = new term (field name, search keyword ); 4 5 multiphrasequery = new multiphrasequery (); 6 7 multiphrasequery. add (TERM); 8 multiphrasequery. add (new term [] {term1, term2}); 9 10 hits = searcher. search (multiphrasequery );

 

 

7. Fuzzy search (as the name suggests)

Main object: fuzzyquery

The call method is as follows:

1 Term term = new term (field name, search keyword); 2 fuzzyquery = new fuzzyquery (term, parameter); 3 hits = searcher. Search (fuzzyquery );

The parameter in this example indicates the Blur degree, which is a floating point decimal point smaller than 1, for example, 0.5f.

 

 

8. wildcard search (as the name suggests)

Main object: wildcardquery

The call method is as follows:

1 Term term = new term (field name, search keyword + wildcard); 2 wildcardquery = new wildcardquery (TERM); 3 hits = searcher. Search (wildcardquery );

The wildcard is divided into two types: * and?

* It indicates any amount of conceited

? Represents any character

 

9. Regular Expression search (as the name suggests)

Main object: regexquery

 

Lucene has many search types. Here we will introduce several of the most common ones. Termquery, booleanquery, and regexquery are the most powerful and commonly used functions.

 

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.