Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 如何使用Go olivere/elastic基于多个字段进行排序_Sorting_Go_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Sorting,Go,elasticsearch" /> elasticsearch,Sorting,Go,elasticsearch" />

Sorting 如何使用Go olivere/elastic基于多个字段进行排序

Sorting 如何使用Go olivere/elastic基于多个字段进行排序,sorting,go,elasticsearch,Sorting,Go,elasticsearch,几天来,我一直试图知道如何使用Go根据多个字段进行排序。我正试着把它翻译成围棋 { "sort" : [ "name", { "age" : "desc" }, ], } 我试图使用NewFieldSort()并在搜索服务中提供一些SortBy()。它可以与一个SortBy()配合使用,但不能与两个SortBy()配合使用。它返回错误400(错误请求):所有碎片失败[

几天来,我一直试图知道如何使用Go根据多个字段进行排序。我正试着把它翻译成围棋

{
    "sort" : [
        "name",
        { "age" : "desc" },
    ],
}
我试图使用
NewFieldSort()
并在搜索服务中提供一些
SortBy()
。它可以与一个
SortBy()
配合使用,但不能与两个
SortBy()
配合使用。它返回
错误400(错误请求):所有碎片失败[type=search\u phase\u execution\u exception]

这是我的密码

    sortQuery1 := elastic.NewFieldSort("name")
    sortQuery2 := elastic.NewFieldSort("age").Desc()

    searchService := esclient.Search().
        Index("students").
        SortBy(sortQuery1).
        SortBy(sortQuery2)

    searchResult, err := searchService.Do(ctx)
你们有什么建议吗?提前谢谢

您在示例中使用的函数是可变的,您可以从签名中看到:
SortBy(sorter…sorter)*SearchService

因此,您只需使用两个筛选条件调用它一次:

    sortQuery1 := elastic.NewFieldSort("name")
    sortQuery2 := elastic.NewFieldSort("age").Desc()

    searchService := client.Search().
        Index("students").
        SortBy(sortQuery1, sortQuery2)
一旦将此请求正文编组为JSON,它将如下所示:

{
    "sort": [
        { "name": { "order": "asc" } },
        { "age": { "order": "desc" } }
    ]
}
您在示例中使用的函数是可变的,您可以从签名中看到:
SortBy(sorter…sorter)*SearchService

因此,您只需使用两个筛选条件调用它一次:

    sortQuery1 := elastic.NewFieldSort("name")
    sortQuery2 := elastic.NewFieldSort("age").Desc()

    searchService := client.Search().
        Index("students").
        SortBy(sortQuery1, sortQuery2)
一旦将此请求正文编组为JSON,它将如下所示:

{
    "sort": [
        { "name": { "order": "asc" } },
        { "age": { "order": "desc" } }
    ]
}

请指定您使用的olivere/elastic的哪个版本?@blackgreen olivere/elastic/V7请指定您使用的olivere/elastic的哪个版本?@blackgreen olivere/elastic/V7它工作起来很有魅力!!!!非常感谢你。顺便说一句,我认为我得到的错误的原因是因为按名称排序。我试着只按名字排序,结果得到了同样的错误。它像一个符咒一样工作!!!!非常感谢你。顺便说一句,我认为我得到的错误的原因是因为按名称排序。我试着只按名字排序,结果得到了同样的错误。