Sorting Elasticsearch通过匹配数组项进行排序
我在索引文档中有以下结构:Sorting Elasticsearch通过匹配数组项进行排序,sorting,elasticsearch,Sorting,elasticsearch,我在索引文档中有以下结构: document1: "customLists":[{"id":8,"position":8},{"id":26,"position":2}] document2: "customLists":[{"id":26,"position":1}] document3: "customLists":[{"id":8,"position":1},{"id":26,"position":3}] 我能够使用匹配查询“customLists.id=26”搜索属于给定列表的匹配文档
document1: "customLists":[{"id":8,"position":8},{"id":26,"position":2}]
document2: "customLists":[{"id":26,"position":1}]
document3: "customLists":[{"id":8,"position":1},{"id":26,"position":3}]
我能够使用匹配查询“customLists.id=26”搜索属于给定列表的匹配文档。但是我需要根据列表中的位置值对文档进行排序,并忽略其他列表的位置
因此,预期结果将按文档2、文档1、文档3的顺序排列
数据结构是否适合这种排序以及如何处理?实现这一点的一种方法是将
自定义列表的映射类型设置为,然后使用排序方式
例如:
1)创建索引和映射
put test
put test/test/_mapping
{
"properties": {
"customLists": {
"type": "nested",
"properties": {
"id": {
"type": "integer"
},
"position": {
"type": "integer"
}
}
}
}
}
2)索引文档:
put test/test/1
{
"customLists":[{"id":8,"position":8},{"id":26,"position":2}]
}
put test/test/2
{
"customLists":[{"id":26,"position":1}]
}
put test/test/3
{
"customLists":[{"id":8,"position":1},{"id":26,"position":3}]
}
3)根据给定id的位置进行排序的查询
post test/_search
{
"filter": {
"nested": {
"path": "customLists",
"query": {
"term": {
"customLists.id": {
"value": "26"
}
}
}
}
},
"sort": [
{
"customLists.position": {
"order": "asc",
"mode": "min",
"nested_filter": {
"term": {
"customLists.id": {
"value": "26"
}
}
}
}
}
]
}
忽略其他列表的位置是什么意思。
在您的问题中是什么意思?如果我使用CustomList.id=26搜索,我只对该项中的位置值感兴趣{“id”:26,“position”:5}无法理解您。请共享您想要的示例输出?