如何使用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.
我是Elasticsearch的新手。 p>
我有文档,每个文档 它们具有这样的结构: p>
{
“ created_at”:“ 2018-01-01 01:01:01”,
“ student”:{
“ first_name”:“ john”,
“ last_name”:“ doe”
},
“ parent”:{
“ first_name”:“ susan”,
“ last_name”:“微笑”
}
}
code> pre>
我只想使用 olivere / elastic code>根据 student.first_name code>对这些文档进行排序 p>
这是我目前的查询: p>
searchSvc = searchSvc.SortBy( elastic.NewFieldSort(“ student.first_name”)。Asc()) code> p>
,并且我收到此错误: p>
\ n 弹性:错误400(错误请求):所有分片均失败
[type = search_phase_execution_exception] p>
blockquote>
但是,当我尝试按对其进行排序时 created_at code>,它可以正常工作。 p>
searchSvc = searchSvc.SortBy(弹性。 NewFieldSort(“ created_at”)。Asc()) code> p>
索引中没有任何映射。 (这是问题吗?) p>
我尝试搜索“ Elasticsearch按嵌套对象排序”之类的内容,但始终遇到需要对嵌套对象中的数组进行排序的问题。 p> p>
div>
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!