2015-06-13 60 views

回答

2

您可以通过sorting在预定义字段_type上实现此目的。下面的查询按文档类型的升序对结果进行排序。

POST <indexname>/_search 
{ 
    "sort": [ 
     { 
     "_type": { 
      "order": "asc" 
     } 
     } 
    ], 
    "query": { 
     <query goes here> 
    } 
} 
+0

这将按字母顺序按类型名称进行排序,但作者希望它按给定顺序排列,例如, _television_,_foo_,_bar_ – wajda

0

我通过添加一个数字字段_is_OF_TYPE到索引的文档并将其设置为1对于那些给定类型的文档做到这一点。然后按照您想要的顺序排序这些字段。

例如:

Document A: 
{ 
    _is_television: 1, 
    ... some television props here ... 
} 

Document B: 
{ 
    _is_television: 1, 
    ... another television props here ... 
} 

Document C: 
{ 
    _is_radio: 1, 
    ... some radio props here ... 
} 

and so on... 
在ElasricSearch查询

然后:

POST radio,television,foo,bar,baz/_search 
{ 
    "sort": [ 
     {"_is_television": {"unmapped_type" : "long"}}, // television goes first 
     {"_is_radio":  {"unmapped_type" : "long"}}, // then radio 
     {"_is_another_type": {"unmapped_type" : "long"}} // ... and so on 
    ] 
} 

这种解决方案的优点是速度。您只需对数字字段进行排序。不需要脚本排序。