How to create more complex Lucene query strings?

老子叫甜甜 提交于 2019-12-07 05:20:41

问题


This question is a spin-off from this question. My inquiry is two-fold, but because both are related I think it is a good idea to put them together.

  • How to programmatically create queries. I know I could start creating strings and get that string parsed with the query parser. But as I gather bits and pieces of information from other resources, there is a programattical way to do this.
  • What are the syntax rules for the Lucene queries?

--EDIT--

I'll give a requirement example for a query I would like to make:
Say I have 5 fields:

  1. First Name
  2. Last Name
  3. Age
  4. Address
  5. Everything

All fields are optional, the last field should search over all the other fields. I go over every field and see if it's IsNullOrEmpty(). If it's not, I would like to append a part of my query so it adds the relevant search part.
First name and last name should be exact matches and have more weight then the other fields. Age is a string and should exact match. Address can varry in order. Everything can also varry in order.

How should I go about this?


回答1:


Use the BooleanQuery class to compose query objects. Create one of these and add() other Query objects to it to create a larger, disjunctive query:

  • BooleanQuery q = new BooleanQuery();
  • q.add(qFirstName, Occur.SHOULD);
  • q.add(qLastName, Occur.SHOULD);
  • ...

Atomic queries can be built with the Term and TermQuery classes.

(Links and example are for Lucene Java, but .NET should be similar.)



来源:https://stackoverflow.com/questions/532512/how-to-create-more-complex-lucene-query-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!