首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >按跨文档查询的Elasticsearch组合组

问按跨文档查询的Elasticsearch组合组
EN

Stack Overflow用户
提问于 2020-02-22 12:16:40
回答 1查看 250关注 0票数 0

我们有一个弹性搜索文档,它有一个叫做城市的维度。每个文档对于城市字段只有一个值。我有一个场景,我需要根据城市来查询这个人。

文档在弹性搜索中的应用

代码语言:javascript
复制
{
  person_id: "1",
  property_value : 25000,
  city: "Bangalore"
}
{
  person_id: "2",
  property_value : 100000,
  city: "Bangalore"
}
{
  person_id: "1",
  property_value : 15000,
  city: "Delhi"
}

备注:聚合应该在property_value上执行,而group应该在person_id上执行。

比如,

  1. 如果我查询班加罗尔,它应该返回带有person_id 1和2的文档。
  2. 如果我同时查询德里和班加罗尔,它应该返回以下内容 { person_id:"1",property_value : 40000,城市:“班加罗尔”,“德里”}
EN

回答 1

Stack Overflow用户

发布于 2020-02-22 13:00:28

查看您的数据,我提供了一个示例映射、请求查询和响应。

制图:

代码语言:javascript
复制
PUT my_index_city
{
  "mappings": {
    "properties": {
      "person_id":{
        "type": "keyword"
      },
      "city":{
        "type":"text",
        "fields":{
          "keyword":{       
            "type": "keyword"
          }
        }
      },
      "property_value":{
        "type": "long"
      }
    }
  }
}

抽样请求:

请注意,我使用了简单的查询字符串来过滤包含班加罗尔和德里的文档。

对于聚合,我在person_id上使用了person_id,在property_value字段上使用了和聚合。

代码语言:javascript
复制
POST my_index_city/_search
{
  "size": 0, 
  "query": {
    "query_string": {
      "default_field": "city",
      "query": "Bangalore Delhi"
    }
  },
  "aggs": {
    "my_person": {
      "terms": {
        "field": "person_id",
        "size": 10,
        "min_doc_count": 2
      },
      "aggs": {
        "sum_property_value": {
          "sum": {
            "field": "property_value"
          }
        }
      }
    }
  }
}

抽样答复:

代码语言:javascript
复制
{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "my_person" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1",
          "doc_count" : 2,
          "sum_property_value" : {
            "value" : 40000.0
          }
        }
      ]
    }
  }
}

注意:这个查询只有在person_id有多个文档(),但是每个文档具有唯一/不同的城市值时才能工作。

我想说的是,如果person_id有多个具有相同城市的文档,聚合就不会给出正确的答案。

最新答复:

除非您修改映射,否则没有直接的方法来实现您想要的结果。我所做的是,使用nested数据类型,并将person_id的所有文档作为一个单独的文档。

制图:

代码语言:javascript
复制
PUT my_sample_city_index
{
  "mappings": {
    "properties": {
      "person_id":{
        "type": "keyword"
      },
      "property_details":{
        "type":"nested",               <------ Note this
        "properties": {
          "city":{
            "type": "text",
            "fields":{
              "keyword":{
                "type":"keyword"
              }
            }
          },
          "property_value":{
            "type": "long"
          }
        }
      }
    }
  }
}

样本文件:

代码语言:javascript
复制
POST my_sample_city_index/_doc/1
{
  "person_id": "1",
  "property_details":[
    {
      "property_value" : 25000,
      "city": "Bangalore"
    },
    {
      "property_value" : 15000,
      "city": "Delhi"
    }
    ]
}

POST my_sample_city_index/_doc/2
{
  "person_id": "2",
  "property_details":[
    {
      "property_value" : 100000,
      "city": "Bangalore"
    }
    ]
}

聚合查询:

代码语言:javascript
复制
POST my_sample_city_index/_search
{
  "size": 0,
  "query": {
    "nested": {
      "path": "property_details",
      "query": {
        "query_string": {
          "default_field": "property_details.city",
          "query": "bangalore delhi"
        }
      }
    }
  },
  "aggs": {
    "persons": {
      "terms": {
        "field": "person_id",
        "size": 10
      },
      "aggs": {
        "property_sum": {
          "nested": {                          <------ Note this
            "path": "property_details"
          },
          "aggs": {
            "total_sum": {
              "sum": {
                "field": "property_details.property_value"
              }
            }
          }
        }
      }
    }
  }
}

请注意,我最初在person_id post上应用了一个术语查询,并应用了嵌套聚合,进一步应用了度量和聚合查询。

如果一个人在同一个城市拥有多个属性,这也应该是正确的。

响应:

代码语言:javascript
复制
{
  "took" : 31,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "persons" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "1",
          "doc_count" : 1,
          "property_sum" : {
            "doc_count" : 2,
            "total_sum" : {
              "value" : 40000.0
            }
          }
        },
        {
          "key" : "2",
          "doc_count" : 1,
          "property_sum" : {
            "doc_count" : 1,
            "total_sum" : {
              "value" : 100000.0
            }
          }
        }
      ]
    }
  }
}

如果这有帮助的话请告诉我!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60352070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档