Lucene QueryParser - produce a “portable” query to

2019-08-25 00:57发布

问题:

Following on from my earlier question:

Manipulate Lucene query before performing search

I've run into a problem where I'm wanting to send a QueryParser parsed Query (Query.toString()) onto a webservice that uses SOLR with a default operator of AND. Because Lucene has OR as its default operator any OR'ed terms are left "as is". E.g. given the query:

(f1:cat OR f1:dog) AND f2:cow AND f3:"tree frog"

once parsed (with QuerParser.setDefaultOperator(QueryParser.Operator.AND)), the String version becomes:

+(f1:cat f1:dog) +f2:cow +f3:"tree frog"

When this is passed to SOLR, with an assumed AND operator, the two f1 terms get AND'ed instead of OR'ed.

The only work-around I can think of is to change my SOLR installation to use OR as its default Boolean operator... Any other suggestions?

回答1:

The default operator can be overriden on a per-request basis. For exemple :

Using params: http://solr/select?q=query&q.op=OR

Or using local params: http://solr/select?q={!lucene q.op=OR}query

See SolrQuerySyntax for more details.



标签: solr lucene