如何创建更复杂的Lucene查询字符串?

时间:2022-04-28 03:07:42

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?
  • Lucene查询的语法规则是什么?

--EDIT--

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

我将给出一个我想要的查询的需求示例:假设我有5个字段:

  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.

所有字段都是可选字段,最后一个字段应搜索所有其他字段。我遍历每个字段,看看它是否是IsNullOrEmpty()。如果不是,我想附加我的查询的一部分,以便添加相关的搜索部分。名字和姓氏应该是完全匹配,并且比其他字段具有更多权重。年龄是一个字符串,应该完全匹配。地址可以按顺序变化。一切也可以按顺序变化。

How should I go about this?

我该怎么办呢?

1 个解决方案

#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类来组合查询对象。创建其中一个并向其添加()其他Query对象以创建更大的析取查询:

  • BooleanQuery q = new BooleanQuery();
  • 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.

可以使用Term和TermQuery类构建原子查询。

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

(链接和示例适用于Lucene Java,但.NET应该类似。)

#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类来组合查询对象。创建其中一个并向其添加()其他Query对象以创建更大的析取查询:

  • BooleanQuery q = new BooleanQuery();
  • 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.

可以使用Term和TermQuery类构建原子查询。

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

(链接和示例适用于Lucene Java,但.NET应该类似。)