【问题标题】:Match documents where _id exists in nested array of objects匹配嵌套对象数组中存在 _id 的文档
【发布时间】:2022-11-05 00:23:53
【问题描述】:

我有以下弹性搜索索引

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

我想搜索所有行业数组包含带有_id = 81ca8f45-5b6a-11ed-96b4-0242ac110002 的标签的公司。

我尝试了以下查询,但我无法让它匹配任何文档。

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

【问题讨论】:

    标签: php elasticsearch ongr


    【解决方案1】:

    尝试使用带有关键字字段的术语查询。更改为“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"
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-05-01
      • 2017-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 1970-01-01
      相关资源
      最近更新 更多