【问题标题】:Elasticsearch filter the maximum value documentElasticsearch 过滤最大值文档
【发布时间】:2016-02-18 19:25:41
【问题描述】:

我试图从同名记录中获取文档的最大值。例如,我有 3 个用户,其中 2 个同名但关注者数不同,我想根据 follower_count 的最大值从 2 个同名的同名文档中只返回 1 个文档。

{ id: 1, name: "John Greenwood", follower_count: 100 }
{ id: 2, name: "John Greenwood", follower_count: 200 }
{ id: 3, name: "John Underwood", follower_count: 300 }

所以结果是,

{ id: 2, name: "John Greenwood", follower_count: 200 }
{ id: 3, name: "John Underwood", follower_count: 300 }

从2个相同的名字中,拥有最多关注者的人获胜,另外一个人也会来。

我有如下映射,

"users-development" : {
    "mappings" : {
      "user" : {
        "dynamic" : "false",
        "properties" : {
          "follower_count" : {
            "type" : "integer"
          },
          "name" : {
            "type" : "string",
            "fields" : {
              "exact" : {
                "type" : "string",
                "index" : "not_analyzed"
              }
            }
          },
        }
      }
    }

这就是我长期被困的地方,

         {
            query: {
              filtered: {
                filter: {
                  bool: {
                    must: [
                      { terms: { "name.exact": [ "John Greenwood", "John Underwood" ] } },
                    ]
                  }
                }
              }
            },

            aggs: {
              max_follower_count: { max: { field: 'follower_count' } }
            },

            size: 1000,
          }

有什么建议

【问题讨论】:

  • 这是我唯一有时间找到的:stackoverflow.com/questions/18449703/… 将尝试再次回复更多细节
  • 这是完整的查询吗?您收到任何错误消息吗? size:1000, 末尾的尾随逗号会导致问题。我使用您的数据运行了查询,其他方面没有问题。
  • @IanGabes 不,我在 ruby​​ 中工作时没有收到任何错误消息

标签: ruby ruby-on-rails-4 elasticsearch elasticsearch-rails


【解决方案1】:

您的问题在弹性堆栈中有一个特殊工具,作为头部 kkk 的锤子。 是Aggregations,见例子: 首先,在您的情况下,您需要按全名聚合,包括空格,您的姓名字段需要像这样not_analyzed

`PUT /index
{
  "mappings": {
    "users" : {
      "properties" : {
        "name" : {
          "type" :    "string",
          "index": "not_analyzed"
        }
      }
    }
  }
}`

现在您的查询将是这样的:

`POST /index/users/_search
{
   "aggs": {
      "users": {
         "terms": {
            "field": "name"
         },
         "aggs": {
            "followers": {
               "max": {
                  "field": "follower_count"
               }
            }
         }
      }
   }
}`

我只是按名称聚合并使用了最大 metric 来获得最多的关注者数量。

响应会是这样的:

`"aggregations": {
      "users": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "John Greenwood",
               "doc_count": 2,
               "followers": {
                  "value": 200
               }
            },
            {
               "key": "John Underwood",
               "doc_count": 1,
               "followers": {
                  "value": 300
               }
            }
         ]
      }
   }`

希望这对你有好处。 在所有需要聚合数据并获取值的总和的情况下使用聚合。

【讨论】:

  • awesome 似乎是一个很好的解决方案,如果我有另一个字段,我想在 false 上设置一个 true 并且如果应用了该偏好,我不想应用聚合。想法
  • @MuhamamdAwais 可能这就是你的问题stackoverflow.com/questions/28050292/…
【解决方案2】:

好的,我认为您正在寻找类似的东西,使用 terms aggregation

{
   "query": {
      "terms": { "name.exact": [ "John Greenwood", "John Underwood" ] }
   },
   "aggs": {
      "max_follower_count": {
         "terms": {
            "field":"name.exact"
         },
         "aggs":{
             "max_follow" : { "max" : { "field" : "follower_count" } }
         }
      }
   },
   "size": 1000
}

terms 聚合将为来自names.exact 的每个唯一值创建一个存储桶,该值仅是您在 terms 查询中指定的值。所以我们现在有两个 Johns 的存储桶,现在我们可以使用 max 聚合来计算谁拥有最多的关注者。 max 聚合将对其父聚合中的每个存储桶进行操作。

然后,这些唯一术语中的每一个都将计算其最大值follower_count,并显示在存储桶中。结果如下:

... //query results of just the terms query up here
"aggregations": {
  "max_follower_count": {
     "doc_count_error_upper_bound": 0,
     "sum_other_doc_count": 0,
     "buckets": [
        {
           "key": "John Greenwood",
           "doc_count": 2,
           "max_follow": {
              "value": 200
           }
        },
        {
           "key": "John Underwood",
           "doc_count": 1,
           "max_follow": {
              "value": 300
           }
        }
     ]
  }
}

术语聚合带有一些关于它如何进行计数的警告,并且链接的文档应该非常清楚。

【讨论】:

    猜你喜欢
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    相关资源
    最近更新 更多