• yii2.0 查询elasticsearch常用语句


    整理一下最近用到的yii2.0查询es的语句,总结几个常用的查询语句

    1、不用聚合函数的,直接获取源数据,可以用yii2.0  yiielasticsearchQuery类直接查询

    如:

      public function get_single_index_data(&$index_arr, $table, &$conditons){
            $this->from($index_arr,$table)->where($conditons)->addOrderBy('data_date asc')->limit(10000);
            $command = $this->createCommand();
            $rows = $command->search([],'post');
            $data_arr = $rows['hits']['hits'];
            return $data_arr;
        }
    

     es支持多个数据结构相同的index,type查找,addOrderBy() 相当于es查询的order,limit(10000)相当于es的size:10000,不设置的时候es默认查找10条。

    2、带有聚合函数的

     $first_pay=array(
                "terms" =>array(
                    "field" => "type_i",
                    "size"=>10000,
                    "order"=>array("_term"=>"asc")
                ),
                "aggregations" => array(
                    "total_num"=>array(
                        "sum" =>array(
                            "field"=>"num_l"
                        )
                    )
                )
            );
            $this->aggregations=array();
            $this->from($index_arr_new_first_pay,$table)->where($condition)->addAggregate('first',$first_pay)->limit(0);
            $command_first = $this->createCommand();
            $rows_first = $command_first->search([],'post');
            $first_pay_result = $rows_first['aggregations']['first']['buckets'];
    

      yii中有一个函数addAggregate(),可以添加es中的聚合函数,terms相当于mysql中的group by关键字,上面就是求同一个类型的和。同理,除了sum关键字,还可以求max,min,avg,有一个关键字stats可以同时求出sum,min,max,avg值。内es内置排序"order"=>array("_term"=>"asc"),是按词项的字符串值的字母顺序排序,也可以按 doc_count 值的升序排序,是"order"=>array("_count",=>"asc")。

    引用文档上面的,内置排序有以下几种:

    _count按文档数排序。对 terms 、 histogram 、 date_histogram 有效。

    _term按词项的字符串值的字母顺序排序。只在 terms 内使用。

    _key按每个桶的键值数值排序(理论上与 _term 类似)。 只在 histogram 和 date_histogram 内使用。

    除了聚合时候用到的sum,min,max,avg外,有时候还需要把其他字段展示出来。用到了一个top_hits。引用文档的例子:

     "top_hits"=>array(
      "sort"=>array(
        "date"=>array(
          "order"=>"desc"
        )
      ),
      "_source"=>array(
        "includes"=>["date","price"]
      ),
      "size"=>1,
    )

      top_hit按照排序的数据取一条,并且把源数据取出来,包括的字段是date和price字段。

  • 相关阅读:
    jmeter 创建
    MySQL远程连接不上的解决方法
    删除文件及文件夹
    wsdl使用方式
    solr查询参数过长问题解决
    Inno Setup添加自定义页面
    java中日期的加减,比较,以及与String的互相转换
    vue项目引入第三方js
    vue覆盖elementui样式的几种方式
    SQL Server 连接表内部查询中的逗号分隔字符串
  • 原文地址:https://www.cnblogs.com/angellating/p/7250404.html
Copyright © 2020-2023  润新知