ElasticSearch:桶排序聚合+按字母字段排序

ggazkfy8  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(580)

我正在尝试俱乐部数据的领域,这应该是另一个领域的排序。我还想分页,所以我想我可以使用bucketsort的ElasticSearch。我正面临一个字符串(字母顺序)的问题。
这是我的虚拟数据。

{
    "_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"
        ]
    }
}

上述数据的Map。

{
    "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。
谢谢你,沙维尔·沙阿

xdnvmnnf

xdnvmnnf1#

如果你想分类 name 按字母顺序输入字段,然后代替 groupbyid 你可以用 name.keyword 在关键字上进行聚合和排序。
您不能使用 name 最小聚合中的字段,因为最小聚合不支持文本字段

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

相关问题