我正在使用Elasticsearch 5.5并且有一个带有这种映射的索引:
{
"my_index": {
"mappings": {
"my_type": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"my_array": {
"properties": {
"array": {
"type": "float"
},
"length": {
"type": "long"
}
}
}
}
}
}
}
}
我想按标题搜索并按数组中的第一个值排序。将第一个值设置为_score
字段也很棒。
所以,我已经准备好了这样的要求:
GET my_index/my_type/_search
{
"query": {
"term": {
"title.keyword": "Shorts"
}
},
"sort" : {
"_script" : {
"type" : "number",
"script" : {
"lang": "painless",
"inline": "doc['my_array.array'][0]"
},
"order" : "asc"
}
}
}
不幸的是它不起作用。我觉得有些东西缺失或错误。
答案 0 :(得分:2)
使用该无痛脚本的正确方法是这样的:
{
"query": {
"term": {
"title.keyword": "Shorts"
}
},
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"inline": "params._source.my_array.array[0]"
},
"order": "asc"
}
}
}
答案 1 :(得分:1)
正如Andrei在他的回答中指出的那样,你应该在你的无痛脚本中直接提到template<class>
。
这是因为在Lucene索引(构建于ElasticSearch的索引)中没有数组中值的原始顺序概念。此外,arrays do not work as you would expect:
对象数组无法正常工作:您无法查询 每个对象独立于数组中的其他对象。
基本上,您要按列表中的随机数进行排序。
Andrei建议使用_source
来读取原始JSON文档,解析它并从那里提取所需的值(它会起作用)。但是_source
很慢(因为不是每次都从磁盘读取快速索引,而是每个文档)。
您还有其他2个选项:
nested
data type并明确定义订单。希望有所帮助!