Elasticsearch之Nested Sorting

                                                    

                                                      (这是一个小系列:请戳:ElasticsearchNested(嵌套)系列,查看其他nested相关文章)


         It is possible to sort by the value of a nested field, even though the value exists in a separate nested document. To make the result 

more interesting, we will add another record:

  尽管存在于独立的nested文本内,基于nested字段的值排序还是可行的。为了让结果更有意思,让我们增加其他的记录:

curl -XPUT 'localhost:9200/my_index/blog/2' -d '
{
  "title": "Investment secrets",
  "body":  "What they don't tell you ...",
  "tags":  [ "shares", "equities" ],
  "comments": [
    {
      "name":    "Mary Brown",
      "comment": "Lies, lies, lies",
      "age":     42,
      "stars":   1,
      "date":    "2014-10-18"
    },
    {
      "name":    "John Smith",
      "comment": "You're making it up!",
      "age":     28,
      "stars":   2,
      "date":    "2014-10-16"
    }
  ]
}

             Imagine that we want to retrieve blog posts that received comments in October, ordered by the lowest number of stars that each blog

 post received. The search request would look like this:

设想我们想要检索在10月份被评论的博客文章,同时按每篇文章收到的最低星级排序。检索请求应该类似如下:

curl -XPUT 'localhost:9200/_search' -d '
{
  "query":{
     "nested":{
	    "path":"comments",
		"filter":{
		   "range":{
		      "comments.date":{
			      "gte":"2014-10-01",
				  "lt":"2014-11-01"
			  }
		   }
		}
	 }
  },
  "sort":{
     "comments.stars":{
	     "order":"asc",
		 "mode": "min",
		 "nested_filter":{
		     "range":{
			    "comments.date":{
				   "gte":"2014-10-01",
				   "lt":"2014-11-01"
				}
			 }
		 }
	 }
  }
}

           Why do we need to repeat the query conditions in the nested_filter? The reason is that sorting happens after the query has been executed. 

The query matches blog posts that received comments in October, but it returns blog post documents as the result. If we didn’t include the 

nested_filter clause, we would end up sorting based on any comments that the blog post has ever received, not just those received in October.

         为什么我们要在nested_filter内重复查询条件?原因是排序发生在查询执行结束后。查询匹配10月收到评论的博客文章,但是它返回博客文章

文本作为结果。如果我们不包括nested_filter语块,那么最终会基于返回的博客文章的所有评论进行排序,而不是在10月份收到的评分。

(!!!根据自己有限的elasticsearch使用经验,这点非常值得各位读者重视!!!)


原文:http://www.elastic.co/guide/en/elasticsearch/guide/current/nested-sorting.html



  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值