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.

1 Answers1

0

I think that MultiFieldQueryParser is what you are looking for.

Edit

MultiFieldQueryParser will:

  • dispatch the query among several fields if the field is not explicit in the query string ("The Right Way")
  • use a normal query over a single field otherwise (title:"The Right Way").

For example,

    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]"));

prints

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

If you want to stick to your syntax with a virtual composite field all, you could extend QueryParser to add special cases when the field name is all. You can get some inspiration by looking at the source code of MultiFieldQueryParser.

jpountz
  • 9,904
  • 1
  • 31
  • 39
  • How so? Not from what I can tell. – user1387012 May 10 '12 at 12:31
  • The query may also include non composite fields. For example: all:[20020101 TO 20030101] and not subject:lucene – user1387012 May 10 '12 at 12:32
  • MultiFieldQueryParser will not work in this scenario. We have some fields that are composite, other that are not.. also the query is possibly complex. like subject:"hello" body:test AND all:[1222 to 2333] and so on. – user1387012 May 10 '12 at 14:14
  • MultiFieldQueryParser can parse that, what is the problem with the parsed query? – jpountz May 10 '12 at 15:18