简体   繁体   English

Lucene:MultiFieldQueryParser中的布尔值OR

[英]Lucene: Boolean OR in MultiFieldQueryParser

I have a database with 10 fields, and I need to construct a query that looks something like the following pseudo code: 我有一个包含10个字段的数据库,我需要构造一个看起来类似于以下伪代码的查询:

theQuery = ((field1 == A) && 
           (field2 == B) && 
           (field3 == C) && 
           (field4 == D) && 
           (field5 == E) && 
           (field6 == F) && 
           (field7 == G) && 
           ((field8 == H) || (field9 == H) || (field10 == H)))

That is to say that I need fields 1-7 to definitely contain the corresponding supplied variable, and I need the variable H to definitely appear in at least one of fields 8-10. 也就是说,我需要字段1-7明确包含相应提供的变量, 并且我需要变量H 明确出现在字段8-10中的至少一个中。

I have been trying to use the MultiFieldQueryParser, but the problem that I have is that the BooleanClauses supplied are MUST, MUST_NOT and SHOULD, and we can set the default operator of the MultiFieldQueryParser to be either AND or OR. 我一直在尝试使用MultiFieldQueryParser,但是我遇到的问题是所提供的BooleanClauses必须为MUST,MUST_NOT和SHOULD,并且我们可以将MultiFieldQueryParser的默认运算符设置为AND或OR。

When I try using AND and setting fields 1-7 with MUST and fields 8-10 with SHOULD, the query parser basically ignores fields 8-10 and gives me back anything that contains the specified data in fields 1-7. 当我尝试使用AND并将字段1-7设置为MUST,将字段8-10设置为SHOULD时,查询解析器基本上会忽略字段8-10,并为我提供包含字段1-7中指定数据的任何内容。

I haven't yet tried setting the default operator to OR, because I'm guessing that the query will return results that contain one or more of the supplied variables in fields 1-10. 我尚未尝试将默认运算符设置为OR,因为我猜测查询将返回包含字段1-10中提供的一个或多个变量的结果。

For those that wish to see code, my code is as follows: 对于那些希望查看代码的人,我的代码如下:

ArrayList queries = new ArrayList();
ArrayList fields = new ArrayList();
ArrayList flags = new ArrayList();

if(varA != null && !varA.equals(""))
{
    queries.Add(varA);
    fields.Add("field1");
    flags.Add(BooleanClause.Occur.Must);
}
//... The same for 2-7
if(varH != null && !varH.equals(""))
{
    queries.Add(varA);
    queries.Add(varA);
    queries.Add(varA);
    fields.Add("field8");
    fields.Add("field9");
    fields.Add("field10");
    flags.Add(BooleanClause.Occur.Should);
    flags.Add(BooleanClause.Occur.Should);
    flags.Add(BooleanClause.Occur.Should);
}
Query q = MultiFieldQueryParser.parse(VERSION.LUCENE_34, 
                                      queries.toArray(), 
                                      fields.toArray(),
                                      flags.toArray(),
                                      theAnalyzer);

Obviously this is somewhat simplified as the ArrayLists don't neatly return me arrays of Strings and BooleanClause.Occurs, but you get the idea. 显然,这有所简化,因为ArrayLists不能整齐地返回String和BooleanClause数组给我,但是您明白了。

Does anyone know of a way of forming a multifield query, including both boolean ANDs and boolean ORs? 有谁知道一种形成多字段查询的方法,包括布尔AND 布尔OR?

Thanks, Rik 谢谢,瑞克

I don't really understand your notation, so it's hard to figure out what the problem is. 我不太了解您的用法,因此很难弄清问题所在。 But just use standard queries: 但是,只需使用标准查询:

 BooleanQuery topQuery = new BooleanQuery();
 topQuery.add(new TermQuery(...), BooleanClause.Occur.Must);
 etc.

Or just do it in text and let the parser parse it for you: +field1:A +field2:B ... 或者只是以文本形式执行,然后让解析器为您解析: +field1:A +field2:B ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM