elasticsearch ELASTICERATION-内部命中中的过滤器值

tjrkku2a  于 2023-10-17  发布在  ElasticSearch
关注(0)|答案(2)|浏览(154)

在这个查询中,我想得到值为“data.addresses.ces.cp”的字段:“true”,从最近的文档中,然后它返回true值,比如false我怎么能只过滤true呢?

{ "_source": "created_at",
  "size": 1, 
  "sort": [
    {
      "created_at.keyword": {
        "order": "desc"
      }
    }
  ], 
  "query": {
    "nested": {
      "path": "data.addresses",
      "inner_hits": {
        "_source": ["data.addresses.ces.secondname", "data.addresses.ces.cp"],
        "size":100
      },
      "query": {
        "nested": {
          "path": "data.addresses.ces",
          "query": {
            "match": {
              "data.addresses.ces.cp": "true"
            }
          }
        }
      }
    }
  }

输出量:

"ces" : [
                  {
                    "secondname" : "lopez",
                    "cp" : true
                  },
                  {
                    "secondname" : "gomez",
                    "cp" : false          <--------------- error
                  },
                  {
                    "secondname" : "garcia",
                    "cp" : false         <---------------error
                  }
                ]
              }
            },
wvyml7n5

wvyml7n51#

这有点令人困惑,为什么你查询data.addresses.services.cp,但你想检索data.addresses.ces.cp。这个ces是什么这是一个服务打印错误吗?
在任何情况下,正如@瓦尔建议的那样,您可以将另一个inner_hits放入最内层的嵌套查询中:

{
  "_source": [
    "created_at"
  ],
  "size": 10,
  "query": {
    "nested": {
      "inner_hits": {
        "size": 100,
        "name": "ih_addresses"
      },
      "path": "data.addresses",
      "query": {
        "nested": {
          "inner_hits": {
            "name": "ih_services"
          },
          "path": "data.addresses.services",
          "query": {
            "match": {
              "data.addresses.services.cp": true
            }
          }
        }
      }
    }
  }
}

注意,inner_hits可以有一个name属性来区分它们。

sxpgvts3

sxpgvts32#

您需要将inner_hits移动到最里面的嵌套查询中:

{ "_source": "created_at",
  "size": 1, 
  "sort": [
    {
      "created_at.keyword": {
        "order": "desc"
      }
    }
  ], 
  "query": {
    "nested": {
      "path": "data.addresses",
      "query": {
        "nested": {
          "path": "data.addresses.ces",
          "inner_hits": {
            "_source": ["data.addresses.ces.secondname", "data.addresses.ces.cp"],
            "size":100
          },
          "query": {
            "match": {
              "data.addresses.ces.cp": "true"
            }
          }
        }
      }
    }
  }

相关问题