elasticsearch 匹配对象嵌套数组中存在_id的文档

30byixjq  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(115)

我有以下ElasticSearch索引

{
  "companies": {
    "aliases": {},
    "mappings": {
      "properties": {
        "industries": {
          "type": "nested",
          "properties": {
            "_id": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "description": {
              "type": "text"
            },
            "priority": {
              "type": "integer"
            },
            "title": {
              "type": "text"
            }
          }
        }
      }
    }
  }
}

并且我希望搜索其industries数组包含_id = 81 ca 8 f45 - 5 b6 a-11 ed-96 b4 - 0242 ac 110002标记的所有公司。
我尝试了以下查询,但无法使其与任何文档匹配。

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "industries",
            "query": {
              "bool": {
                "should": [
                  {
                    "term": {
                      "industries._id": "81ca8f45-5b6a-11ed-96b4-0242ac110002"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "term": {
            "industries._id": "81ca8f45-5b6a-11ed-96b4-0242ac110002"
          }
        }
      ]
    }
  }
}

是否有可能匹配_id字段?因为我测试了下面的术语查询,它返回了一个很好的结果。

{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "industries.priority": 1
          }
        }
      ]
    }
  }
}
dy2hfwbg

dy2hfwbg1#

尝试使用关键字字段的术语查询。更改为“industries._id.keyword”

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "industries",
            "query": {
              "bool": {
                "should": [
                  {
                    "term": {
                      "industries._id.keyword": "81ca8f45-5b6a-11ed-96b4-0242ac110002"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "term": {
            "industries._id.keyword": "81ca8f45-5b6a-11ed-96b4-0242ac110002"
          }
        }
      ]
    }
  }
}

相关问题