Reputation: 35
While using complexphrase parser in Solr (4.10) I'm trying to run this query: ((a AND b) OR c) AND D.
The order is important, (A and B) or C should appear before D.
This is my query - {!complexphrase inOrder=true}title:"((a AND b) OR c) d"~1000
.
The problem is that Solr threat the first AND as OR, and returning all the documents which their title is (a OR b OR c) AND d
.
What can I do?
Upvotes: 2
Views: 1075
Reputation: 21
try this:
_query_:"{!complexphrase}title:\"A\"" AND _query_:"{!complexphrase}title:\"B\""
Upvotes: 1
Reputation: 33341
I don't believe there is support for AND
syntax in the complex phrase query parser. OR
s use a SpanOr
. There is no equivalent SpanAnd
in Lucene.
Even if a SpanAnd
did exist, I get the feeling it might mean something different than you expect. An OR is used to match one or the other at the same position. That is, (A OR B) C
matches "A C" and "B C". So, this theoretical SpanAnd (A AND B) C
would have to have both A and B at the same position, just before C. It is possible to have two terms occupying the same space in the index (with stemmers, synonym filters, etc.), but it's usually not particularly interesting to search for directly.
To achieve what you are looking for, you may need go with something like: "a b d"~1000 "c d"~1000
Expressing this query as you've written it is (roughly) possible through the Lucene SpanQuery API:
SpanQuery abQuery = new SpanNearQuery(new SpanQuery[] {
new SpanTermQuery(new Term("field", "a")),
new SpanTermQuery(new Term("field", "b"))
}, 1000, true);
SpanQuery cQuery = new SpanTermQuery(new Term("field", "c"));
SpanQuery abcQuery = new SpanOrQuery(new SpanQuery[] {
abQuery,
cQuery
});
Query finalQuery = new SpanNearQuery(new SpanQuery[] {
abcQuery,
new SpanTermQuery(new Term("field", "d"))
}, 1000, true);
TopDocs docs = searcher.search(finalQuery, 10);
I don't believe the ComplexPhrase parser is quite capable of expressing it, though. That involves nested SpanNears, which I don't believe that parser supports.
Upvotes: 2