【问题标题】:Elasticsearch: Bucket Sort Aggregation + Sorting on Alphabetical FieldElasticsearch:桶排序聚合+按字母字段排序
【发布时间】:2021-01-30 19:00:02
【问题描述】:

我正在尝试将数据放在字段中,并且应该在另一个字段上进行排序。我也想要分页,所以我想我可以使用弹性搜索的 BucketSort。我遇到了字符串(按字母顺序)归档的问题。

这是我的虚拟数据。

{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp001_local000000000000001",
    "_score": 10.0,
    "_source": {
        "name": [
            "Person 01"
        ],
        "groupbyid": [
            "group0001"
        ],
        "ranking": [
             "2.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp002_local000000000000001",
    "_score": 85146.375,
    "_source": {
        "name": [
            "Person 02"
        ],
        "groupbyid": [
            "group0001"
        ],
        "ranking": [
             "10.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp003_local000000000000001",
    "_score": 20.0,
    "_source": {
        "name": [
            "Person 03"
        ],
        "groupbyid": [
            "group0002"
        ],        
        "ranking": [
             "-1.0"
        ]
    }
},
{
    "_index": "testing-aggregation",
    "_type": "employee",
    "_id": "emp004_local000000000000001",
    "_score": 5.0,
    "_source": {
        "name": [
            "Person 04"
        ],
        "groupbyid": [
            "group0002"
        ],
        "ranking": [
             "2.0"
        ]
    }
}

以上数据的映射。

{
    "name": {
        "type": "text",
        "fielddata": true,
        "fields": {
            "lower_case_sort": {
                "type": "text",
                "fielddata": true,
                "analyzer": "case_insensitive_sort"
            }
        }
    },
   "ranking": {
         "type": "float"
    },
    "groupbyid": {
        "type": "text",
        "fielddata": true,
        "index": "analyzed",
        "fields": {
            "raw": {
                "type": "keyword",
                "index": "not_analyzed"
            }
        }
    }
}

ES查询:

{
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "name:XYZ"
          }
        }
      ]
    }
  },
  "aggregations": {
    "groupbyid": {
      "terms": {
        "field": "groupbyid.raw",
        "size": 100
      },
      "aggs": {
        "top_hit_agg": {
          "top_hits": {
            "size": 100
          }
        },
        "ranking_agg": {
            "min": {
                "field": "ranking"
            }
        },
        "test_bucket_sort": {
          "bucket_sort": {
            "sort": [
              {
                "ranking_agg": {
                  "order": "desc"
                }
              }
            ],
            "size": 100,
            "from": 0
          }
        }
      }
    }
  }
}

我能够实现数字领域。但是不知道我将如何处理名称字段。一种方法是使用脚本,但我不想采用这种方法,因为它可能是一项昂贵的操作。

谁能帮我解决这个问题?我正在使用 ES 7.7.1。

谢谢你, 沙维尔沙阿

【问题讨论】:

  • 你能分享你的预期输出吗?
  • 假设我想对字段 groupbyid 进行俱乐部记录并按 desc 对 name 字段进行排序,输出将如下所示。 group0002(emp003_local000000000000001,emp004_local000000000000001)group0001(emp001_local000000000000001,emp002_local000000000000001)`

标签: elasticsearch elastic-stack elasticsearch-aggregation


【解决方案1】:

如果你想按字母顺序对name 字段进行排序,那么 groupbyid 您可以在聚合和排序方面使用 name.keyword 在钥匙上。

您不能在 min 聚合中使用 name 字段,因为 Min aggregation 不支持文本字段

{
  "aggregations": {
    "groupbyname": {
      "terms": {
        "field": "name.keyword",
        "order": { "_key" : "desc" }
      }
    }
  }
}

搜索结果:

"aggregations": {
    "groupbyname": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "Person 04",
          "doc_count": 1
        },
        {
          "key": "Person 03",
          "doc_count": 1
        },
        {
          "key": "Person 01",
          "doc_count": 1
        }
      ]
    }

【讨论】:

  • 在这个场景中,我还想对记录进行分组。它也将转到一个价值相同的俱乐部名称。所以我想要一个基于 groupbyid 的俱乐部,并根据名称字段对整个存储桶进行排序。我知道这有点棘手,但这是我们的用例之一。
猜你喜欢
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 2019-02-18
  • 2018-09-14
  • 2016-01-26
  • 2020-06-17
  • 1970-01-01
相关资源
最近更新 更多