lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
Subject Re: Did you mean...
Date Thu, 12 Feb 2004 08:50:30 GMT

Hi Timo!

There is no built-in way in Lucene to achieve this. I have done a simple implementation with
a patched FuzzyQuery for each term. A new method (bestOrderRewrite) returns a ordered list
of all fuzzy terms that indeed exist in index. There is no guarantee that the suggested term
is spelled correct though...  Basically this works best when search is done on one term only.
Search code is something like this:

// on no_hits
cquery = cquery.rewrite(reader);
if (cquery instanceof TermQuery) {
    // if search contained only one term this is a TermQuery instance
    FuzzyQuery fquery = new FuzzyQuery(new Term("contents", cquery.toString("contents")));
    TermQuery[] terms = fquery.bestOrderRewrite(reader);
    if (terms.length > 0) {
        StringBuffer alts = new StringBuffer();
        alts.append("Did you mean ").append(terms[0].getTerm().text());
} else if (cquery instanceof BooleanQuery) {
    // split queries
    // <snip>
    BooleanClause[] clauses = ((BooleanQuery)cquery).getClauses();
    // get suggestion for each term
    if (clauses[i].required) {
       FuzzyQuery fquery = new FuzzyQuery(new Term("contents", clauses[i].query.toString("contents")));
       TermQuery[] terms = fquery.bestOrderRewrite(reader);
        // ...
    // </snip>
} // and so on...


>On Thursday 12 February 2004 00:15, Matt Tucker wrote:
>> We implemented that type of system using a spelling engine by Wintertree:
>> There are some free Java spelling packages out there too that you could
>> likely use.
>But this does not ensure that the word really exists in the index. The word 
>google does propose however to exist.
>To unsubscribe, e-mail:
>For additional commands, e-mail:

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

View raw message