Using Elasticsearch, I'm trying to search nested data and return the documents with the most "hits."
Relevant Example Data
POST myrecipes/recipe
{
"title": "Potato Salad",
"ingredients": [
{"name":"potato"},
{"name":"mayonaise"},
{"name":"mustard"},
{"name":"bacon"},
{"name":"onion"},
{"name":"parsley"}
]
}
POST myrecipes/recipe
{
"title": "Seared Scallops",
"ingredients": [
{"name":"scallops"},
{"name":"butter"},
{"name":"bacon"}
]
}
POST myrecipes/recipe
{
"title": "Tuna melt",
"ingredients": [
{"name":"tuna"},
{"name":"onion"},
{"name":"butter"},
{"name":"bacon"},
{"name":"mayonaise"},
{"name":"lettuce"},
{"name":"tomato"},
{"name":"bread"}
]
}
My Index
PUT myrecipes
{
"mappings": {
"recipe": {
"properties": {
"ingredients": {
"type": "nested",
"include_in_parent": true,
"properties": {
"name": {
"type": "string",
"fields": {
"untouched": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
My Search:
POST myrecipes/recipe/_search
{
"query": {
"nested": {
"path": "ingredients",
"query": {
"bool": {
"should": [
{
"match": {
"ingredients.name": {
"query": "bacon"
}
}
},
{
"match": {
"ingredients.name": {
"query": "butter"
}
}
}
]
}
}
}
}
}
The query returns
"Tuna melt", "Potato Salad", "Seared Scallops"
I'm hoping for:
"Seared Scallops", "Tuna melt", "Potato Salad"
Since there are two "hits" on "Seared scallops" and "Tuna Melt" (bacon and butter), and only one "hit" on "Potato Salad" (bacon).