elasticsearch 在ElasticSearch中筛选嵌套字段,elasticsearch,elasticsearch" /> elasticsearch 在ElasticSearch中筛选嵌套字段,elasticsearch,elasticsearch" />

elasticsearch 在ElasticSearch中筛选嵌套字段

elasticsearch 在ElasticSearch中筛选嵌套字段,elasticsearch,elasticsearch,我正在尝试过滤ElasticSearch 7.11上的一些文档 我的索引具有以下映射: { "properties": { "recArrNested": { "type": "nested", "properties": { "nBTxt": { "type": "keywo

我正在尝试过滤ElasticSearch 7.11上的一些文档

我的索引具有以下映射:

{
  "properties": {
    "recArrNested": {
      "type": "nested",
      "properties": {
        "nBTxt": {
          "type": "keyword"
        },
        "nBInt": {
          "type": "long"
        }
      }
    },
    "recNested": {
      "type": "object",
      "properties": {
        "nAInt": {
          "type": "long"
        },
        "nATxt": {
          "type": "keyword"
        }
      }
    },
    "recId": {
      "type": "keyword"
    }
  }
}
我有这样的记录:

{
  "recArrNested": [
    {
      "nBTxt": "juliette",
      "nBInt": 10
    },
    {
      "nBTxt": "alpha",
      "nBInt": 42
    },
    {
      "nBTxt": "kilo",
      "nBInt": 11
    }
  ],
  "recNested": {
    "nAInt": 1,
    "nATxt": "manual"
  },
  "recId": "1alpha"
}
我的目标是过滤那些
recArrNested.nBTxt
等于其
recNested.nAInt
NATO对应的语音字母表(alpha->1、bravo->2等)的记录

我已生成以下查询:

{
  "size": 5,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "1"
                        }
                      }
                    },
                    {
                      "term": {
                        "recArrNested.nBTxt": {
                          "value": "alpha"
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "2"
                        }
                      }
                    },
                    {
                      "term": {
                        "recArrNested.nBTxt": {
                          "value": "bravo"
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "recId": {
        "order": "desc"
      }
    }
  ],
  "track_scores": false
}
遗憾的是,上面的示例文档不匹配。
你有什么建议来正确处理这个查询吗?

我看不出有什么理由将
recNested
声明为
nested
类型——只需保持
recarrneted
实际上
nested
就足够了,因为你处理的是

根据您当前的映射,您希望在任何适用的情况下使用:

{
  "size": 5,
  "from": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "1"
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "recArrNested",        <--
                        "query": {
                          "term": {
                            "recArrNested.nBTxt": {
                              "value": "alpha"
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "recNested.nAInt": {
                          "value": "2"
                        }
                      }
                    },
                    {
                      "nested": {
                        "path": "recArrNested",        <--
                        "query": {
                          "term": {
                            "recArrNested.nBTxt": {
                              "value": "bravo"
                            }
                          }
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  },
  "sort": [
    {
      "recId": {
        "order": "desc"
      }
    }
  ],
  "track_scores": false
}
{
“尺寸”:5,
“from”:0,
“查询”:{
“布尔”:{
“过滤器”:[
{
“布尔”:{
“应该”:[
{
“布尔”:{
“必须”:[
{
“期限”:{
“recNested.nAInt”:{
“值”:“1”
}
}
},
{
“嵌套”:{

“路径”:“重新注册”,我刚刚接收了您的示例文档,运行了查询并匹配了文档。您是否使用了任何特殊映射?为了使其正常工作,您可能需要在索引映射中创建类型为
nested
recNested
ange(即使使用嵌套的
而不是
对象
)。