views:

622

answers:

1
string q = "m";
Query query = new QueryParser("company", new StandardAnalyzer()).Parse(q+"*");

will result in query being a prefixQuery :company:a*

Still I will get results like "Fleet Africa" where it is rather obvious that the A is not at the start and thus gives me undesired results.

Query query = new TermQuery(new Term("company", q+"*"));

will result in query being a termQuery :company:a* and not returning any results. Probably because it interprets the query as an exact match and none of my values are the "a*" literal.

Query query = new WildcardQuery(new Term("company", q+"*"));

will return the same results as the prefixquery;

What am I doing wrong?

A: 

The short answer: all your queries do not constrain the search to the start of the field. You need an EdgeNGramTokenFilter or something like it. See this question for an implementation of autocomplete in Lucene.

Yuval F
Surely the example is too farfeched, right? Isn't it possible to create a startswith like query without all the fuzz?
borisCallens
Not that I know of. startswith is tricky. If you manage to do this, please let me know. From what I see, PrefixQuery means looking for the start of any term, not just the first.
Yuval F
This surprises me actually. Startswith must be the most easy query to do, not?
borisCallens