Gang Li
2016-12-04 21:39:56 UTC
Hi everyone,
I'm trying to make the QueryParser parse a raw query without quotes into a
phrase query by default, and according to Lucene doc it seems I can use the
method setAutoGeneratePhraseQueries(). (
http://lucene.apache.org/core/6_2_0/queryparser/org/apache/lucene/queryparser/classic/QueryParserBase.html#setAutoGeneratePhraseQueries-boolean-
)
But after I call parser.setAutoGeneratePhraseQueries(True), the parser
still doesn't produce a phrase query. Please see the code example below.
I'm using Ubuntu 16.04, Java 1.8, Pylucene (Lucene version 6.2.0). All the
tests are passed by running "make test" under pylucene folder.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.queryparser.classic import QueryParser
from org.apache.lucene.search import PhraseQuery
from org.apache.lucene.index import Term
jcc_env = lucene.initVM(vmargs=[str('-Djava.awt.headless=true')])
# Parse raw query.
analyzer = StandardAnalyzer()
parser = QueryParser('field', analyzer)
# Auto generate phrase query over multiple terms.
parser.setAutoGeneratePhraseQueries(True)
# This prints field:term1 field:term2, but it should be field:"term1 term2"
print parser.parse('term1 term2')
# Build a phrase query.
builder = PhraseQuery.Builder()
builder.add(Term('field', 'term1'))
builder.add(Term('field', 'term2'))
# This prints field:"term1 term2", which is correct.
print builder.build()
Does anyone know how to make it work? Thank you!
Gang
I'm trying to make the QueryParser parse a raw query without quotes into a
phrase query by default, and according to Lucene doc it seems I can use the
method setAutoGeneratePhraseQueries(). (
http://lucene.apache.org/core/6_2_0/queryparser/org/apache/lucene/queryparser/classic/QueryParserBase.html#setAutoGeneratePhraseQueries-boolean-
)
But after I call parser.setAutoGeneratePhraseQueries(True), the parser
still doesn't produce a phrase query. Please see the code example below.
I'm using Ubuntu 16.04, Java 1.8, Pylucene (Lucene version 6.2.0). All the
tests are passed by running "make test" under pylucene folder.
import lucene
from org.apache.lucene.analysis.standard import StandardAnalyzer
from org.apache.lucene.queryparser.classic import QueryParser
from org.apache.lucene.search import PhraseQuery
from org.apache.lucene.index import Term
jcc_env = lucene.initVM(vmargs=[str('-Djava.awt.headless=true')])
# Parse raw query.
analyzer = StandardAnalyzer()
parser = QueryParser('field', analyzer)
# Auto generate phrase query over multiple terms.
parser.setAutoGeneratePhraseQueries(True)
# This prints field:term1 field:term2, but it should be field:"term1 term2"
print parser.parse('term1 term2')
# Build a phrase query.
builder = PhraseQuery.Builder()
builder.add(Term('field', 'term1'))
builder.add(Term('field', 'term2'))
# This prints field:"term1 term2", which is correct.
print builder.build()
Does anyone know how to make it work? Thank you!
Gang