2015-02-08 56 views
2

我在基於所選嵌套文檔中的值排序文檔時遇到問題。我使用這樣設置:按嵌套文檔之一的值排序文檔

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文件,就由孩子選擇大小進行排序。所以查詢的樣子:

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),返回的文件都在相同的順序。什麼可能是問題?

回答

1

它看起來像你的結構nested filter是不正確的。你在這裏列出的東西也不適合我。

但是,當我更換此:

"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

+0

謝謝。這很奇怪,ES沒有提出mu查詢的問題。另外,我已經使用ES庫準備了這種排序過濾器,我希望他們能夠準備適當的消息。 – szymond 2015-02-14 09:39:53