I'm just getting started with ES and its Java API. As a proof-of-concept I
implemented a generic search method that takes user-inputted text and does
prefix matching against various fields:
List searchFoos(@Nullable String phrase) {
final QueryBuilder queryBuilder;
if (phrase != null && !phrase.isEmpty()) {
phrase = phrase.trim().toLowerCase();
final BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
for (final String term : phrase.split("\\s+")) {
boolQueryBuilder
.should(QueryBuilders.prefixQuery("name", term))
.should(QueryBuilders.prefixQuery("slogan", term))
.should(QueryBuilders.prefixQuery("affiliation", term))
.should(QueryBuilders.prefixQuery("tags", term));
}
queryBuilder = boolQueryBuilder;
}
else {
queryBuilder = QueryBuilders.matchAllQuery();
}
//etc.
}
This is obviously a pretty naive implementation. I'd like to improve it
with respect to two overlapping concerns:
- I'd like to use some kind of multi-match on the various fields instead
of tacking prefix queries onto the bool one after the other. I know there
is multi_matchhttp://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query/
and match_phrase_prefixhttp://www.elasticsearch.org/guide/reference/query-dsl/match-query/,
but I couldn't find any documentation or examples of a multi_
match_phrase_prefix. - I'd like to avoid manually splitting on whitespace and attaching
queries for each individual token. match_phrase_prefix seems to offer a
solution here again, but it wasn't clear to me how similar it is to
regular prefixhttp://www.elasticsearch.org/guide/reference/query-dsl/prefix-query/.
I do understand that match_phrase_prefix only "allows for prefix matches
on the last term in the text" - so this would be different behavior than
what I have currently, which allows for prefix matches on any term in the
text.
My questions are:
- How can I best improve the above implementation while keeping the same
behavior? - If there is indeed something like multi_match_phrase_prefix, where is
its documentation, or else helpful examples? - As I noted, match_phrase_prefix will behave differently from a prefix
query for each term of the phrase - is that a more typical implementation
for the use case of a search bar for a user? This is a really common use
case so I'm wondering what queries others have used and why.
Paul
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.