看,灰机...

elasticsearch搜索结果返回特定数据

Elasticsearch | 作者 ccsy | 发布于2019年01月03日 | 阅读数:2369

mapping是这样的
{
"mappings": {
"blogpost": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"name": { "type": "string" },
"comment": { "type": "string" },
"age": { "type": "short" },
"stars": { "type": "short" },
"date": { "type": "date" }
}
}
}
}
}
}

搜索条件:
{
"query": {
"nested": {
"path": "comments",
"filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
},
"sort": {
"comments.stars": {
"order": "asc",
"mode": "min",
"nested_path": "comments",
"nested_filter": {
"range": {
"comments.date": {
"gte": "2014-10-01",
"lt": "2014-11-01"
}
}
}
}
}
}

这个搜索是搜索指定时间内的文章,并且根据文章评论数量的最小值进行asc排序,那搜素结果中除了返回_source,可以把最小的这个评论数量也一块返回吗
已邀请:

rochy - rochy_he

赞同来自:

如果只想获取评论数量,那么再 SearchHit 的 Source 里面即可获取到;
楼主应该是希望, 最后的得分是 _score 和 评论数量两者共同作用的结果吧
 
这个可以使用 function_score_query 来实现,具体可参考案例:
GET /_search
{
"query": {
"function_score": {
"query": {
"match": { "message": "elasticsearch" }
},
"script_score" : {
"script" : {
"source": "Math.log(2 + doc['likes'].value)"
}
}
}
}
}
官方文档:https://www.elastic.co/guide/e ... .html

要回复问题请先登录注册