elasticsearch 如何在Elastic Search Query中搜索嵌套数组中的特定值?

xu3bshqb  于 2023-06-21  发布在  ElasticSearch
关注(0)|答案(1)|浏览(122)

我正在搜索以下索引:

{
    "_index": "example-index",
    "_type": "_doc",
    "_id": "C-123",
    "_version": 1685532751615,
    "_seq_no": 123,
    "_primary_term": 16,
    "found": true,
    "_source": {
        "definitionKey": "example.definition",
        "definitionName": "example",
        "startTime": "2023-05-31T11:32:31.526Z",
        "id": "C-123",
        "state": "active",
        "variables": [
            {
                "id": "VAR-d4a01b91-ffa6-11ed-83be-560acee90493",
                "name": "skills",
                "jsonValue": [
                    "Skill 1"
                ]
            }
        ],
        "definitionId": "C-456",
        "tenantId": ""
    }
}

我通过检查wether来查找此索引:
1.“definitionKey”等于“example.definition”
1.“variables”数组的条目有一个字段“name”,它与“skills”完全匹配

  1. 2的入口。有一个“jsonValue”数组,其中至少包含一个与“Skill1”完全匹配的条目
    前两个工作完美,但3。好像不能正常工作。我没有得到一个异常,但索引也没有显示在结果中。
    如何检查“jsonValue”是否包含值为“Skill 1”的条目?
    我尝试的查询:
{
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "definitionKey": "example.definition"
                    }
                },
                {
                    "nested": {
                        "path": "variables",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "variables.name": "skills"
                                        }
                                    },
                                    {
                                        "nested": {
                                            "path": "variables",
                                            "query": {
                                                "terms": {
                                                    "variables.jsonValue.keyword": [
                                                        "Skill 1"
                                                    ]
                                                }
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    }
}
oxcyiej7

oxcyiej71#

分析索引文档,只有变量字段嵌套类型,jsonValue字段没有嵌套。
这样就不需要对齐嵌套查询。请参见下面的示例。
Map

PUT idx_test
{
  "mappings": {
    "properties": {
      "variables": {
        "type": "nested",
        "properties": {
          "jsonValue": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

数据

POST idx_test/_doc
    {
      "definitionKey": "example.definition",
      "definitionName": "example",
      "startTime": "2023-05-31T11:32:31.526Z",
      "id": "C-123",
      "state": "active",
      "variables": [
        {
          "id": "VAR-d4a01b91-ffa6-11ed-83be-560acee90493",
          "name": "skills",
          "jsonValue": [
            "Skill 1"
          ]
        }
      ],
      "definitionId": "C-456",
      "tenantId": ""
    }

查询

GET idx_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "definitionKey": "example.definition"
          }
        },
        {
          "nested": {
            "path": "variables",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "variables.name": "skills"
                    }
                  },
                  {
                    "term": {
                        "variables.jsonValue": {
                        "value": "Skill 1"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

相关问题