【问题标题】:How can I sort Elasticsearch result based on nested field with keyword datatype?如何根据关键字数据类型的嵌套字段对 Elasticsearch 结果进行排序?
【发布时间】:2018-07-02 07:26:34
【问题描述】:

这是我的映射的 sn-p:

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

这不合理:

“[nested] 未能在路径 [categories.name] 下找到嵌套对象”

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

【问题讨论】:

  • 你最后找到解决办法了吗?我也面临着类似的问题

标签: sorting elasticsearch nested


【解决方案1】:

你应该使用

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

因为嵌套字段仍然是类别而不是类别名称

【讨论】:

  • 这不会给我一个错误,但结果仍然未排序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-06
相关资源
最近更新 更多