如何在不更改大小写的情况下不区分大小写进行排序

问题描述:

我的索引名称是data_new

My index name is data_new

下面是要插入索引的代码

Below is the code to insert into index

test = [   {'id':1,'name': 'A', 'professor': ['Bill Cage', 'accounting']},
    { 'id':2,  'name': 'AB', 'professor': ['Gregg Payne', 'engineering']},
    {'id':3,   'name': 'a',   'professor': ['Bill Cage', 'accounting']},
    {'id':4,'name': 'Tax Accounting 200', 'professor': ['Thomas Baszo', 'finance']},
    {'id':5,'name': 'Capital Markets 350', 'professor': ['Thomas Baszo', 'finance']},
    {'id':6,'name': 'Theatre 410', 'professor': ['Sebastian Hern', 'art']},
    {'id':7,'name': 'Accounting 101', 'professor': ['Thomas Baszo', 'finance']},
    {'id':8,'name': 'Marketing 101', 'professor': ['William Smith', 'finance']},
    {'id':8,'name': 'Anthropology 230', 'professor': ['Devin Cranford', 'history']},
    {'id':10,   'name': 'Computer Science 101',
        'professor': ['Gregg Payne', 'engineering']}]
from elasticsearch import Elasticsearch
import json
es = Elasticsearch()
es.indices.create(index='data_new', ignore=400)
for e in test:
        es.index(index="data_new", body=e, id=e['id'])
search = es.search(index="data_new", body={"from" : 0, "size" : 2,"query": {"match_all": {}}})
search['hits']['hits']

现在

[{'id':1,'name': 'A'},
{ 'id':2,  'name': 'AB'},
{'id':3,   'name': 'a'}]

预期按以下顺序

[{'id':1,'name': 'A'},
{ 'id':3,  'name': 'a'},
{'id':2,   'name': 'AB'}]

用于输入 ["a","b","B","C","c","A"] 结果为: ["A","B","C","a","b","c"]

我希望输出为 ["A","a","B","b","C","c"]

预期

我的第一个预期输出>我只需要在{不区分大小写}中对名称进行排序.我需要规范化name关键字并进行排序

My first Expected output > I need to sort the output with respect to name only in {Case insensitive}. I need to normalise name keyword and sort

如何对 search = es.search(index ="data_new",body = {"from":0,"size":2,"query":{"进行修改).match_all":{}}})

我使用以下代码更新了代码 search = es.search(index ="data_new",body = {"sort":[["name.keyword":{"order":"asc"}],{"size";:1000,"query":{"query_string":{"query":"A"}}}})

I have updated the code with below search = es.search(index="data_new", body={ "sort" : [{"name.keyword" : {"order" : "asc"}], {"size": 1000, "query": {"query_string": {"query": "A"}}})

"normalizer":"case_insensitive"} 我得到了错误

RequestError:RequestError(400,'x_content_parse_exception','[1:41] [field_sort]未知字段[normalizer]')

要使用规范化器,您需要将其定义到映射中.您将无法在搜索中将其用作参数.在您的情况下,您需要两个字段进行排序.我是通过将数据复制到其他字段来实现的.第一个字段具有小写规范化器,而另一个没有.

In order to use normalizer, you need to define it into your mapping. you are not able to use it as an argument in your search. In your case, you need to have two fields for sort. I have made this by copying data to other fields. the first field has lowercase normalizer and the other one not.

PUT /test_index/
{
  "settings": {
    "analysis": {
      "normalizer": {
        "myLowercase": {
          "type": "custom",
          "filter": [ "lowercase" ]
        }
      }
    }
  },
  "mappings":{
     "post":{
        "properties":{
           "name":{
              "normalizer":"myLowercase",
              "type":"keyword",
              "copy_to": [
              "name2"
            ]
           },
           "name2":{
              "type":"keyword"
           }
        }
     }
  }
}

您的查询将如下所示:

GET test_index/_search
{
  "query": {
    "match_all": {}
  },"sort": [
    {
      "name": {
        "order": "asc"
      }
    },
    {
      "name2":{
        "order": "asc"
      }
    }
  ]
}

这是索引中 name 字段必须具有的映射和​​设置,并且还需要向映射添加其他字段.请注意,这是针对低于7的elasticsearch版本的.如果您使用的是Elasticsearch版本7,则必须从映射中删除此处名为 post 的doc_type.

This is the mapping and setting that you must have for your name field in your indices and you need to add other fields to the mapping as well. Please have the attention that this is for elasticsearch version below 7. If you use elasticsearch version 7 you must delete doc_type which is named post here from the mapping.