0

I am having trouble implementing a function that would apply composite fields to Lucene queries.

Instead of typing the lucene query:

title:"The Right Way" subject:"The Right Way"

I want users to be able to type:

all:"The Right Way"

Where all is composite field consisting of real fields title and subject. The function should generate a valid lucene query with constituents of a composite field expanded out.

String query = applyCompositeFields(String query, String compositeField, String[] subFields) {

} The supplied query may be any query according to the lucene query syntax (see: http://lucene.apache.org/core/old_versioned_docs/versions/3_0_0/queryparsersyntax.html#Range%20Searches)

For example:

all:[20020101 TO 20030101]

Should be expanded to:

title:[20020101 TO 20030101] subject:[20020101 TO 20030101]

Any ideas on how to do this neatly without breaking complex query inputs?

I've tried using the Lucene query object model, however, the field names cannot be set on the query elements, so its useless.

4

1 回答 1

0

我认为MultiFieldQueryParser是您正在寻找的。

编辑

MultiFieldQueryParser 将:

  • "The Right Way"如果字段在查询字符串 ( )中不明确,则在多个字段之间分派查询
  • 否则对单个字段使用普通查询 ( title:"The Right Way")。

例如,

    MultiFieldQueryParser qp = new MultiFieldQueryParser(
            Version.LUCENE_36, new String[] { "subject", "body" },
            new KeywordAnalyzer());
    System.out.println(qp.parse("subject:\"hello\" body:test AND [1222 TO 2333]"));

印刷

    subject:hello +body:test +(subject:[1222 TO 2333] body:[1222 TO 2333])

如果您想坚持使用虚拟复合字段的语法all,您可以扩展 QueryParser 以在字段名称为all. 您可以通过查看MultiFieldQueryParser的源代码获得一些启发。

于 2012-05-10T12:29:08.170 回答