查询数量
[root@localhost ~]# curl -XGET 192.168.0.***:9200/****/_count?pretty
{
"count" : 8406117,
"_shards" : {
"total" : 9,
"successful" : 9,
"failed" : 0
}
}
简单聚合
curl -XGET 192.168.0.***:9200/*****/_search?pretty -d '
{
"size": 0,
"aggs": {
"my_group_by_cxz": {
"terms": {
"field": "cxz"
}
}
}
}'
查看所有库
curl -XGET 192.168.1.*****:9200/_cat/indices?V
查询某库中的mapping:
curl -XGET 192.168.1.****:9200/******/_mapping?pretty
条件查询
返回内容参数 _source 、排序 sort
curl -XGET 192.168.0.****:9200/job/type1/_search?pretty -d
'{
"query": {
"match_all": {}
},
"from": 1,
"size": 2,
"_source": [
"date",
"words"
],
"sort": [
{
"words": "desc"
}
]
}'
返回date、words数据内容 按照words数据降序排序
查询语句 must must_not
{
"filter": {
"bool": {
"must": [
{
"range": {
"hka": {
"gte": 370400,
"lte": 370499
}
}
}
],
"must_not": [
{
"range": {
"regionid": {
"gte": 370000,
"lte": 379999
}
}
}
]
}
},
"_source": [
"iname"
],
"size": 100
}
嵌套查询
{
"query": {
"bool": {
"must": [
{
"nested": {
"inner_hits": {},
"path": "work",
"filter": {
"query": {
"bool": {
"must": [
{
"queryString": {
"query": "",
"fields": [
"work.company"
],
"auto_generate_phrase_queries": true
}
},
{
"term": {
"work.islast": 1
}
}
]
}
}
}
}
},
{
"range": {
"updatetime": {
"gte": 1546272000000
}
}
}
],
"must_not": [
{
"term": {
"shouji": 110
}
}
]
}
},
"size": 4000,
"_source": [
"id",
"iname",
"shouji"
]
}
多重聚合与嵌套
{
"size": 0,
"aggs": {
"per_count": {
"nested": {
"path": "edus"
},
"aggs": {
"zym_term": {
"terms": {
"field": "edus.zym",
"size": 1000
}
}
}
}
},
"query": {
"nested": {
"path": "edus",
"query": {
"bool": {
"must_not": [
{
"terms": {
"edus.zym": [
"",
"不限",
"其他"
]
}
}
]
}
}
}
},
"bool": {
"must": [
{
"range": {
"regionid": {
"gte": 110000,
"lte": 119999
}
}
}
]
}
}
reindex 重建索引
索引的数据结构(mapping)建立后可以增加字段,但不能修改已存在的字段,如果要修改已存在的字段,只能重建索引。
1、新建一个索引,新索引的名称一般使用 原索引名称_重建日期,eg. handlemonitorlog_20210111 ,mapping可以复制原来的mapping来改
2、将数据同步到新索引中
#同步等待方式
POST /_reindex
{
"source": {
"index": "handlemonitorlog"
},
"dest": {
"index": "handlemonitorlog_20210111"
}
}
#如果同步时间过⻓可以使用异步方式。
POST /_reindex?wait_for_completion=false #url中加一个参数即可 wait_for_completion=false
{
"source": {
"index": "handlemonitorlog"
},
"dest": {
"index": "handlemonitorlog_20210111"
}
}
别名的使用
#reindex 重建索引
POST /_reindex
{
"source": {
"index": "handlemonitorlog"
},
"dest": {
"index": "handlemonitorlog1"
}
}
#查询所有的别名
GET /_alias
#查询指定index的别名
GET /handlemonitorlog/_alias
#别名的增删改
#add是新增,remove是删除,更新别名:先remove再add
POST /_aliases
{
"actions": [
{
"remove": {
"index": "handlemonitorlog1",
"alias": "log1"
}
}
,{
"add": {
"index": "handlemonitorlog1",
"alias": "log"
}
}
]
}
refresh 刷新数据
es默认每隔1s刷新1次索引,就是说增删改后可能不会立刻同步,需要等待0~1s
#增删改时,可以在url中添加?refresh,操作后立刻刷新索引
PUT /handlemonitorlog/_doc/1?refresh
{
//......
}
#可以修改自动刷新的默认时间间隔
#默认值1s。-1表示关闭自动刷新,需要手动刷新
PUT /handlemonitorlog/_settings
{
"index": {
"refresh_interval": "5s"
}
}
高亮查询
GET businssstatisticslog/_search
{
"query": {
"bool": {
"must": [
{"match": {
"industry": "其他金融业"
}}
]
}
},
"highlight": {
"fields": {
"*" : {}
},
"require_field_match": "false",
"pre_tags": ["<span style='color:red;'>"],
"post_tags": ["</span>"]
}
}
结果: