【问题标题】:Nested filter returns 0 doc_count嵌套过滤器返回 0 doc_count
【发布时间】:2020-07-22 14:43:45
【问题描述】:

对于这个索引和样本数据:

PUT job_offers
{
  "mappings": {
    "properties": {
      "location": {
        "properties": {
          "slug": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        },
        "type": "nested"
      },
      "experience": {
        "properties": {
          "slug": {
            "type": "keyword"
          },
          "name": {
            "type": "keyword"
          }
        },
        "type": "nested"
      }
    }
  }
}

POST job_offers/_doc
{
  "title": "Junior Ruby on Rails Developer",
  "location": [
    {
      "slug": "new-york",
      "name": "New York"
    },
    {
      "slug": "atlanta",
      "name": "Atlanta"
    },
    {
      "slug": "remote",
      "name": "Remote"
    }
  ],
  "experience": [
    {
      "slug": "junior",
      "name": "Junior"
    }
  ]
}

POST job_offers/_doc
{
  "title": "Ruby on Rails Developer",
  "location": [
    {
      "slug": "chicago",
      "name": "Chicago"
    },
    {
      "slug": "atlanta",
      "name": "Atlanta"
    }
  ],
  "experience": [
    {
      "slug": "senior",
      "name": "Senior"
    }
  ]
}

我尝试在experience.slug 上运行过滤器:

GET job_offers/_search
{
  "query": {
    "nested": {
      "path": "location",
      "query": {
        "terms": {
          "location.slug": [
            "remote",
            "new-york"
          ]
        }
      }
    }
  },
  "aggs": {
    "filtered_job_offers": {
      "global": {},
      "aggs": {
        "filtered_location": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "experience.slug": [
                      "junior"
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
  }
}

对此的回应:

  "aggregations" : {
    "filtered_job_offers" : {
      "doc_count" : 2,
      "filtered_location" : {
        "doc_count" : 0
      }
    }
  }

为什么filtered_location 得到doc_count: 0 而不是1?我怎样才能让它发挥作用?

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation elasticsearch-query


    【解决方案1】:

    你已经很接近了!必须在聚合中使用嵌套查询:

    ...
      "aggs": {
        "filtered_job_offers": {
          "global": {},
          "aggs": {
            "filtered_location": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "nested": {                    <-----
                        "path": "experience",             
                        "query": {
                          "terms": {
                            "experience.slug": [
                              "junior"
                            ]
                          }
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        }
      }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 2015-03-03
    • 1970-01-01
    • 2020-05-26
    • 2022-09-12
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多