带有嵌套对象的弹性搜索嵌套查询
Elastic Search Nested Query with Nested Object
这是我在弹性搜索中存储在索引中的数据类型。
我必须找到主要成分牛肉(重量小于 1000)的食谱,配料 -(辣椒粉,重量小于 250),(橄榄油和重量小于 300),所有其他成分也类似。
"Name": "Real beef burritos",
"Ingredients": [
{"name": "olive oil",
"id": 27,
"weight": 200},
{"name": "bonion","id": 3,"weight": 300},
{"name": "garlic",
"id": 2,
"weight": 100
},
{"name": "chilli powder",
"id": 35,
"weight": 150},
{"name": "coriander",
"id": 40,
"weight": 600},
{"name": "tortillas",
"id": 41,
"weight": 700}
],"Main_ingredient": {
"type": "Beef",
"id": 101,
"weight": 1000
}}}
索引的映射是
{"final":{"mappings":{"superb":{"properties":{"Cook Time":{"type":"long"},"Ingredients":{"type":"nested","properties":{"id":{"type":"short"},"name":{"type":"string"},"type":{"type":"string"},"weight":{"type":"short"}}},"Main_ingredient":{"properties":{"id":{"type":"long"},"type":{"type":"string"},"weight":{"type":"long"}}},"Name":{"type":"string"},"Prep Time":{"type":"long"},"Servings":{"type":"long"},"Tags":{"type":"string"},"Urls":{"type":"string"},"Views":{"type":"long"}}}}}}
我的查询是
{
"query": {
"bool": {
"must": [
{ "match": { "Main_ingredient.type": "Beef" }},
{"range":{"Main_ingredient.weight":{"lte":1000}}},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{ "match": { "Ingredients.name": "garlic" }},
{ "range": { "Ingredients.weight":{"lte":400} }},
{ "match": { "Ingredients.name": "chilli powder" }},
{ "range": { "Ingredients.weight":{"lte":400} }}
]
}}}}
]
}}}
但是 Null.Can 任何人都可以帮助我。我想我没有正确使用嵌套查询
试试这个:
{
"query": {
"bool": {
"must": [
{
"match": {
"Main_ingredient.type": "Beef"
}
},
{
"range": {
"Main_ingredient.weight": {
"lte": 1000
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "garlic"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "chilli powder"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
}
]
}
}
}
这是我在弹性搜索中存储在索引中的数据类型。 我必须找到主要成分牛肉(重量小于 1000)的食谱,配料 -(辣椒粉,重量小于 250),(橄榄油和重量小于 300),所有其他成分也类似。
"Name": "Real beef burritos",
"Ingredients": [
{"name": "olive oil",
"id": 27,
"weight": 200},
{"name": "bonion","id": 3,"weight": 300},
{"name": "garlic",
"id": 2,
"weight": 100
},
{"name": "chilli powder",
"id": 35,
"weight": 150},
{"name": "coriander",
"id": 40,
"weight": 600},
{"name": "tortillas",
"id": 41,
"weight": 700}
],"Main_ingredient": {
"type": "Beef",
"id": 101,
"weight": 1000
}}}
索引的映射是
{"final":{"mappings":{"superb":{"properties":{"Cook Time":{"type":"long"},"Ingredients":{"type":"nested","properties":{"id":{"type":"short"},"name":{"type":"string"},"type":{"type":"string"},"weight":{"type":"short"}}},"Main_ingredient":{"properties":{"id":{"type":"long"},"type":{"type":"string"},"weight":{"type":"long"}}},"Name":{"type":"string"},"Prep Time":{"type":"long"},"Servings":{"type":"long"},"Tags":{"type":"string"},"Urls":{"type":"string"},"Views":{"type":"long"}}}}}}
我的查询是
{
"query": {
"bool": {
"must": [
{ "match": { "Main_ingredient.type": "Beef" }},
{"range":{"Main_ingredient.weight":{"lte":1000}}},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{ "match": { "Ingredients.name": "garlic" }},
{ "range": { "Ingredients.weight":{"lte":400} }},
{ "match": { "Ingredients.name": "chilli powder" }},
{ "range": { "Ingredients.weight":{"lte":400} }}
]
}}}}
]
}}}
但是 Null.Can 任何人都可以帮助我。我想我没有正确使用嵌套查询
试试这个:
{
"query": {
"bool": {
"must": [
{
"match": {
"Main_ingredient.type": "Beef"
}
},
{
"range": {
"Main_ingredient.weight": {
"lte": 1000
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "garlic"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
},
{
"nested": {
"path": "Ingredients",
"query": {
"bool": {
"must": [
{
"match": {
"Ingredients.name": "chilli powder"
}
},
{
"range": {
"Ingredients.weight": {
"lte": 400
}
}
}
]
}
}
}
}
]
}
}
}