Sorry for this noob question, but ElasticSearch queries can be confusing...
Goal: Create a query that works like Google's search queries. The more words you enter the fewer matching results you get. For example "szechuan sauce" yields more results than "mulan szechuan sauce". My index has a type for blog posts, which has the fields "title", "tags", "categories". ElasticSearch must find every document which matches with all words from the query. The words can be spread over all fields. For example if a document's title contains "mulan", its tags contain "szechuan" and its categories contain "sauce", then this should be a match, but not if one word is missing.
I tried different bool queries, but even when I use "must" then the result contains documents which do not match with all query words.
For example I tried:
String queryString = "mulan szechuan sauce";
QueryBuilder titleMatchQuery = QueryBuilders.matchQuery("title", queryString);
QueryBuilder tagsMatchQuery = QueryBuilders.matchQuery("tags", queryString);
QueryBuilder categoryMatchQuery = QueryBuilders.matchQuery("categories", queryString);
QueryBuilder combinedBoolQuery = QueryBuilders.boolQuery()
.must(titleMatchQuery)
.must(tagsMatchQuery)
.must(categoryMatchQuery);
This compiles to:
{
"bool" : {
"must" : [ {
"match" : {
"title" : {
"query" : "mulan szechuan sauce",
"type" : "boolean"
}
}
}, {
"match" : {
"tags" : {
"query" : "mulan szechuan sauce",
"type" : "boolean"
}
}
}, {
"match" : {
"categories" : {
"query" : "mulan szechuan sauce",
"type" : "boolean"
}
}
} ]
}
}
As said, this also yields results which to not contain all words from the query :-/