弹性搜索过滤最大值文档 [英] Elasticsearch filter the maximum value document

查看:185
本文介绍了弹性搜索过滤最大值文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从同名记录中获取文档的最大值。例如,我有3个用户,其中2个具有相同的名称,但不同的追随者计数,我想根据最大的followers_count从相同的名称返回只有1个文档。

I trying to get the maximum value of document from the same name records. Forexample, I have 3 users, 2 of them have same name but different followers count, I wanted to return only 1 document from the 2 same with same name based on the maximum of followers_count.

{ 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个相同的名字,最多追随者的胜利和其他一个名字也将来临。

From 2 same names, the one with the maximum followers wins and other single one will also come.

我的映射如下,

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

这是我从长久而来的地方, / p>

This is where I have been stucked from long,

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

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

            size: 1000,
          }

任何建议请

推荐答案

你的问题在弹性堆栈中有一个特殊的工具,作为头kkk的锤子。
集合,请参阅示例:
首先在您的情况下,您将需要通过全名(包括空格)进行聚合,您的姓名字段需要 not_analyzed 这样

Your question have a special tool in the elastic stack as a hammer for a head kkk. Are Aggregations, See the examples: First of all in your case you will need aggregate by full name including spaces, your name field need to be not_analyzed like this

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

现在您的查询将如下所示:

Now your query will be like this one:

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

我只是按名称聚合,并使用最大指标以获得最高的追随者数。

I just aggregated by name and used a max metric to get the higgest follower count.

答案将如下所示:

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

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

Hope that will be good for you. Use aggregations for all situations that you need aggregate data and get sum on values.

这篇关于弹性搜索过滤最大值文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆