In Lucene, Booleanclause is used to represent a Boolean query clause relationship class, including: BooleanClause.Occur.MUST means and,booleanclause.occur.must_not represents not, BooleanClause.Occur.SHOULD represents or.
Here's an example.
- **
- * Query according to information classification and keywords
- * @param type, resource, with a value of news or product
- * @param searchkey, search for keywords
- * @return Hits
- */
- Public Hits executesearch (String keyword)
- {
- Hits result = null;
- if(keyword! = null &&!keyword.equals (""))
- {
- Try
- {
- //construct an array by keyword
- string[] key = new String[]{keyword,type};
- //Declare a corresponding array of fields at the same time
- string[] fields = {"title"};
- //Declaration booleanclause.occur[] Array, which represents the relationship between multiple conditions
- Booleanclause.occur[] flags=new Booleanclause.occur[]{booleanclause.occur.must, BooleanClause.Occur.MUST};
- Chineseanalyzer Analyzer = new chineseanalyzer ();
- //Use Multifieldqueryparser to get the query object
- Query query = Multifieldqueryparser.parse (key, fields, flags, analyzer);
- //c:/index indicates the directory where our index files are located
- Indexsearcher searcher = new indexsearcher ("C:/index");
- //Query Results
- result = Searcher.search (query);
- } catch (Exception e)
- {
- E.printstacktrace ();
- }
- }
- return result;
- }
Multi-Conditional index relationships
Booleanclause classes used to represent Boolean query clause relationships, including: BooleanClause.Occur.MUST,
Booleanclause.occur.must_not,booleanclause.occur.should. There are 6 combinations of the following:
1. Must and must: Gets the intersection of a query clause.
2. Must and Must_not: indicates that the query results cannot contain the results of the query clause that the must_not corresponds to.
3. Must_not and Must_not: no meaning, no results are retrieved.
4. Should with must, should and must_not:should and must, the result is the retrieval of the MUST clause
Results. With Must_not, the function is the same as must.
5. Should and should: represents the "or" relationship, and the final result is the set of all search clauses.
Lucene single-domain multi-condition query