ElasticSearch filter by nested boolean type fields

和自甴很熟 提交于 2021-01-29 14:22:51

问题


I need to query on multiple nested fields on boolean types. Structure of mapping:

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

I am trying to use query both except1 and except2 only to False. Below my try, unfortunately it returns True and False for both fields and I cannot fix it.

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

回答1:


What you're probably looking for is the inner_hits option -- showing only the matched nested subdocuments.

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
              }
            }
          ]
        }
      }
    }
  }
}

then

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

yielding

{
  "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
                }
              }
            ]
          }
        }
      }
    }
  ]
}

Furthermore, you can force the system to only return the inner_hits by saying "_source": "inner_hits" on the top-level of your search query.



来源:https://stackoverflow.com/questions/61664063/elasticsearch-filter-by-nested-boolean-type-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!