Chinaunix首页 | 论坛 | 博客
  • 博客访问: 715752
  • 博文数量: 26
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 3182
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 14:29
个人简介

7年游戏服务器开发,擅长c/c++,javesript,php;熟悉linux,mysql/redis,elasticsearch;开源爱好者.github : https://github.com/yuyunliuhen

文章分类

全部博文(26)

文章存档

2016年(1)

2015年(3)

2014年(3)

2013年(19)

分类: 大数据

2015-12-04 18:19:51

     es进行聚合查询时,需要对结果进行排序,默认是对聚合结果的数量排序的,难以满足需求,可以通过嵌套的聚合解决,部分代码如下:

点击(此处)折叠或打开

  1. {
  2.             "track_scores":true,
  3.             "from":0,
  4.             "size":10,
  5.             "min_score": 0.2,
  6.             "query" : {
  7.                 "filtered":{
  8.                     "query":{
  9.                         "query_string":{
  10.                             "query": "'.$keyword.'",
  11.                             "fields":["name1","name2^3","name3^3"]
  12.                         }
  13.                     }
  14.                 }
  15.             },
  16.             "aggs":
  17.             {
  18.                 "id1s":{
  19.                     "terms":{
  20.                         "field": "id1",
  21.                         "size":15,
  22.                         "order":{"max_score":"desc"}
  23.                     },
  24.                     "aggs":{
  25.                         "max_score":{
  26.                             "max":{
  27.                                 "script": "doc.rank"
  28.                             }
  29.                         }
  30.                     }
  31.                 },
  32.                 "id2s":{
  33.                     "terms":{
  34.                         "field": "id2",
  35.                         "size":10,
  36.                         "order":{"max_score":"desc"}
  37.                     },
  38.                     "aggs":{
  39.                         "max_score":{
  40.                             "max":{
  41.                             "script": "doc.rank"
  42.                             }
  43.                         }
  44.                     }
  45.                 }
  46.             },
  47.             "sort": [
  48.                 { "rank": { "order": "desc" }},
  49.                 { "_score": { "order": "desc" }}
  50.             ]
  51.         }
     因使用了script,会出现   “nested: ScriptException[dynamic scripting for [groovy] disabled]; }],[status] => 400,”,需要动态脚本支持:
在配置文件elasticsearch.yml文件末尾加上  “script.groovy.sandbox.enabled: true” 即可。

    reference:
        https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
        


阅读(3682) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~