ElasticSearch未排序结果

问题描述 投票:1回答:1

我正在尝试根据numeric字段对结果进行排序,

这是我的映射:

{
  "elasticie": {
    "mappings": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "number": {
          "type": "long"
        }
      }
    }
  }
}

我正在使用Python,这是我的测试数据:

data = [
    {'name': 'sElwUYiLXGHaQCKbdxtnvVzqIehfFWkJcPSTurgNoRD', 'number': 8583},
    {'name': 'XJEtNsIFfcwHTMhqAvRkiygjbUGzZQPdS', 'number': 8127},
    {'name': 'ZIeAGosUKJbjOdylM', 'number': 5862},
    {'name': 'HYvcafoXkC', 'number': 7458},
    {'name': 'tATJCjNuizOlGckXBpyVqSQL', 'number': 530},
    {'name': 'TFYixotjhXzNZPvHnkraRDpAMEImJfqdcVGLC', 'number': 7052},
    {'name': 'JCEGfoKDHRrcIkPQSqiVgNshZOBaMdXjAlxwUzmeWLy', 'number': 6168},
    {'name': 'IpCTwUAQynSizJtcsuDmbX', 'number': 6492},
    {'name': 'fTrcoXSBJNFhAkzWpDMxsEiLmZRvgnC', 'number': 382},
    {'name': 'ulVNmqKTpPXfEIdiykhDjMrUGOYazLBFvgnWwsRtJoQbxSe', 'number': 2061}
]

使用以下代码,我正在创建索引并插入数据:

from elasticsearch import Elasticsearch
from data import data  # the data I've shown above

INDEX = 'elasticie'
es = Elasticsearch('http://127.0.0.1:9200')

for _ in data:
    es.index(index=INDEX, body=_)

我正在尝试根据数字ascdesc对数据进行排序这是我到目前为止尝试过的:

es.search(index=INDEX, params={'sort': {'number': {'order': 'asc'}})
es.search(index=INDEX, params={'sort': {'number': 'asc'})
es.search(index=INDEX, params={'sort': [('number', 'asc')]})
es.search(index=INDEX, params={'sort': {'number': {'order': 'asc', 'ignore_unmapped': True}})
es.search(index=INDEX, params={'sort': {'number': {'order': 'asc', 'unmapped_type': 'integer'}})
es.search(index=INDEX, params={'sort': {'number': {'order': 'asc', 'unmapped_type': 'long'}})
es.search(index=INDEX, params={'sort': {'number.raw': 'asc'})

以上方法都不适合我,结果与插入的数据相同,如果我将上述各行分配给名为search_result的变量,然后使用以下代码打印结果:

for index, result in enumerate(search_result['hits']['hits']):
    print(f'{index}. {result["_source"]["number"]}')

我将得到以下结果:

0. 8583
1. 8127
2. 5862
3. 7458
4. 530
5. 7052
6. 6168
7. 6492
8. 382
9. 2061

显然没有使用number字段进行排序!

我不知道自己在做什么错,我使用的是ElasticSearch 7.6和Python 3.8

如何使排序结果起作用?

更新

基于调试日志,Python使用第一种方法向以下URL发送GET请求:http://127.0.0.1:9200/elasticie/_search?sort={%27number%27%3A+{%27order%27%3A+%27asc%27}}

<<

我正在尝试根据数字字段对结果进行排序,这是我的映射:{“ elasticie”:{“ mappings”:{“ properties”:{“ name”:{“ type”:“ text”, “ fields”:...

python sorting elasticsearch
1个回答
0
投票
排序搜索查询
© www.soinside.com 2019 - 2024. All rights reserved.