我在索引文档中有以下结构:
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"搜索属于给定列表的匹配文档。但我需要根据该列表中的位置值对文档进行排序,并忽略其他列表的位置。
因此预期结果将按document2,document1,document3
的顺序排列数据结构是否适合这种排序以及如何处理?
答案 0 :(得分:4)
实现此目的的一种方法是将customLists
的映射类型设置为nested,然后按nested fields排序
示例:
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"
}
}
}
}
}
]
}