实现此目的的一种方法是将的映射类型设置
customLists为嵌套,然后使用按嵌套字段排序
范例:
1)创建索引和映射
put testput 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"} } } } } ] }


