首页 > 解决方案 > 大写字母在弹性搜索中应该排在第一位

问题描述

我有一些数据,比如......芒果、苹果、香蕉、胡萝卜

我正在寻找弹性搜索排序查询,它应该给出结果,因为大写字母应该首先出现,如下所示

香蕉、芒果、苹果、胡萝卜

任何人都可以帮助我如何准备针对此要求的查询。谢谢

标签: elasticsearchelasticsearch-painless

解决方案


使用该keyword字段并按顺序对数据进行asc排序,您将能够获得所需的结果。

添加具有索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      }
    }
  }
}

指数数据:

{
  "name": "apple"
}
{
  "name": "Banana"
}
{
  "name": "Mango"
}
{
  "name": "carrot"
}

搜索查询:

{
  "sort": [
    {
      "name": {
        "order": "asc"
      }
    }
  ]
}

搜索结果:

"hits": [
      {
        "_index": "65624606",
        "_type": "_doc",
        "_id": "3",
        "_score": null,
        "_source": {
          "name": "Banana"
        },
        "sort": [
          "Banana"
        ]
      },
      {
        "_index": "65624606",
        "_type": "_doc",
        "_id": "1",
        "_score": null,
        "_source": {
          "name": "Mango"
        },
        "sort": [
          "Mango"
        ]
      },
      {
        "_index": "65624606",
        "_type": "_doc",
        "_id": "2",
        "_score": null,
        "_source": {
          "name": "apple"
        },
        "sort": [
          "apple"
        ]
      },
      {
        "_index": "65624606",
        "_type": "_doc",
        "_id": "4",
        "_score": null,
        "_source": {
          "name": "carrot"
        },
        "sort": [
          "carrot"
        ]
      }
    ]

推荐阅读