首页 > 解决方案 > 弹性搜索排序 - 非嵌套元素

问题描述

我想在弹性搜索中使用排序。我尝试了 ES 中建议的嵌套排序 - 嵌套排序示例

但后来我发现我的索引挡住了,属性不是“嵌套”类型。

图像 - ES 映射

那么对非嵌套类型的内部元素进行排序的最佳方法是什么。

我希望在 MDMGlobalData.City 字段中使用排序

标签: elasticsearch

解决方案


好的,我试图复制你的问题

PUT my_index
{
  "mappings": {
    "properties": {
      "MDMGlobalData": {
        "properties": {
          "City": {
            "type": "text"
          }
        }
      }
    }
  }
}

用这些插入文档


POST my_index/_doc
{
  "MDMGlobalData.City":  "Chennai"
  
}
POST my_index/_doc
{
  "MDMGlobalData.City":  "Bangalore"
  
}

试图用这个排序

GET /my_index/_search
{
  "sort": {
    "MDMGlobalData.City": {
      "order": "asc"
    }
  }
}

收到以下错误

{
  "error": {
    "root_cause": [
      {
        "type": "illegal_argument_exception",
        "reason": "Fielddata is disabled on text fields by default. Set fielddata=true on [MDMGlobalData.City] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
      }
......

因此我更新了启用 fielddata 的映射

PUT my_index/_mapping
{
  "properties": {
    "MDMGlobalData": {
      "properties": {
        "City": {
          "type": "text",
          "fielddata": true
        }
      }
    }
  }
}

现在我搜索

GET /my_index/_search
{
  "sort": {
    "MDMGlobalData.City": {
      "order": "asc"
    }
  }
}

它有效


推荐阅读