2013-06-01 122 views
4

我使用elasticsearch构建了一个自动完成搜索,因此我需要查询3个索引postscommentsauthors。我有以下查询:Elasticsearch - 对多索引查询进行排序

{ 
    "query":{ 
     "query_string":{ 
     "query":"something" 
     } 
    } 
} 

电话:

curl -X GET 'http://localhost:9200/posts,comments,authors/_search?pretty' -d '{ 
    "query": { 
    "query_string": { 
     "query": "something" 
    } 
    } 
}' 

我需要通过特定的索引字段对结果进行排序,例如:

帖子指数有一个字段名为comments_count评论votes_count和作者posts_count。当比较帖子时,则应按comments_count排序,当评论时votes_count,当时作者为posts_count

可以这样做吗?我不想将索引合并为一个索引,因为它们索引完全不同的文档。

回答

2

那么,我的问题是,如果我排序的字段不存在于所有索引中,文档将不会被返回。

管理与解决:

{ 
    "_script":{ 
     "script":"type=doc['_type'].value;if(type=='posts'){return doc['comments_count'].value} else {return 0};", 
     "type":"number", 
     "order":"desc" 
    } 
} 

至少现在的文件显示,即使在底部。

+0

“如果我排序所有索引中不存在的字段,文档将不会返回”。这是可控的。参见http://www.elasticsearch.org/guide/reference/api/search/sort/。在你的情况下,你应该使用'{“missing”:“_last”}' –

0

而不是使用comments_countvotes_countposts_count的,你可以投入指数相同的名称(如items_count)和执行通常的排序:

{ 
    "sort": [ 
    { "items_count": "desc" } 
    ] 
} 

如果有缺乏items_count其他实体有一个选项,以ignore unmapped fields

{ 
    "sort": [ 
    { "items_count": { "order": "desc", "unmapped_type": "long" } } 
    ] 
} 
相关问题