1
votes

I'm trying to change the default operator for a MultiFieldQueryParser:

fields = [...]
query = "hello stackoverflow"
clauses = [BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD, ...]

parser = MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer)
parser.setDefaultOperator(QueryParserBase.AND_OPERATOR)
query = parser.parse(Version.LUCENE_CURRENT, query, fields, clauses, analyzer)

The resulting query is:

(FieldA:hello FieldA:stackoverflow) (FieldB:hello FieldB:stackoverflow)

How can I get an AND query to retrieve only these documents that contain BOTH words (hello AND stackoverflow) in one or more of the available fields (fieldA, fieldB)?

Thanks! - PyLucene 4.8.0, Python 2.7 64 bit

2

2 Answers

1
votes

Those MultiFieldQueryParser.parse methods that take a bunch of arguments are all static. The query parser instance and anything you did to it might as well not be there at all, what you have is equivalent to:

query = MultiFieldQueryParser.parse(Version.LUCENE_CURRENT, query, fields, clauses, analyzer)

The parse you want, when using a instance of the query parser, is the one that just takes a string.

fields = ["FieldA", "FieldB"]
query = "hello stackoverflow"

parser = MultiFieldQueryParser(Version.LUCENE_CURRENT, fields, analyzer)
parser.setDefaultOperator(QueryParserBase.AND_OPERATOR)
query = parser.parse(query)
0
votes
searcher = IndexSearcher(reader)
analyzer = WhitespaceAnalyzer(Version.LATEST)
fields = ('a', 'b', 'c')
parser = MultiFieldQueryParser(fields, analyzer)
query = parser.parser(query)
results = searcher.search(query,10)