您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

按嵌套文档之一中的值对文档进行排序

按嵌套文档之一中的值对文档进行排序

看起来您构建嵌套过滤器的方式不正确。您在这里列出的内容也不适合我。

但是当我替换这个:

"sort": [
    {
      "children.size": {
        "order": "asc",
        "nested_filter": {
          "nested": {
            "filter": {
              "term": {
                "id": 1
              }
            },
            "path": "children"
          }
        }
      }
    }
]

有了这个:

"sort": [
   {
      "children.size": {
         "order": "desc",
         "nested_filter": {
            "term": {
               "id": 1
            }
         }
      }
   }
]

有效。

更准确地说,我建立了索引并添加了数据:

DELETE /test_index

PUT /test_index/
{
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 0
    }
}

PUT /test_index/item/_mapping
{
   "item": {
      "properties": {
         "name": {
            "type": "string",
            "store": "yes"
         },
         "children": {
            "properties": {
               "name": {
                  "type": "string",
                  "store": "yes"
               },
               "id": {
                  "type": "integer",
                  "store": "yes"
               },
               "size": {
                  "type": "integer",
                  "store": "yes"
               }
            },
            "type": "nested"
         }
      }
   }
}

PUT /test_index/item/1
{"name":"item1","children":[{"id":11,"size":15},{"id":3,"size":6}]}

PUT /test_index/item/2
{"name":"item2","children":[{"id":1,"size":2},{"id":3,"size":6}]}

PUT /test_index/item/3
{"name":"item3","children":[{"id":1,"size":7},{"id":3,"size":36}]}

PUT /test_index/item/4
{"name":"item4","children":[{"id":1,"size":11},{"id":3,"size":16}]}

然后使用进行搜索,如下所示,"order": "desc"它似乎可以正常运行:

POST /test_index/item/_search
{
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "nested": {
               "filter": {
                  "term": {
                     "id": 1
                  }
               },
               "path": "children"
            }
         }
      }
   },
   "sort": [
      {
         "children.size": {
            "order": "desc",
            "mode": "avg",
            "nested_filter": {
               "term": {
                  "id": 1
               }
            }
         }
      }
   ]
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "Failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": null,
      "hits": [
         {
            "_index": "test_index",
            "_type": "item",
            "_id": "4",
            "_score": null,
            "_source": {
               "name": "item4",
               "children": [
                  {
                     "id": 1,
                     "size": 11
                  },
                  {
                     "id": 3,
                     "size": 16
                  }
               ]
            },
            "sort": [
               11
            ]
         },
         {
            "_index": "test_index",
            "_type": "item",
            "_id": "3",
            "_score": null,
            "_source": {
               "name": "item3",
               "children": [
                  {
                     "id": 1,
                     "size": 7
                  },
                  {
                     "id": 3,
                     "size": 36
                  }
               ]
            },
            "sort": [
               7
            ]
         },
         {
            "_index": "test_index",
            "_type": "item",
            "_id": "2",
            "_score": null,
            "_source": {
               "name": "item2",
               "children": [
                  {
                     "id": 1,
                     "size": 2
                  },
                  {
                     "id": 3,
                     "size": 6
                  }
               ]
            },
            "sort": [
               2
            ]
         }
      ]
   }
}

这是我使用的代码

http://sense.qbox.io/gist/1582560ed13bec82dc321944a639336ad7ae6a60

其他 2022/1/1 18:13:40 有649人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶