es 基本语法 使用 案例
2017-07-31 11:46
274 查看
[javascript] view
plain copy
elasticsearch 语法汇总
使用 es 已有段时间 今天 有时间 就把最近使用用到的知识点 记录下来,以备后续使用
es 安装 和插件配置 等知识 这里就不在 累述
1 基本命令 使用
基于 curl 使用 命令
修改 副本信息 因为 index 一旦建成 分片个数就不能修改 只能修改 副本个数
curl -XPUT 'http://localhost:9200/test/_settings' -d '{
"index":{"number_of_replicas" : 0}
}'
同样(注意 mapping 一旦创建 只能新增 不能修改)
基于 DSL 语句
查询 指定 数据
term 完全匹配
{
"query":{
"term":{
"id":1
}
}
}
match 使用
{
"query":{
"match":{
"title":"bmw"
}
}
}
must 使用 必须
{
"query":{
"bool":{
"must":{
"title":"baidu"
}
}
}
}
must should must_not 综合使用 基本
{
"query":{
"bool":{
"must":{
"title":"baidu"
},
"should":{
"name":"cdd"
},
"must_not":{
"age":23
}
}
}
}
must should must_not 综合使用 复杂
{
"query":{
"bool":{
"must":[
{
"age":2
},
{
"name":"cdd"
}
],
"should":[],//一样用法 不在举例
"must_not":[] //一样用法 不在举例
}
}
}
聚合 使用 aggs
max min 简单使用
{
"aggs":{
"max_id":{
"max":{
"field":"id"
}
},
"min_id":{
"min":{
"field":"id"
}
}
}
}
terms 使用 基本
{
"aggs":{
"title":{
"terms":{
"field":"name",
"size":10, // 显示 个数 默认10个 ,0 代表所有 最好根据情况定
}
}
}
}
terms 使用 复杂
{
"aggs":{
"title":{
"terms":{
"field":"name",
"size":10, // 显示 个数 默认10个 ,0 代表所有 最好根据情况定
"order":{
"score":"desc"//asc
}
},
"aggs":{
"score":{
"max":{
"field":"score"
}
}
}
}
}
}
cardinality 去重 使用 统计 title 不同的总个数
{
"aggs":{
"title":{
"cardinality":{
"field":"title"
}
}
}
}
DSL 满足各种情况 更加复杂 的aggs 统计 如何(案例)
{
"size": 0,
"aggs": {
"daterange": {
"filter": {
"range": {
"date": {
"from": "now-2M"
}
}
},
"aggs": {
"publisher": {
"terms": {
"field": "publisher_na",
"size": 30,
"order": {
"ads": "desc"
}
},
"aggs": {
"ads": {
"cardinality": {
"field": "md5"
}
},
"date_num": {
"terms": {
"field": "date_day",
"size": 0,
"order": {
"date_day": "asc"
}
},
"aggs": {
"date_day": {
"max": {
"field": "date_day"
}
}
}
}
}
}
}
}
}
}
更为复杂 和 个别语法 请参考官网
未完 待续
plain copy
elasticsearch 语法汇总
使用 es 已有段时间 今天 有时间 就把最近使用用到的知识点 记录下来,以备后续使用
es 安装 和插件配置 等知识 这里就不在 累述
1 基本命令 使用
基于 curl 使用 命令
修改 副本信息 因为 index 一旦建成 分片个数就不能修改 只能修改 副本个数
curl -XPUT 'http://localhost:9200/test/_settings' -d '{
"index":{"number_of_replicas" : 0}
}'
同样(注意 mapping 一旦创建 只能新增 不能修改)
基于 DSL 语句
查询 指定 数据
term 完全匹配
{
"query":{
"term":{
"id":1
}
}
}
match 使用
{
"query":{
"match":{
"title":"bmw"
}
}
}
must 使用 必须
{
"query":{
"bool":{
"must":{
"title":"baidu"
}
}
}
}
must should must_not 综合使用 基本
{
"query":{
"bool":{
"must":{
"title":"baidu"
},
"should":{
"name":"cdd"
},
"must_not":{
"age":23
}
}
}
}
must should must_not 综合使用 复杂
{
"query":{
"bool":{
"must":[
{
"age":2
},
{
"name":"cdd"
}
],
"should":[],//一样用法 不在举例
"must_not":[] //一样用法 不在举例
}
}
}
聚合 使用 aggs
max min 简单使用
{
"aggs":{
"max_id":{
"max":{
"field":"id"
}
},
"min_id":{
"min":{
"field":"id"
}
}
}
}
terms 使用 基本
{
"aggs":{
"title":{
"terms":{
"field":"name",
"size":10, // 显示 个数 默认10个 ,0 代表所有 最好根据情况定
}
}
}
}
terms 使用 复杂
{
"aggs":{
"title":{
"terms":{
"field":"name",
"size":10, // 显示 个数 默认10个 ,0 代表所有 最好根据情况定
"order":{
"score":"desc"//asc
}
},
"aggs":{
"score":{
"max":{
"field":"score"
}
}
}
}
}
}
cardinality 去重 使用 统计 title 不同的总个数
{
"aggs":{
"title":{
"cardinality":{
"field":"title"
}
}
}
}
DSL 满足各种情况 更加复杂 的aggs 统计 如何(案例)
{
"size": 0,
"aggs": {
"daterange": {
"filter": {
"range": {
"date": {
"from": "now-2M"
}
}
},
"aggs": {
"publisher": {
"terms": {
"field": "publisher_na",
"size": 30,
"order": {
"ads": "desc"
}
},
"aggs": {
"ads": {
"cardinality": {
"field": "md5"
}
},
"date_num": {
"terms": {
"field": "date_day",
"size": 0,
"order": {
"date_day": "asc"
}
},
"aggs": {
"date_day": {
"max": {
"field": "date_day"
}
}
}
}
}
}
}
}
}
}
更为复杂 和 个别语法 请参考官网
未完 待续
相关文章推荐
- es 基本语法 使用 案例
- CodeSmith 使用教程(7): 基本语法-转义Asp.Net标记
- 动态sql语句基本语法(字段名,表名,数据库名之类作为变量时,必须用动态SQL如ALTER TABLE中使用程序传递的参数)
- redis在PHP中的基本使用案例
- 黑马程序员——05C语言语法和mac基本快捷键使用
- C++ 有关string类的基本语法以及一个简单算法 理论加案例的形式
- 详细的KVO总结,包括基本改变,使用案例,注意点.看我就够了!
- 【eoe 特刊】Windows 8 第一期 -- C#基本语法---sqlite数据库的使用---ListView 滚动加载 ---等
- redis在PHP中的基本使用案例
- sql的存储过程使用详解--基本语法
- OC——copy语法的基本使用
- ViewPager的基本使用(案例:图片的无限轮播)
- CodeSmith 使用教程(6): 基本语法-声明和使用属性
- ES 基本使用《三》--terms
- 基于JSP编译器基本语法的使用详解
- Extjs4.x Ext.tree.Panel基本树控件的使用案例、源码
- ES6 基本语法
- 数据库sqlite3的使用-基本语法
- ES 基本使用《一》--分析
- OC——多态、NSString的基本使用、点语法、成员变量作用域