我想根据文档上的嵌套属性对文档进行排序。假设我有以下文档:
{
"id": 1,
"attributes": [
{
"name": "a",
"value": 1
},
{
"name": "b",
"value": 2
}
]
}
{
"id": 2,
"attributes": [
{
"name": "a",
"value": 2
},
{
"name": "b",
"value": 2
}
]
}
{
"id": 3,
"attributes": [
{
"name": "b",
"value": 1
}
]
}排序应该比较相同属性的值,所以它应该首先比较属性"a“的值。
如果组"a“的值相同,则应该继续比较属性"b”。
如果整个组都丢失了,则缺少该属性的文档将丢失比较。
属性可以具有任何名称和值,因此事先不知道属性名称。
我已经在我的客户端应用程序中编写了这个算法,我只需要一种使用ElasticSearch来实现的方法。
发布于 2016-04-14 08:23:02
您可以尝试在两个查询中执行此操作。下面的内容都没有经过测试,只是为了给你一个方向去考虑。
第一个查询是一个聚合,以获得不同的attribute.name列表
第二个包含实际的查询,类似于下面的sort (您必须根据上面的agg的结果构建这个排序)
{
"query": {"match_all": {}},
"sort": [
{
"attributes.value": {
"nested_path": "attributes",
"nested_filter": {"term": {"attributes.name": "a"}}
}
},
{
"attributes.value": {
"nested_path": "attributes",
"nested_filter": {"term": {"attributes.name": "b"}}
}
},
...
]
}https://stackoverflow.com/questions/35476996
复制相似问题