在Elasticsearch中需要对具有数字和字母数字值的字段进行排序

时间:2019-04-22 09:45:48

标签: elasticsearch

我正在尝试为一个字段创建映射,该映射仅需要用于完全匹配和排序。我不想将主要数据类型设置为文本,因为我只需要完全匹配即可。

{
  "index_patterns": "*",
  "mappings": {
    "doc": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "my_field": {
          "type": "keyword",
          "index": true,
          "fielddata": true
        }
      }
    }
  }
}

但是我遇到以下错误:

{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Mapping definition for [my_field] has unsupported parameters:  [fielddata : true]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [doc]: Mapping definition for [my_field] has unsupported parameters:  [fielddata : true]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Mapping definition for [my_field] has unsupported parameters:  [fielddata : true]"
    }
  },
  "status": 400
}

问题在于,如果没有fielddata进行排序,就不会正确进行。例如,以下是示例asc排序输出:

"90000001" "90000001" "" "" "" "90000008" "9100000"

1 个答案:

答案 0 :(得分:0)

您无需为fielddata: true字段设置keyword。您还可以删除index: true,因为它是keyword字段的默认设置:

就像这样:

{
  "index_patterns": "*",
  "mappings": {
    "doc": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "my_field": {
          "type": "keyword"
        }
      }
    }
  }
}