drnf09037160 2018-11-14 03:26
浏览 97
已采纳

如何使用go在弹性搜索中按嵌套对象对文档进行排序?

I'm new to Elasticsearch.

I have documents and each of them has a structure like this:

{
    "created_at": "2018-01-01 01:01:01",
    "student": {
        "first_name": "john",
        "last_name": "doe"
    },
    "parent": {
        "first_name": "susan",
        "last_name": "smile"
    }
}

I just want to sort those documents based on student.first_name using olivere/elastic package for go.

This is my query at the moment:

searchSvc = searchSvc.SortBy(elastic.NewFieldSort("student.first_name").Asc())

and I'm getting this error:

elastic: Error 400 (Bad Request): all shards failed [type=search_phase_execution_exception]

However when I tried sorting it by created_at, it's working.

searchSvc = searchSvc.SortBy(elastic.NewFieldSort("created_at").Asc())

I don't have any mapping in the index. (is this the problem?)

I tried searching for something like "Elasticsearch sort by nested object", but I always got questions that need to sort an array in the nested object.

  • 写回答

1条回答 默认 最新

  • doulin6761 2018-11-14 03:40
    关注

    It turns out that this is a beginner mistake.. You can't sort by text fields. I got it from here elasticsearch-dsl-py Sorting by Text() field

    What you can do though, if you don't specify mappings, you can sort by the keyword property of the field.

    searchSvc = searchSvc.SortBy(elastic.NewFieldSort("student.first_name.keyword").Asc())

    And it works!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制