ElasticSearch DSL 详解


在学习之前首先你要弄懂下面这条sql的每个部分的意义,以及它能干什么?因为这条sql就是我们这次内容的提纲。

SELECT 
	FIELD1,  
	AVG(FIELD2) AS AVG_VAL
 FROM 
 	TABLE 
 WHERE 
 	FIELD1='ELASTICSEARCH' 
 GROUP BY 
 	FIELD1 
 HAVING 
 	AVG_VAL>20 
 ORDER BY 
 	FIELD1 desc
 LIMIT 0,10

学前必读

1 基本概念

  1. 索引
    相当于关系型数据库中的数据库
  2. 类型
    相当于关系型数据库中的数据表,但新版本的ES中,淡化了类型的概念,索引和类型从1对多的关系变成了一对一的关系,
  3. 数据结构
    基本数据结构:text, keyword, date, long, double, boolean,ip
    json数据结构:object, nested
    专用数据结构:geo_point, geo_shape, completion
  4. 查询api
    单条
    GET /index/type/id
    多条
    GET/POST /index/type/_search
  5. 查询语句
    json就是es的空气!
GET /analysis/_search
{
  "_source": {   ---SELECT
    "includes": ["fileName","starttime","duration","repNo","repGroup","typeOfService"],
    "excludes": ["blankKeyword","keyword","topicHitDetail"]
  }, 
  "query": {   ---WHERE
    "bool": {
      "filter": {
        "term": {
          "typeOfService": "转账"
        }
      }
    }
  },
  "aggs": {  ---GROUP BY
    "class_buckets": {  ---HAVING
      "filter": {
        "range": {
          "duration": {
            "gte": 600
          }
        }
      },
      "aggs": {
        "class_count": {
          "terms": {
            "field": "classfication_f"
          },
          "aggs": {
            "count": {
              "value_count": {
                "field": "classfication_f"
              }
            },
            "count_filter":{
              "bucket_selector": { ------HAVING
                "buckets_path": {
                  "count":"count"
                },
                "script": "params.count>=1000"
              }
            }
          }
        }
      }
    }
  },
  "from": 0, ---LIMIT
  "size": 10, 
  "sort": [    ---ORDER BY
    {
      "starttime": {
        "order": "desc"
      }
    }
  ]
}

SELECT

以JSON数组格式,传入需要(不需要)的字段。

  1. 指定需要的字段

“_source”: [“fileName”,“starttime”,“duration”,“repNo”,“repGroup”,“typeOfService”]

  1. 指定不需要的字段

“_source”: {
“excludes”: [“blankKeyword”,“keyword”,“topicHitDetail”]
}

  1. 即指定需要的也指定不需要的

“_source”: {
“includes”: [“fileName”,“starttime”,“duration”,“repNo”,“repGroup”,“typeOfService”],
“excludes”: [“blankKeyword”,“keyword”,“topicHitDetail”]
}

WHERE

logical operator

逻辑关系对应关键字
must, filter
should
must_not

它们之间可以通过bool关键子互相组合 e.g.

"query": {
    "bool": {
      "must": [
        {}
      ], 
      "must_not": [
        {}
      ],
      "should": [
        {"bool": {
          "must": [
            {"term": {
              "callNumber": {
                "value": "95533"
              }
            }}
          ]
        }}
      ], 
      "filter": [
        { "term": {
          "typeOfService": "转账"
          }
        }
      ]
    }
  }

filter

filter名称说明
Exists是否存在
IdsID
Prefix前缀匹配
Range范围
Regexp正则
Script脚本
Term单值精确匹配
Terms多值精确匹配
WildCard通配符

更多查询方法

GROUP BY

metric

聚合函数描述
avg平均值
min最小值
max最大值
sum求和
value_count值的数量
cardinality不重复值的数量
percentile百分位数
stats状态

buckets

分组类型描述
terms按字段分组
histogram按指定数值间隔分组
date_histogram按指定日期间隔分组
range按区间分组

nested

聚合nested结构

ElasticSearch 的聚合

HAVING

aggs filter

ORDER BY

查询的排序

"sort": [ 
    {
      "starttime": {
        "order": "desc"
      }
    }
  ]

聚合的排序

"order": [
{"_key": "asc"}
]

LIMIT

因为查询性能,通过limit方式并不能得到全部分页,elasticsearch默认配置,可以通过,分页查询到10000条的数据,这个阈值可以修改。如果需要的到全部查询结果,通过scrollAPI进行获取。

案例分析

1、查询
2、聚合
2.1简单分桶
2.2按固定日期间隔分桶
2.3按时段分桶
2.4子聚合查询
2.4度量聚合
重点讲 cardinality和percentile
2.5nested聚合
3.推荐算法
搜索推荐
seach-time推荐相关阅读
索引推荐
index-time推荐相关阅读
3.评分算法
TF/IDF
空间向量模型

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TBicycle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值