一、索引维护:
----------------------------------------------------
1、查看所有索引
GET /_cat/indices
2、查看某个索引信息
GET myindex
3、创建索引(并且指定分片数量)
PUT tb_car
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
}
}
或者直接创建
PUT tb_car
4、创建索引(新增生成自定义ID)
PUT myindex/_doc/1
{
"name":"测试1",
"opercation":1,
"location":"ad1,ad2,ad3"
}
5、创建索引(自动生成ID)
POST myindex/_doc
{
"name":"test2"
}
6、批量插入数据
POST _bulk
{"index":{"_index":"tb_user","_id":1}}
{"user":"张三","addres":"广州"}
{"index":{"_index":"tb_user","_id":2}}
{"user":"李四","addres":"北京"}
或者设置字段类型
POST /tb_car/_mapping/_doc
{
"properties": {
"goodsId": {
"type": "long"
},
"subTitle": {
"type": "keyword"
}
}
7、批量查询多个索引
GET _mget
{
"docs": [
{
"_index": "myindex",
"_id": 1
},
{
"_index": "test3",
"_id": 1
}
]
}
8、查询index中多行数据
GET myindex/_doc/_mget
{
"ids":["1","2"]
}
9、给索引添加字段
POST myindex/_doc/_update
{
"address":"广州市天河区"
}
10、更新索引某条数据值
POST myindex/_update/2
{
"doc":{
"age":40
}
}
11、更新字段值(根据条件查询后更新)
POST myindex/_update_by_query
{
"script": {
"source": "ctx._source.name=params.name;ctx._source.city=params.city",
"long": "painless",
"params": {
"name": "test-name",
"city": "北京"
},
"query": {
"match": {
"age": 40
}
}
}
}
12)、索引的关闭和打开
POST tb_user/_close
POST tb_user/_open
13)、删除索引
DELETE testindex
DELETE myindex/_doc/2
删除(根据查询条件进行删除)
POST myindex/_delete_by_query
{
"query":{
"match":{
"name":"张三"
}
}
}
一、索引搜索:
----------------------------------------------------
1)、查询记录是否存在
HEAD myindex/_doc/1
2)、查询数量
GET tb_user/_count
3)、查询最新数据记录(默认10条)
GET tb_user/_search
4)、查询指定字段
GET tb_user/_search
{
"_source": ["user","addres"]
}
5)、查询最新1条数据记录
GET tb_user/_search?size=1
6)、搜索(分页)
GET tb_user/_search
{
"size": 20,
"from": 1
}
GET tb_user/_search
{
"size": 20,
"from": 1,
"query": {
"match_all": {}
}
}
7)、搜索(根据条件分页)
GET tb_user/_search
{
"query": {
"match": {
"user": "李四"
}
}
}
8)、搜索(根据条件查询[全词匹配])
GET tb_user/_search
{
"query": {
"match": {
"user.keyword": "李四"
}
}
}
9)、搜索(复合查询:根据多个条件(must and must查询))
GET tb_user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"addres": "广州"
}
},
{
"match": {
"user": "test1"
}
}
]
}
}
}
10)、搜索(复合查询:根据多个条件(must or should查询))
GET tb_user/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"addres": "广州"
}
}
],
"should": [
{
"match": {
"user": "赵六"
}
}
],
"minimum_should_match": 1
}
}
}
* 主要参数:
* must:文档必须匹配这些条件才能被包含进来。
* must_not:文档必须不匹配这些条件才能被包含就来。
* should:如果满足这些语句中的任意语句,将增加_score,否则无任何影响,
* 主要用于修正每个文档的相关性得分。
* filter:必须匹配,但他以不评分、过滤模式来进行,这些语句对评分没有贡献,只是根据过滤标准来排除或包含文档。
11)、搜索(仅仅根据条件刷选数据(不做打分处理))
GET tb_user/_search
{
"query": {
"bool": {
"filter": {
"term": {
"addres.keyword": "北京"
}
}
}
}
}
12)、搜索(范围查询)
GET myindex/_search
{
"query": {
"range": {
"age": {
"gte": 40,
"lte": 30
}
}
}
}
13)、搜索(查询是否存在该字段值的数据)
GET myindex/_search
{
"query": {
"exists": {
"field": "location"
}
}
}
14)、搜索(多个索引通配符查询)
GET tb_*/_search
15)、搜索(批量查询)
GET /_msearch
{"index" : "myindex"}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{"index" : "tb_user"}
{"query" : {"match_all" : {}}}
{"index" : "tb_car"}
{"query" : {"match_all" : {}}}
或者(当指定索引名称myindex时,查询条件可以直接使用{}表示默认索引)
GET myindex/_msearch
{}
{"query" : {"match_all" : {}}, "from" : 0, "size" : 10}
{"index" : "tb_user"}
{"query" : {"match_all" : {}}}
{}
{"query" : {"match_all" : {}}}
16)、搜索(聚合分组范围统计)
GET myindex/_search
{
"size": 0,
"aggs": {
"age_aggs_XXX": {
"range": {
"field": "age",
"ranges": [
{
"from": 0,
"to": 10
},
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 60
}
]
}
}
}
}
17)、搜索(聚合分组统计)
GET myindex/_search
{
"size": 0,
"aggs": {
"city_agges_nameXXX": {
"terms": {
"field": "age",
"size": 5
}
}
}
}
18)、搜索(根据条件查询后聚合统计)
GET myindex/_search
{
"size": 0,
"query": {
"match": {
"name": "test-name"
}
},
"aggs": {
"city_agges_xxx": {
"terms": {
"field": "city",
"size": 5
}
}
}
}
19)、搜索(聚合统计个数)
GET myindex/_search
{
"size": 0,
"aggs": {
"city_agges_xxx": {
"cardinality": {
"field": "city"
}
}
}
}
20)、搜索(多层聚合统计)
GET myindex/_search
{
"size": 0,
"aggs": {
"city_agges_nameXXX": {
"terms": {
"field": "city",
"size": 5
},
"aggs": {
"avg_nameXXX": {
"avg": {
"field": "age"
}
}
}
}
}
}
21)、搜索(聚合统计函数)
GET myindex/_search
{
"size": 0,
"aggs": {
"avg_nameXXX": {
"avg": {
"field": "age"
}
}
}
}
GET myindex/_search
{
"size": 0,
"aggs": {
"avg_nameXXX": {
"percentiles": {
"field": "age",
"percents": [10,30,50,80,100]
}
}
}
}