2

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

Peter Clause
  • 1,132
  • 9
  • 22

2 Answers2

1

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)
femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • 1
    PyLucene's method binding seems to be broken, I got an error saying "descriptor parse requires a QueryParserBase object but received a str". When I call it like query = MultiFieldQueryParser.parse(parser, query), it works like a charme. Thank you! – Peter Clause Nov 11 '14 at 08:34
  • The getMultiFieldQuery method on line 284 of MultiFieldQueryParser (lucene version 8.1.1) looks fishy. I had to switch the BooleanClause.Occur from SHOULD to MUST to get AND behavior. – Ryan R. Jun 20 '19 at 02:38
  • same here, your way worked for me too, thank you @PeterClause – yi-ji Jul 20 '23 at 18:30
0
searcher = IndexSearcher(reader)
analyzer = WhitespaceAnalyzer(Version.LATEST)
fields = ('a', 'b', 'c')
parser = MultiFieldQueryParser(fields, analyzer)
query = parser.parser(query)
results = searcher.search(query,10)