Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 按嵌套文档之一中的值对文档进行排序_Sorting_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Sorting,elasticsearch" /> elasticsearch,Sorting,elasticsearch" />

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

Sorting 按嵌套文档之一中的值对文档进行排序,sorting,elasticsearch,Sorting,elasticsearch,我在根据选定嵌套文档中的值对文档进行排序时遇到问题。我正在使用这样的设置: 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"

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

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的文档,这些子id将按选定子大小排序。因此,查询如下所示:

    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"
          }
        }
      }
    }
  ]
}
'

在这个查询中,无论我在“订单”字段(asc或desc)中输入什么,返回的文档都是按相同的顺序排列的。可能有什么问题?

您的组织方式似乎不正确。你在这里列出的东西对我也不起作用

但当我替换这个时:

"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
            ]
         }
      ]
   }
}
以下是我使用的代码: