最近在使用es进行存储数据,可以说对es有了一个简单的了解,但是也遇到了很多问题。es mapping字段选择的问题,查询语法如何使用等等。。这里就先介绍下es的一些简单常用的查询语法,方便平时复习和查询。
[hadoop@hadoop ~]$ curl -get http://hadoop:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1566912000 21:20:00 my-application yellow
如何让快速了解集群的健康状态? green,yellow,red
green:每个索引的primary shard 和replica shard 都是active状态的
yellow:每个索引的primary shard都是active,但是部分的replica shard不是active 状态,处于不可用状态
red:不是所有的索引的primary shard都是active,部分索引数据有丢失。
注意:
如果电脑只有一个节点,启动es进程就只有一个node,如果创建一个index,默认会给这个index分配5个primary shard 和5个replica shard,而且primary shard 和 replica shard不能在同一个节点上,所以只有一个primary shard
被分配,但是一个replica shard没有第二台机器去启动所以会处于yellow状态。
[hadoop@hadoop ~]$ curl -get http://hadoop:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open ecommerce Jt8sSxp6Qt2hSL0F39FdiQ 5 1 1 0 6kb 6kb
yellow open test_index uZnzDfthQ7af4b0LMgoEGA 5 1 0 0 650b 650b
创建索引:PUT /test_index?pretty
删除索引:DELETE /test_index?pretty
PUT /index/type/id
PUT /ecommerce/product/1
{
"name": "gaolujie yagao",
"desc": "gaolujie meibai",
"price": 30,
"producer": "gaolujie producer",
"tags":["meibai","fangzhu"]
}
PUT /ecommerce/product/2
{
"name": "jiajieshi yagao",
"desc": "meibai",
"price": 25,
"producer": "jiajieshi producer",
"tags":["meibai"]
}
PUT /ecommerce/product/3
{
"name": "c yagao",
"desc": "caoben zhiwu",
"price": 40,
"producer": "zhonghua producer",
"tags":["qingxin"]
}
注意: es会自动建立index和type,不需要提前创建,而且es会默认对document的每个field建立倒排索引,可以被检索
GET /ecommerce/product/1
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "gaolujie yagao",
"desc": "gaolujie meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai"
,
"fangzhu"
]
}
}
PUT /ecommerce/product/1
{
"name": "shengjiban gaolujie yagao",
"desc": "gaolujie meibai",
"price": 30,
"producer": "gaolujie producer",
"tags":["meibai","fangzhu","qingxin"]
}
-------
{
"_index": "ecommerce",
"_type": "product",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "shengjiban gaolujie yagao",
"desc": "gaolujie meibai",
"price": 30,
"producer": "gaolujie producer",
"tags": [
"meibai"
,
"fangzhu"
,
"qingxin"
]
}
}
注意:这种方式不是很好,必须带上所有的field,才能进行信息的修改
POST /ecommerce/product/1/_update
{
"name":"shengjiban gaolujie yagao"
}
DELETE /ecommerce/product/1?pretty
GET /ecmomerce/pruduct/_search
took:耗费几毫秒
time_out:时候是否超时
_shards:数据拆分成5个分片所以对于搜索请求,会打到所有的primary shard
(或者relica shard)
hits.total:查询结果 多少个document
hits.max_score:score的含义就是document对于一个search的相关度的匹配分数,
越相关,分数越高,越匹配
hits.hits:包含了匹配搜索的document详细数据
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
(该种方式了解即可,生产环境基本不会用到,用起来不方便url太长)
GET /ecmomerce/pruduct/_search
{
"query":{"match_all:0"}
}
GET /ecmomerce/pruduct/_search
{
"query":{
"match":{
"name":"yagao"
}
},
"sort":[
{"price":"desc"}
]
}
GET /ecmomerce/pruduct/_search
{
"query":{"match_all":0},
"from":1,
"size":2
}
GET /ecmomerce/pruduct/_search
{
"query":{"match_all":0},
"_source":["name","price"]
}
(常用)
GEt /ecmomerce/pruduct/_search
{
"query":{
"bool":{
"must":{
"name":"yagao"
}
},
"filter":{
"range":{
"price":{"gt":25}
}
}
}
}
GET /ecommerce/product/_search
{
"query":{
"match":{
"priduce":"yagao producer"
}
}
}
yagao produce 会拆两个单词进行匹配
GET /ecmomerce/pruduct/_search
{
"query":{
"match_phrase":{
"produce":"yagao producer"
}
}
}
跟全文搜索相对应,相反,全文搜素会将输入的搜索串拆解开来,去倒排索引进行匹配,只要能匹配上任意一个拆解的单词就可以作为返回结果
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的才可以匹配成功,进行返回。
GET /ecmomerce/pruduct/_search
{
"query":{
"match":{
"producer":"producer"
}
},
"highlight":{
"fields":{
"producer":{}
}
}
}
GET /ecmomerce/pruduct/_search
{
"aggs":{
"group_by_tags":{
"term":{"field":"tags"}}
}
}
}
将本文field 的fielddata属性设置为true
PUT /ecommerce/_mapping/product
{
"properties":{
"tags":{
"type":"text",
"fielddata":true
}
}
}
GET /ecmomerce/pruduct/_search
{
"size":0,
"query":{
"match":{
"name":"yagao"
}
},
"aggs":{
"group_by_tags":{
"term":{"field":"tags"}}
}
}
}
GET /ecmomerce/pruduct/_search
{
"size":0,
"aggs":{
"group_by_tags":{
"terms":{"filed":"tags"},
"aggs":{
"avg_price":{
"avg":{"field":"price"}
}
}
}
}
}
GET /ecmomerce/pruduct/_search
{
"size":0
"aggs":{
"all_tags":{
"terms":{"field":"tags","order":{"avg_price":"desc"}},
"aggs":{
"avg_price":{
"avg":{"field":"price "}
}
}
}
}
}
GET /ecmomerce/pruduct/_search
{
"size":0,
"aggs":{
"group_by_price":{
"range":{
"field":"price",
"ranges":[
{
"from":0,
"to",20
},
{
"from":20,
"to",40
},
{
"from":40,
"to",50
}
]
},
"aggs"{
"group_by_tags":{
"terms":{
"field":"tags"
},
"aggs":{
"average_price":{
"avg":{
"field":"price"
}
}
}
}
}
}
}
}
//TODO