【问题标题】:ElasticSearch - query documents: All given labels appear at least one time in a nested documentElasticSearch - 查询文档:所有给定的标签在嵌套文档中至少出现一次
【发布时间】:2016-08-24 11:46:16
【问题描述】:

考虑一些包含嵌套文档 (skills) 的索引文档 (employees),这些文档具有一个属性,比如“label”,这样每个员工都与一些技能相关联。我想从 ElasticSearch 索引中获取所有掌握给定技能的员工(文档),例如“Python”和“Java”。

我正在努力寻找合适的查询来确保所有给定的技能(“Python”、“Java”)在员工,尽管他们不必必须一起出现!

我的映射和这个类似:

{
  "mappings": {
    "employee": {
      "_all": { "enabled": false },
      "properties": {
        "id" : { "type": "integer" },
        "first_name" : { "type": "string" },
        "last_name" : { "type": "string" },

        "skills": {
          "type": "nested",
          "properties": {
            "label": { "type": "string" },
            "rating": { "type": "integer" }
          }
        }

      }
    }
  }
} 

所以我正在寻找有关如何检索所需结果的任何解决方案(查询)。

【问题讨论】:

    标签: search elasticsearch


    【解决方案1】:

    您需要在bool/filter 查询中组合使用两个 nested 过滤器,如下所示:

    POST /employees/employee/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "nested": {
                "path": "skills",
                "query": {
                  "term": {
                    "skills.label": "python"
                  }
                }
              }
            },
            {
              "nested": {
                "path": "skills",
                "query": {
                  "term": {
                    "skills.label": "java"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多