Elasticsearch使用空格排序

时间:2014-03-21 07:42:40

标签: elasticsearch

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

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

标题文件包含以下值:

" Java Developer"

" C Developer"

" Ruby Developer"

" Java QA"

" Ruby QA"

" Java Dev"

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

{
  "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
}

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

1 个答案:

答案 0 :(得分:1)

您需要重新映射字段。

示例:

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

然后进行适当的排序,您需要指定“关键字”。

示例:

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