沙师弟,师父的充电器掉了

elasticsearch使用bucket_script查询的结果如何排序

Elasticsearch | 作者 qiqi18 | 发布于2015年12月25日 | 阅读数:11691

聚合中使用脚本查询语句如下:
{
  "aggregations" : {
    "domain" : {
      "terms" : {
        "field" : "domain"
      },
      "aggregations" : {
        "totalVist" : {
          "sum" : {
            "field" : "visit_count"
          }
        },
        "totalHit" : {
          "sum" : {
            "field" : "hit_count"
          }
        },
        "percentage" : {
          "bucket_script" : {
            "script" : {
              "inline" : "tHit / tVisit"
            },
            "buckets_path" : {
              "tVisit" : "totalVist",
              "tHit" : "totalHit"
            }
          }
        }
      }
    }
  }
}
查询出得到的聚合结果如下:
"aggregations" : {
    "domain" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [ {
        "key" : "www.youku.com",
        "doc_count" : 2,
        "totalVist" : {
          "value" : 300.0
        },
        "totalHit" : {
          "value" : 150.0
        },
        "percentage" : {
          "value" : 0.5
        }
      }, {
        "key" : "www.jd.com",
        "doc_count" : 1,
        "totalVist" : {
          "value" : 300.0
        },
        "totalHit" : {
          "value" : 50.0
        },
        "percentage" : {
          "value" : 0.16666666666666666
        }
      }, {
        "key" : "www.qq.com",
        "doc_count" : 1,
        "totalVist" : {
          "value" : 200.0
        },
        "totalHit" : {
          "value" : 10.0
        },
        "percentage" : {
          "value" : 0.05
        }
      }, {
        "key" : "www.sina.com",
        "doc_count" : 1,
        "totalVist" : {
          "value" : 200.0
        },
        "totalHit" : {
          "value" : 50.0
        },
        "percentage" : {
          "value" : 0.25
        }
      } ]
    }
  }
现在我想查询的时候执行percentage 值排序,es排序API好像都只能对字段进行排序,脚本查询出的结果无法排序。有大神对这个排序有研究的吗?还是es 只能对字段排序?
 
已邀请:

三斗室 - ELK

赞同来自:

https://www.elastic.co/guide/e ... .html  
{
"aggs" : {
"countries" : {
"terms" : {
"field" : "address.country",
"order" : { "females>height_stats.avg" : "desc" }
},
"aggs" : {
"females" : {
"filter" : { "term" : { "gender" : "female" }},
"aggs" : {
"height_stats" : { "stats" : { "field" : "height" }}
}
}
}
}
}
}

qiqi18 - 80后攻城狮

赞同来自:

这个排序也是对字段的啊,我的那个是先聚合两个字段visit_count ,hit_count,然后求出 tHit / tVisit 的值,es 里面存数据是这样的
 字段: domian(域名)   visit_count(访问数)   hit_count(连接成功数)
 值:  www.qq.com  100          50
      www.qq.com  200          160
     www.163.com  500          300
     www.jd.com  800          740
我现在是要按域名统计访问成功率,按成功率进行排序。
     

三斗室 - ELK

赞同来自:

我贴的官方示例不是已经按照avg aggregation的值来排序了么?无非是你用pipeline aggregation的值来排序啊。

qiqi18 - 80后攻城狮

赞同来自:

能详细点不,这个如何增加排序percentage的值{
  "aggregations" : {
    "domain" : {
      "terms" : {
        "field" : "domain"
      },
      "aggregations" : {
        "totalVist" : {
          "sum" : {
            "field" : "visit_count"
          }
        },
        "totalHit" : {
          "sum" : {
            "field" : "hit_count"
          }
        },
        "percentage" : {
          "bucket_script" : {
            "script" : {
              "inline" : "tHit / tVisit"
            },
            "buckets_path" : {
              "tVisit" : "totalVist",
              "tHit" : "totalHit"
            }
          }
        }
      }
    }
  }
}
官网没看到有说明Bucket Script Aggregation 的排序。

redhat

赞同来自:

  有考虑  "tVisit" : "totalVist", totalVisit值为0的情况吗

zpzkit

赞同来自:

官方文档明确说明了pipeline不支持排序:链接地址
Pipeline aggs cannot be used for sorting
Pipeline aggregations are run during the reduce phase after all other aggregations have already completed. For this reason, they cannot be used for ordering.

 
 

要回复问题请先登录注册