如何根据具有关键字数据类型的嵌套字段对Elasticsearch结果进行排序?

时间:2018-07-02 07:26:35

标签: sorting elasticsearch nested

这是我的映射的一个片段:

    "products": {
    "properties": {
        "availability_date": {
            "type": "date"
        },
        "banner": {
            "properties": {
                "id": {
                    "type": "long"
                },
                "copyright": {
                    "type": "keyword"
                },
                "url": {
                    "type": "keyword"
                }
            }
        },
        "categories": {
            "type": "nested",
            "properties": {
                "id": {
                    "type": "long"
                },
                "category_type": {
                    "type": "keyword"
                },
                "name": {
                    "type": "text"
                }
            }
        }
    }

我想根据“ categories.name”对搜索结果进行排序

我尝试用以下方式击中它:

"sort":[
  {
     "categories.name":{
        "order":"asc",
        "nested_path":"categories"
     }
  }
],

但这不起作用,并返回一条消息:

“默认情况下,文本字段上的字段数据是禁用的。在[categories.name]上设置fielddata = true以便通过反转取反的索引将字段数据加载到内存中。请注意,这可能会占用大量内存。或者使用关键字字段代替。”

因此,我将映射更改为:

"products": {
"properties": {
    "availability_date": {
        "type": "date"
    },
    "banner": {
        "properties": {
            "id": {
                "type": "long"
            },
            "copyright": {
                "type": "keyword"
            },
            "url": {
                "type": "keyword"
            }
        }
    },
    "categories": {
        "type": "nested",
        "properties": {
            "id": {
                "type": "long"
            },
            "category_type": {
                "type": "keyword"
            },
            "name": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "normalizer": "sort_normalizer"
                    }
                }
            }
        }
    }
}

现在如何进行分类?我尝试过:

"sort":[
  {
     "categories.name.keyword":{
        "order":"asc",
        "nested_path":"categories.name"
     }
  }
],

这与原因不符:

“ [嵌套]无法在路径[categories.name]下找到嵌套对象”

其他一些地图工作却给了我未排序的结果

1 个答案:

答案 0 :(得分:0)

您应该使用

 "sort":[
      {
         "categories.name.keyword":{
            "order":"asc",
            "nested_path":"categories"
         }
      }
    ],

因为嵌套字段仍然是类别而不是category.name