ElasticSearch排序不含空格且区分大小写

ojsjcaue  于 2023-01-29  发布在  ElasticSearch
关注(0)|答案(1)|浏览(202)

我在ElasticSearch中有用户信息,我想查询人员并按姓氏对结果进行排序,当我运行

{
  "size": 10,
  "query": {
    "bool": {
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [{ "last_name": { "order": "desc" } }]
}

我得到的结果是以空格开头,并且以小写字母开头。我想进行不区分大小写的排序,并在排序时忽略空格。
例如,输出的一部分是

{
            "first_name": "test",
            "last_name": "test"
        },
        {
            "first_name": "name",
            "last_name": "mangina"
        },
        {
            "first_name": "Nona",
            "last_name": "Zucker"
        }

我期望Z是降序的第一个。

krcsximq

krcsximq1#

因为不分析关键字数据类型(即字段值),所以当ES比较“t”的第一个字符(ASCII值116)与“Zucker”的第一个字符(ASCII值90)时,由于ASCII值的原因,“t”在此处具有较高的等级,并且“test”是结果中的第一条记录。
你必须包括你自己的分析器.你可以用下面的方法.
Map:

PUT username
{
  "settings": {
    "analysis": {
      "normalizer": {
        "my_normalizer": {
          "type": "custom",
          "filter": [
            "lowercase" // to treat uppercase and lowercase as being the same
          ],
          "char_filter": [
            "alphabets_char_filter" // to ignore everything except letters while sorting
          ]
        }
      },
      "char_filter": {
        "alphabets_char_filter": {
          "type": "pattern_replace",
          "pattern": "[^a-zA-Z]",
          "replacement": ""
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "first_name": {
        "type": "keyword"
      },
      "last_name": {
        "type": "keyword",
        "normalizer": "my_normalizer"
      }
    }
  }
}

插入文件:

PUT username/_doc/1
{
  "first_name": "test",
  "last_name": "test"
}

PUT username/_doc/2
{
  "first_name": "name",
  "last_name": "mangina"
}

PUT username/_doc/3
{
  "first_name": "Nona",
  "last_name": "Zucker"
}

PUT username/_doc/4
{
  "first_name": "Nona",
  "last_name": " Xucker"
}

质询:

GET username/_search
{
  "size": 10,
  "query": {
    "bool": {
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  },
  "sort": [{ "last_name": { "order": "desc" } }]
}

输出:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": null,
    "hits": [
      {
        "_index": "username",
        "_id": "3",
        "_score": null,
        "_source": {
          "first_name": "Nona",
          "last_name": "Zucker"
        },
        "sort": [
          "zucker"
        ]
      },
      {
        "_index": "username",
        "_id": "4",
        "_score": null,
        "_source": {
          "first_name": "Nona",
          "last_name": " Xucker"
        },
        "sort": [
          "xucker"
        ]
      },
      {
        "_index": "username",
        "_id": "1",
        "_score": null,
        "_source": {
          "first_name": "test",
          "last_name": "test"
        },
        "sort": [
          "test"
        ]
      },
      {
        "_index": "username",
        "_id": "2",
        "_score": null,
        "_source": {
          "first_name": "name",
          "last_name": "mangina"
        },
        "sort": [
          "mangina"
        ]
      }
    ]
  }
}

相关问题