在一个嵌套文档中按值排序文档 [英] Sorting documents by value in one of nested documents

查看:100
本文介绍了在一个嵌套文档中按值排序文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据选定的嵌套文档中的值排序文档时遇到问题。我正在使用这样的设置:

I am having problems with sorting documents based on value in selected nested document. I am using such setup:

curl -XPUT 'http://127.0.0.1:9200/test/' -d '
index :
    number_of_shards : 1
    number_of_replicas : 1
'


curl -XPUT '127.0.0.1:9200/test/item/_mapping' -d '
{
"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"
  }
 }
}
}' 


curl -XPUT 'http://localhost:9200/test/item/1' -d '{
    "name" : "item1",
    "children": [
      {
        "id": 11,
        "size": 15
      }, 
      {
        "id":3,
        "size": 6
      }
     ]
    }
}'
curl -XPUT 'http://localhost:9200/test/item/2' -d '{
    "name" : "item2",
    "children": [
      {
        "id": 1,
        "size": 2
      }, 
      {
        "id":3,
        "size": 6
      }
     ]
    }
}'
curl -XPUT 'http://localhost:9200/test/item/3' -d '{
    "name" : "item3",
    "children": [
      {
        "id": 1,
        "size": 7
      }, 
      {
        "id":3,
        "size": 36
      }
     ]
    }
}'
curl -XPUT 'http://localhost:9200/test/item/4' -d '{
    "name" : "item4",
    "children": [
      {
        "id": 1,
        "size": 11
      }, 
      {
        "id":3,
        "size": 16
      }
     ]
    }
}'

我要检索的是具有选定子项ID的文档,这些文档将按选定的子项大小排序。所以查询看起来像:

What I am trying to retrieve are documents with selected children id which would be sorted by selected children size. So the query looks like:

    curl -XGET 'http://127.0.0.1:9200/test/item/_search?pretty=1'  -d '
{
  "query": {
    "filtered": {
      "query": {
        "match_all": {}
      },
      "filter": {
        "nested": {
          "filter": {
            "term": {
              "id": 1
            }
          },
          "path": "children"
        }
      }
    }
  },
  "sort": [
    {
      "children.size": {
        "order": "asc",
        "nested_filter": {
          "nested": {
            "filter": {
              "term": {
                "id": 1
              }
            },
            "path": "children"
          }
        }
      }
    }
  ]
}
'

在这个查询中,无论我输入order字段(asc或desc),返回的d眼睛是相同的顺序。什么可能是一个问题?

In this query no matter what I put into "order" field (asc or desc), the returned documents are in same order. What might be a problem?

推荐答案

看起来像你的方式结构化你的嵌套过滤器是不正确的。你在这里列出的内容对我来说也不行。

It looks like you the way you've structured your nested filter isn't correct. What you have listed here didn't work for me either.

但是当我取而代之的是:

But when I replaced this:

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

这个:

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

它的工作。

更确切地说,我构建了索引并添加了您的数据:

More precisely, I built the index and added your data:

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 ,并且似乎按预期工作:

Then searched as follows, with "order": "desc", and it seems to work as expected:

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

这篇关于在一个嵌套文档中按值排序文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆