lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Dmitry Savenko <>
Subject Re: Is it possible to combine Wildcard and Phrasequery for the Queryparser
Date Thu, 13 Oct 2011 10:23:57 GMT

Here is a solution I come up with. Heavy code, has limitations, but works for my case. 

// parsing query
QueryParser parser = new QueryParser(LUCENE_VERSION, CONTENTS_FIELD, analyzer);
Query q = parser.parse("\"" + QueryParser.escape(phraseText) + "\"");
q = q.rewrite(searcher.getIndexReader());

// getting terms (using LinkedHashSet to preserve the order)
Set<Term> qTerms = new LinkedHashSet<Term>();
if (qTerms.isEmpty()) {
	// empty query
	return ret;

// putting each term except for the last one to multi-phrase query
MultiPhraseQuery mq = new MultiPhraseQuery();
Iterator<Term> it = qTerms.iterator();
Term lastTerm =;
while (it.hasNext()) {
	lastTerm =;

// searching for terms which start with the last term
termEnum = searcher.getIndexReader().terms(lastTerm);
List<Term> qTail = new ArrayList<Term>();
do {
	Term t = termEnum.term();
	if (!t.text().startsWith(lastTerm.text())) {
		// no need to go further because of terms sorting
	if (t.field().equals(CONTENTS_FIELD)) {
} while (;

// adding last term variations
mq.add(qTail.toArray(new Term[] {}));

// mq is now the query you need

Best regards,

----- Original Message -----
From: "Ralf Heyde" <>
Sent: Thursday, October 13, 2011 5:07:20 PM
Subject: Is it possible to combine Wildcard and Phrasequery for the Queryparser



i'm trying to search the following phase:


I'm searching all occurrences of:

.         "The Right Way"

.         "The Right Ways"


Possible solutions could be something like this - combining a phrase &
wildcard search:


.         title:"The Right Way*"

.         title:"The Right Way"*


But these approaches don't work using the QueryParser - a ParseException is


Does anybody have an idea how to face this problem?

Is there a general (another) solution for that problem?





To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message