elasticsearch 如何在嵌套文档中编写正确的查询?

irlmq6kh  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(159)

我有两个指数:国家和个人

PUT person 
{
  "mappings": {
    "properties": {
      "info": {
        "type": "nested",
        "properties": {
          "int_val": {"type": "integer"},
          "str_val": {"type": "text"},
          "field_id": {"type": "keyword"},
          "country_id": {"type": "keyword"}
        }
      }
    }
  }  
}

PUT country
{
  "mappings": {
    
    "properties": {
      "country_ids": {"type": "keyword"}
    }
  }
}

PUT country/_doc/user1
{
  "country_ids": ["111", "222", "333"]
}

PUT person/_doc/1
{
  "info": [
    {
      "field_id": "1000",
      "str_val": "Jack Kotlin",
      "country_id": "444"
    },
    {
      "field_id": "1000",
      "str_val": "Jack Martin",
      "country_id": "333"
    },
    {
      "field_id": "1001",
      "str_val": "Jack",
      "country_id": "111"
    },
    {
      "field_id": "2000",
      "int_val": 30,
      "country_id": "444"
    },
    {
      "field_id": "2000",
      "int_val": 30,
      "country_id": "333"
    },
    {
      "field_id": "2001",
      "int_val": 30,
      "country_id": "111"
    }
  ]
}

如果user 1查询'(字段标识=1000,str_瓦尔=“Jack”)&(字段标识=2000,int_val=“Jack”)',则必须返回以下结果:

{
  "info": [
    {
      "field_id": "1000",
      "str_val": "Jack Martin",
      "country_id": "333"
    },
    {
      "field_id": "2000",
      "int_val": 30,
      "country_id": "333"
    }
  ]
}

救救我!
我写了一个对单部分的查询:(字段标识=1000,字符串瓦尔=“Jack”)

GET person/_search
{
  "query": {
    "nested": {
      "path": "info",
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "info.country_id": {
                  "index": "country",
                  "id": "user1",
                  "path": "country_ids"
                }
              }
            }
          ],
          "must": [
            {"match": {"info.field_id": "1000"}},
            {"match": {"info.str_val": "Jack"}}
          ]
        }
      },
      "inner_hits": {}
    }
  },
  "_source": false
}

得到了正确的结果:

{
  "took" : 5,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 1.1530519,
    "hits" : [
      {
        "_index" : "person",
        "_id" : "1",
        "_score" : 1.1530519,
        "inner_hits" : {
          "info" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.1530519,
              "hits" : [
                {
                  "_index" : "person",
                  "_id" : "1",
                  "_nested" : {
                    "field" : "info",
                    "offset" : 1
                  },
                  "_score" : 1.1530519,
                  "_source" : {
                    "field_id" : "1000",
                    "str_val" : "Jack Martin",
                    "country_id" : "333"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

但我不知道如何编写多部件查询!

a0x5cqrl

a0x5cqrl1#

以下查询将显示:
具有(标识=1000 AND str_field=Jack)AND(标识=2000 AND int_field=30)的文档

GET person/_search
{
  "query": {
    "nested": {
      "path": "info",
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "info.country_id": {
                  "index": "country",
                  "id": "user1",
                  "path": "country_ids"
                }
              }
            },
            {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "info.field_id": "2000"
                          }
                        },
                        {
                          "match": {
                            "info.int_val": 30
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "info.field_id": "1000"
                          }
                        },
                        {
                          "match": {
                            "info.str_val": "Jack"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      "inner_hits": {}
    }
  },
  "_source": false
}

相关问题