基于第一个字符的弹性搜索字母排序 [英] Elastic search alphabetical sorting based on first character

查看:71
本文介绍了基于第一个字符的弹性搜索字母排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名字集合。

team dhoni
dhoni1
dibeesh 200
bb vineesh
devan

我想按字母升序(A-Z)对其进行排序,如下例所示

I want to sort it alphabetically ascending order (A - Z) like the following order

bb vineesh
devan
dhoni1
dibeesh 200
team dhoni

映射

 "first_name": {
      "type": "string",
      "store": "true"
},

我尝试过

{
  "sort": [
    {
      "first_name": {
        "order": "asc"

      }
    }
  ], 
 "query": {
    "match_all": {
    }
  }
}

当我运行此查询时,将按以下顺序获取名称。

When i run this query am getting the names in following order.

dibeesh 200
bb vineesh
devan
team dhoni
dhoni1

弹性搜索,将名字和数字作为首选项。

Elastic search taking first names with number as first preference.

如何防止这种情况发生?

How can I prevent this?

推荐答案

我也有类似情况问题,另一个答案对我来说并不太好。我参考的是此文档,可以通过这样的映射来解决

I had a similar issue and the other answer didn't quite get it for me. I referred to this documentation instead, and was able to solve by mapping like this

"name": { 
    "type":     "string",
    "analyzer": "english",
    "fields": {
        "raw": { 
            "type":  "string",
            "index": "not_analyzed"
        }
    }
}

然后进行查询和排序像这样

and then querying and sorting like this

{
    "query": {
        "match": {
            "name": "dhoni"
        }
    },
    "sort": {
        "name.raw": {
            "order": "asc"
        }
    }
}

这篇关于基于第一个字符的弹性搜索字母排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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