This doesn't work.
Query query = new WildcardQuery(new Term("*", term));
If you want to search across all fields, you'll either need to create a Term for each field you need to search, and combine them (a DisjunctionMaxQuery
is usually best for that):
Query query = new DisjunctionMaxQuery(0);
query.add(new TermQuery(new Term("field1", term)));
query.add(new TermQuery(new Term("field2", term)));
query.add(new TermQuery(new Term("field3", term)));
MultiFieldQueryParser
is another good tool for this.
Alternatively, a useful pattern is to add all searchable fields into one unstored catch-all field at index time, and when you want to match against anything in the index, you could just query that field instead of enumerate subqueries on all searchable fields.
Also, it looks like you want to add the wildcard to the query here, but you haven't done so. WildcardQuery
won't assume any wildcards that aren't there in the term.
Query query = new WildcardQuery(new Term("field", term + "*"));
Since you just need a trailing wildcard, though, I would just use PrefixQuery
, which is the standard way of handling a trailing wildcard.
Query query = new PrefixQuery(new Term("field", term));