0

I'm trying to create auto suggestion based on Lucene full text index. The main issue is how to create autosuggestion(autocomplete) based on multiterm phrases, for example -

nosql dat*

results can be

nosql database
nosql data

but not

perfect nosql database

What is the correct syntax for Lucene query in order to create auto suggestion based on the first words in a multi term query with a wildcard at the end ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • 2
    there is a workaround described at http://blogs.perl.org/users/mark_leighton_fisher/2012/01/stupid-lucene-tricks-exact-match-starts-with-ends-with.html - that should work as well for a manual index in Neo4j doing fulltext. – Stefan Armbruster Mar 15 '15 at 14:14

1 Answers1

0

I had a similar requirement, Lucene has Span queries that allow you to use location of words in the text in queries.

I've implemented it in Lucene using FirstSpanQuery. (read about it in the docs)

here I use SpanNearQuery to force all the words to be next to each other and SpanFirstQuery to force all of them to be in the start of the text.

    if (querystr.contains(" ")) // more than one word?
    {
        String[] words = querystr.split(" ");           
        SpanQuery[] clausesWildCard = new SpanQuery[words.length];          
        for (int i = 0; i < words.length; i++) {                
            if (i == words.length - 1) //last word, add wildcard clause
            {
                PrefixQuery pq = new PrefixQuery(new Term(VALUE, words[i])); 
                clausesWildCard[i] = new SpanMultiTermQueryWrapper<PrefixQuery>(pq);
            }
            else
            {
                Term clause = new Term(VALUE, words[i]); 
                clausesWildCard[i] = new SpanTermQuery(clause);
            }               
        }

        SpanQuery allTheWordsNear = new SpanNearQuery(clausesWildCard, 0, true);
        prefixquery = new SpanFirstQuery(allTheWordsNear, words.length);
    }
Yossi Vainshtein
  • 3,845
  • 4
  • 23
  • 39