Hello!

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>();
q.extractTerms(qTerms);
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 = it.next();
while (it.hasNext()) {
        mq.add(lastTerm);
        lastTerm = it.next();
}

// 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
                break;
        }
        if (t.field().equals(CONTENTS_FIELD)) {
                qTail.add(t);
        }
} while (termEnum.next());

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

// mq is now the query you need

Best regards,
Dmitry.

----- Original Message -----
From: "Ralf Heyde" <ralf.he...@gmx.de>
To: java-user@lucene.apache.org
Sent: Thursday, October 13, 2011 5:07:20 PM
Subject: Is it possible to combine Wildcard and Phrasequery for the Queryparser

Hello,

 

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
thrown.

 

Does anybody have an idea how to face this problem?

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

 

Regards

 

Ralf


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
For additional commands, e-mail: java-user-h...@lucene.apache.org

Reply via email to