【问题标题】:Elasticsearch sorting with whitespace使用空格进行 Elasticsearch 排序
【发布时间】:2020-07-30 22:51:07
【问题描述】:

我有名为 clear 的弹性搜索索引并输入 positionTemplate。它包含名为 title 的字段。我使用 River jdbc 插件创建了这个索引/类型。我使用以下 api 更改了标题字段的映射。

 PUT clear/positionTemplate/_mapping
{
    "mappings": {
        "positionTemplate": {
            "properties": {
                "title": {
                    "type": "string", 
                    "index": "not_analyzed"
                }
            }
        }
    }
}

标题字段包含以下值:

“Java 开发者”

“C 开发者”

“Ruby 开发者”

“Java QA”

“Ruby QA”

“Java 开发”

在运行以下查询时排序不正确。

{
  "sort" : [{
    "title" : {"order":"desc"}
  }], 
  "fields" : [ "title"]
}

输出是:

{
    "_shards": {
        "failed": 0,
        "successful": 5,
        "total": 5
    },
    "hits": {
        "hits": [
            {
                "_id": "3",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Ruby Developer"
                    ]
                },
                "sort": [
                    "ruby"
                ]
            },
            {
                "_id": "5",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Ruby QA"
                    ]
                },
                "sort": [
                    "ruby"
                ]
            },
            {
                "_id": "4",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java QA"
                    ]
                },
                "sort": [
                    "qa"
                ]
            },
            {
                "_id": "15",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java Dev"
                    ]
                },
                "sort": [
                    "java"
                ]
            },
            {
                "_id": "1",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "Java Developer"
                    ]
                },
                "sort": [
                    "java"
                ]
            },
            {
                "_id": "2",
                "_index": "clear",
                "_score": null,
                "_type": "positionTemplate",
                "fields": {
                    "title": [
                        "C Developer"
                    ]
                },
                "sort": [
                    "developer"
                ]
            }
        ],
        "max_score": null,
        "total": 6
    },
    "timed_out": false,
    "took": 2
}

这是错误的。有人可以指导我如何纠正它吗?

【问题讨论】:

  • 你确定你的字段没有被分析?因为您的结果显示 "sort": ["ruby"]"sort": ["java"]"sort": ["developer"] 这正是它应该的顺序,但显然标题已被分析。
  • 我已经如前所述更改了映射。我也有类似的疑问。如何验证 not_analyzed 是否被应用?
  • 您可以从服务器http://localhost:9200/myIndex/myType/_mapping(或者更具体地说就是field mapping)检索映射
  • 你能在哪里解决呢?我目前面临完全相同的问题。尽管我在映射中有 index: not_analyzed,但排序字段被空格分割。

标签: elasticsearch


【解决方案1】:

您需要重新映射该字段。

例子:

{
  "<field name>": {
    "type" "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  }
}

然后在排序适当性上,您需要指定“关键字”。

例子:

GET <index>/_search
{
  "sort": [
  {
    "<field name>.keyword": {
      "order": "asc"
    }
  }
 ]
}

【讨论】:

    猜你喜欢
    • 2020-09-19
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多