0

我需要查询布尔类型的多个嵌套字段。映射结构:

    "mappings" : {
      "properties" : {
        "leaders" : {
          "type" : "nested",
          "properties" : {
            "except_1" : {
              "type" : "boolean"
            },
            "except_2" : {
              "type" : "boolean"
            },
            "counter" : {
              "type" : "integer"
            }
          }
        }
      }
    }

我正在尝试同时使用查询,except1并且except2仅用于False. 在我的尝试下,不幸的是它返回TrueFalse两个字段,我无法修复它。

    "query": {
        "nested": {
            "path": "leaders",
            "query": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "leaders.except_1": False
                            }
                        },
                        {
                            "term": {
                                "leaders.except_2": False
                            }
                        }
                    ]
                }
            }
        }
    }
4

1 回答 1

2

您可能正在寻找的是inner_hits选项 - 仅显示匹配的嵌套子文档。

PUT leaders
{"mappings":{"properties":{"leaders":{"type":"nested","properties":{"except_1":{"type":"boolean"},"except_2":{"type":"boolean"},"counter":{"type":"integer"}}}}}}

POST leaders/_doc
{
  "leaders": [
    {
      "except_1": true,
      "except_2": false
    },
    {
      "except_1": false,
      "except_2": false
    }
  ]
}

GET leaders/_search
{
  "query": {
    "nested": {
      "path": "leaders",
      "inner_hits": {}, 
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "leaders.except_1": false
              }
            },
            {
              "term": {
                "leaders.except_2": false
              }
            }
          ]
        }
      }
    }
  }
}

然后

GET leaders/_search
{
  "query": {
    "nested": {
      "path": "leaders",
      "inner_hits": {}, 
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "leaders.except_1": false
              }
            },
            {
              "term": {
                "leaders.except_2": false
              }
            }
          ]
        }
      }
    }
  }
}

屈服

{
  "hits":[
    {
      "_index":"leaders",
      "_type":"_doc",
      "_id":"u-he8HEBG_KW3EFn-gMz",
      "_score":0.87546873,
      "_source":{                           <-- default behavior
        "leaders":[
          {
            "except_1":true,
            "except_2":false
          },
          {
            "except_1":false,
            "except_2":false
          }
        ]
      },
      "inner_hits":{
        "leaders":{
          "hits":{
            "total":{
              "value":1,
              "relation":"eq"
            },
            "max_score":0.87546873,
            "hits":[                              <------- only the matching nested subdocument
              {
                "_index":"leaders",
                "_type":"_doc",
                "_id":"u-he8HEBG_KW3EFn-gMz",
                "_nested":{
                  "field":"leaders",
                  "offset":1
                },
                "_score":0.87546873,
                "_source":{
                  "except_1":false,
                  "except_2":false
                }
              }
            ]
          }
        }
      }
    }
  ]
}

此外,您可以强制系统仅在搜索查询的顶层inner_hits通过说返回。"_source": "inner_hits"

于 2020-05-07T18:25:07.550 回答