首页 文章

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

提问于
浏览
0

这是我的映射的片段:

"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"
     }
  }
],

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

“默认情况下,Fielddata在文本字段上被禁用 . 在[categories.name]上设置fielddata = true,以便通过反转索引来加载内存中的fielddata . 但是,这可以使用大量内存 . 或者使用关键字字段 . ”

那么我将映射更改为:

"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]下找到嵌套对象”

其他一些 Map 工作,但给我未分类的结果

1 回答

  • 0

    你应该用

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

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

相关问题