我试图更改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)
生成的查询是:
(FieldA:hello FieldA:stackoverflow) (FieldB:hello FieldB:stackoverflow)
如何在一个或多个可用字段(fieldA,fieldB)中获取AND查询以仅检索包含两个单词(hello和stackoverflow)的文档?
谢谢! - PyLucene 4.8.0,Python 2.7 64位
答案 0 :(得分:1)
那些带有大量参数的MultiFieldQueryParser.parse
方法都是 static 。查询解析器实例以及您对它所做的任何事情都可能根本不存在,您所拥有的相当于:
query = MultiFieldQueryParser.parse(Version.LUCENE_CURRENT, query, fields, clauses, analyzer)
使用查询解析器的实例时,您想要的parse
是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)
答案 1 :(得分: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)