Reputation: 29557
How do I select for the following documents:
"ice cream" AND chocolate (in title or content field)
If the words "ice" and "cream" appear, but not as "ice cream" then the document shouldn't be returned.
Coming from MySQL with its nice parenthesis, I'm not finding this intuitive.
"must": [
{
"multi_match": {
"query": "'ice cream' chocolate",
"fields": [
"title",
"content"
]
},
}
],
Example
Document 1:
Document 2:
Document 3:
It should find Document 1, but not Document 2 or 3.
Upvotes: 1
Views: 182
Reputation: 32376
Here you go, I used same sample docs and created sample minimum sample mapping to reproduce and try your issue.
I used nested bool query with match phrase query to solve your issue as your require ice cream
as a phrase, otherwise it would hv been very easy problem.
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"content": {
"type": "text"
}
}
}
}
And search query
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match_phrase": {
"title": "ice cream"
}
},
{
"match_phrase": {
"content": "ice cream"
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"title": "chocolate"
}
},
{
"match": {
"content": "chocolate"
}
}
]
}
}
]
}
}
}
This fetches first doc only as shown below:
"hits": [
{
"_index": "phrasematch",
"_type": "_doc",
"_id": "1",
"_score": 2.3210695,
"_source": {
"title": "Ice cream",
"content": "Great with chocolate"
}
}
]
Upvotes: 1