Booleanquery for Lucene query objects (Memo)

Source: Internet
Author: User

Before looking at this object, we need to know what BooleanQuery can do. What can it do? It can perform combined queries. As we all know, generic advanced queries (for example, combinations of queries should be used for job searches without any worries in the future) will be used for combined queries. It is a combination, it should be to search for multiple entries, each entry should be its Clause.

Let's talk about the main attributes and methods of the BooleanQuery class.

/** A Query that matches ents matching boolean combinations of other
* Queries, e.g. {@ link TermQuery} s, {@ link PhraseQuery} s or other
* BooleanQuerys.
*/
Public class BooleanQuery extends Query implements Iterable <BooleanClause>

You can clearly see that BooleanQuery inherits the abstract class of Query.

/** Adds a clause to a boolean query.
*
* @ Throws toomanyclses if the new number of clses exceeds the maximum clause number
* @ See # getMaxClauseCount ()
*/
Public void add (Query query, BooleanClause. Occur occur) {// you can add other Query objects as a condition of Occur to control their composite relationships.
Add (new BooleanClause (query, occur ));
}

/** Adds a clause to a boolean query.
* @ Throws toomanyclses if the new number of clses exceeds the maximum clause number
* @ See # getMaxClauseCount ()
*/
Public void add (BooleanClause clause ){
If (clses. size ()> = maxClauseCount) // ----------- the upper limit of the query condition is added. if the upper limit is exceeded, an exception is reported.
Throw new toomanyclses ();

Clses. add (clause );
}

Next, let's take a look at how the composite relationships of various conditions are controlled. There are several relationships. Next we will analyze Occour. It is an enumeration

/** Specifies how clses are to occur in matching documents .*/
Public static enum Occur {

/** Use this operator for clses that <I> must </I> appear in the matching events .*/
MUST {@ Override public String toString () {return "+ ";}},

/** Use this operator for clses that <I> shocould </I> appear in
* Matching events. For a BooleanQuery with no <code> MUST </code>
* Clauses one or more <code> shocould </code> clses must match a document
* For the BooleanQuery to match.
* @ See BooleanQuery # setMinimumNumberShouldMatch
*/
Shocould {@ Override public String toString () {return "";}},

/** Use this operator for clses that <I> must not </I> appear in the matching events.
* Note that it is not possible to search for queries that only consist
* Of a <code> MUST_NOT </code> clause .*/
MUST_NOT {@ Override public String toString () {return "-";}};

}

The meanings of a composite relationship are as follows:
1. MUST and MUST indicate the relationship between "and", that is, "Union ".
2. The former includes MUST and MUST_NOT, and the latter does not.
3. MUST_NOT and MUST_NOT are meaningless.
4. shoshould and MUST indicate that MUST and shoshould have no meaning;
5. shoshould and MUST_NOT are equivalent to MUST and MUST_NOT.
6. The concept of "or" is represented by shocould and shocould.

See the example!

Directory dic = new SimpleFSDirectory (new File (ILuceneManager. default_region_e_e_index_path ));
IndexSearcher searcher = new IndexSearcher (dic );
// ---------- Combined Query
BooleanQuery query = new BooleanQuery ();
// ----------- Place names with Zhe characters
Term term1 = new Term ("ADDRESS", "zhe ");
TermQuery tq1 = new TermQuery (term1 );
BooleanClause clause = new BooleanClause (tq1, BooleanClause. Occur. shocould );
Query. add (clause );

// -------- The highest weight
Term term2 = new Term ("weight", "1 ");
TermQuery tq2 = new TermQuery (term2 );
BooleanClause clause2 = new BooleanClause (tq2, BooleanClause. Occur. MUST );
Query. add (clause2 );

TopDocs tops = searcher. search (query, 10 );
ScoreDoc [] scores = tops. scoreDocs;
Int length = tops. totalHits;
For (int I = 0; I <(length> receivemanagerimpl. DEFAULT_QUERY_NUM? LuceneManagerImpl. DEFAULT_QUERY_NUM: length); I ++ ){
Document targetdocdomainsearcher.doc(scorespolici#.doc );
System. out. println (targetDoc. getFields ("NAME") [0]. stringValue ());
};

System. out. println ("Number of queried results:" + tops. totalHits );

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.