# 对index的操作
根据shards以及replicas数目创建index
PUT /test_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}
根据上述的命令,在test_index这个index中,会创建3个primary shard,3个replica shard。
常见查询方式
常见的查询es中数据的方式有如下几种:
- query string search
- query DSL
- query filter
- full-text search(全文检索)
- parase search(短语搜索)
- 高亮搜索结果
- 批量查询
- multi-index和multi-type搜索模式
query string search
样例1:搜索name字段为yg,并且价格price降序排序
GET ecommerce/product/_search?q=name:yg&sort=price:desc
样例2:搜索temperature字段不为temp10的记录
GET sensor_index/sensor_type/_search?q=-temperature:temp10
q=xx、 q=+xx、q=-xx 三者的区别 :q=xx 与 q=+xx 功能一致,q=-xx ds表示不包含当前条件的记录。
样例3:搜索任意field值为temp10的记录
GET sensor_index/sensor_type/_search?q=temp1
query DSL
样例1:查询所有商品
GET ecommerce/product/_search
{
"query": {
"match_all": {}
}
}
样例2:查询name包含yg的商品,同事按价格price降序排序
GET ecommerce/product/_search
{
"query": {
"match": {
"name": "yg"
}
},
"sort": [
{
"price": {
"order": "desc"
}
}
]
}
样例3:分页查询商品,从第几个数开始读,共读多少个数
GET /ti/tt/_search
{
"query": {
"match_all": {}
},
"size": 2,
"from": 1
}
size:表示读取多少个数
from:表示从第几个数的下标开始读(es中第一个数下标是0)
样例4:指定查询出来所有商品的名称和价格,其他字段不需要
GET ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"]
}
query filter
样例1: 搜索商品名称name包含yg,而且售价大于20的商品
GET ecommerce/product/_search
{
"query": {
"bool": {
"must": [
{"match": {
"name": "yg"
}}
],
"filter": {
"range": {
"price": {
"gt": 20
}
}
}
}
}
}
full-text search(全文检索)
GET ecommerce/product/_search
{
"query": {
"match": {
"producer": "yg p"
}
}
}
producer字段值,会被拆分为两个值(yg、p),然后去es中查询,按照匹配情况(score)从高到低显示
parase search(短语搜索)
跟全文检索相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面一一匹配,只要能匹配上一个拆解后的单词,就可以作为结果返回。phrase search要求输入的搜索串必须在指定的字段文本中,完全包含一模一样的,才能作为结果返回。
GET ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "yg p"
}
}
}
高亮搜索结果
producer字段高亮显示。
GET ecommerce/product/_search
{
"query": {
"match": {
"producer": "p"
}
},
"highlight": {
"fields": {
"producer": {}
}
}
}
输出结果:
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "glj yg",
"desc": "yxfz",
"price": 18,
"producer": "glj p",
"tags": [
"fz"
]
},
"highlight": {
"producer": [
"glj <em>p</em>"
]
}
}
聚合样例
在进行聚合分析的时候,需要对指定的字段属性进行如下设置:
PUT ecommerce/_mapping/product
"properties": {
"tags": {
"type": "text",
"fielddata": true
}
}
}
样例1:计算每一个tags(字段名)下的商品数量
GET ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
size 为 0,说明返回的结果中,并不会显示原始数据,只会显示聚合分析后的数据;同时,group_by_tags 表示聚合操作的名称,为用户指定。
样例2:对名称name中包含yg的商品,计算每个tag下的商品数量
GET ecommerce/product/_search
{
"size": 0,
"query": {
"match": {
"name": "yg"
}
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
样例3:先分组,再算每组的平均值,计算每个tag下的商品的平均价格
GET ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
在每个分组的基础上,使用avg获得平均价格
样例4:先分组,再算每组的平均值,计算每个tag下的商品的平均价格,然后按照价格降序排序
GET ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
样例5:按照指定的价格范围区间进行分组,然后再每组内在按照tags进行分组,最后在计算每组的平均价格
GET ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 10
},
{
"from": 10,
"to": 20
},
{
"from": 20,
"to": 100
}
]
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
批量查询
数据准备:(mget)
两个index:index1、index2
每个index下都有两个type,index1下的type有type11、type12,index2下的type有type21、type22。
每个index、type下面都有三个document,id分别为1、2、3。
样例1:批量查询指定index、type、id的document
样例2:查询的document是一个index下不同type
样例3:查询的数据在同一个index下的同一个type的不同id
multi-index和multi-type搜索模式
/_search : 所有索引,所有type下的所有数据都搜索出来
/index1/_search : 指定一个index,搜索其下所有type的数据
/index1,index2/_search : 同时搜索两个index下的数据
/*1,*2/_search : 按照通配符去匹配多个索引(以1结尾,以2结尾的index)
/index1/type1/_search :搜索一个index下指定的type的数据
/index1/type1,type2/_search :可以搜索一个index下多个type的数据
/index1,index2/type1,type2/_search :搜索多个index下的多个type的数据
/_all/type1,type2/_search :_all可以代表搜索所有index下的指定type的数据