Lucene single-domain multi-condition query

Source: Internet
Author: User

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.

  1. **
  2. * Query according to information classification and keywords
  3. * @param type, resource, with a value of news or product
  4. * @param searchkey, search for keywords
  5. * @return Hits
  6. */
  7. Public Hits executesearch (String keyword)
  8. {
  9. Hits result = null;
  10. if(keyword! = null &&!keyword.equals (""))
  11. {
  12. Try
  13. {
  14. //construct an array by keyword
  15. string[] key = new String[]{keyword,type};
  16. //Declare a corresponding array of fields at the same time
  17. string[] fields = {"title"};
  18. //Declaration booleanclause.occur[] Array, which represents the relationship between multiple conditions
  19. Booleanclause.occur[] flags=new Booleanclause.occur[]{booleanclause.occur.must, BooleanClause.Occur.MUST};
  20. Chineseanalyzer Analyzer = new chineseanalyzer ();
  21. //Use Multifieldqueryparser to get the query object
  22. Query query = Multifieldqueryparser.parse (key, fields, flags, analyzer);
  23. //c:/index indicates the directory where our index files are located
  24. Indexsearcher searcher = new indexsearcher ("C:/index");
  25. //Query Results
  26. result = Searcher.search (query);
  27. } catch (Exception e)
  28. {
  29. E.printstacktrace ();
  30. }
  31. }
  32. return result;
  33. }

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

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.