我已经将数据映射到这个模式:
curl -X PUT "localhost:9200/data?pretty" -H 'Content-Type: application/json' -d
'{
"settings":{
"number_of_shards":"1",
"number_of_replicas":"1"
},
"mappings":{
"properties":{
"routines":{
"type":"nested",
"properties":{
"title":{
"type":"text"
},
"sources":{
"type":"keyword"
},
"flags":{
"type":"keyword",
"null_value":"NULL"
},
"steps":{
"type":"nested",
"properties":{
"time":{
"type":"keyword"
},
"products":{
"type":"nested",
"properties":{
"name":{
"type":"text"
},
"link":{
"type":"keyword",
"null_value":"NULL"
},
"type":{
"type":"keyword",
"null_value":"NULL"
},
"ingredients":{
"type":"keyword",
"null_value":"NULL"
},
"flags":{
"type":"keyword",
"null_value":"NULL"
}
}
}
}
}
}
}
}
}
}'
现在,我试图用这个查询搜索两个字段data.title
和data.steps.products.name
:
curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "data",
"query": {
"nested": {
"path": "steps",
"query": {
"nested": {
"path": "products",
"query": {
"multi_match": {
"query": "XXX",
"fields": [
"name"
]
}
}
}
}
}
}
}
},
{"multi_match": {
"query": "XXX",
"fields": [
"data.title"
]
}
}
]
}
}
}'
在
步骤下查找路径失败,抛出错误{
"error" : {
"root_cause" : [
{
"type" : "query_shard_exception",
"reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
"index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
"index" : "data"
}
],
"type" : "search_phase_execution_exception",
"reason" : "all shards failed",
"phase" : "query",
"grouped" : true,
"failed_shards" : [
{
"shard" : 0,
"index" : "data",
"node" : "L1azdi09QxanYGnP-y0xbQ",
"reason" : {
"type" : "query_shard_exception",
"reason" : "failed to create query: [nested] failed to find nested object under path [steps]",
"index_uuid" : "SjQgt4BHStC_APsMnZk8BQ",
"index" : "data",
"caused_by" : {
"type" : "illegal_state_exception",
"reason" : "[nested] failed to find nested object under path [steps]"
}
}
}
]
},
"status" : 400
}
你能帮助找到映射/查询的错误吗?
更新:
我的json数据:https://jsonkeeper.com/b/PSVS
查看Elastic文档中的多级嵌套查询。
您在查询中忘记的是每个子级嵌套查询中嵌套对象的完整路径。(另外,您使用了映射中不存在的data
字段,而您想要使用routines
代替)
所以你的查询看起来像
curl -X GET "localhost:9200/data/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{
"nested": {
"path": "routines",
"query": {
"nested": {
"path": "routines.steps",
"query": {
"nested": {
"path": "routines.steps.products",
"query": {
"multi_match": {
"query": "XXX",
"fields": [
"routines.steps.products.name"
]
}
}
}
}
}
}
}
},
{"multi_match": {
"query": "XXX",
"fields": [
"routines.title"
]
}
}
]
}
}
}'
无论如何,首先请重新考虑多层嵌套字段是否是个好主意,因为它们对性能有重大影响。例如,将routines
索引,也许与data
标识符,有意义吗?
编辑:为第一个应该块的多匹配字段添加完整路径