1.查询语句
范例
1)查询所有
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"match_all":{}
}
}
2)查询某个字段
GET http://192.168.56.138:9200/customs/actions/_search
{
“query”:{
"match":{
”eventCategory“:{
"query":"href_click"
}
}
}
}
3)精确查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"term":{
"eventCategory":"txt_input"
}
}
}
4)字段多个值精确查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"terms":{
"eventCategory": [ "txt_input","href_click" ]
}
}
}
5)短语匹配
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"match_phrase": {
"msg":"how you"
}
}
}
6)前缀查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"match_phrase_prefix": {
"msg":"how a"
}
}
}
7)多列匹配查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"multi_match":{
"query":"btn_click",
"fields": [ "eventCategory","msg"]
}
}
}
8)范围查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"range":{
"custid":{
"gte":15000,
"lte":20000
}
}
}
}
9)布尔查询(带分值) must语句
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"bool":{
"must":[
{
"match":{"browse":"chrome:true"}
},
{
"match":{"msg":"hello"}
}
]
}
}
}
10)布尔查询(不带分值)
{
"query":{
"bool":{
"filter":[
{
"match":{"browse":"chrome:true"}
},
{
"match":{"msg":"hello"}
}
]
}
}
}
11)布尔查询(带分值) should语句
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"bool":{
"should":[
{
"match":{"browse":"chrome:true"}
},
{
"match":{"msg":"hello"}
}
]
}
}
}
12)分页查询
//size为每页数量,from后跟(n-1)*size
GET http://192.168.56.138:9200/customs/actions/_search
{
"query":{
"bool":{
"filter":[
{
"match":{"browse":"mozilla:true"}
},
{
"match":{"msg":"hello"}
}
]
}
},
"size":3,
"from":3
}
13)aggs聚合去重查询
GET http://192.168.56.138:9200/customs/actions/_search
{
"aggs":{
"avg_age":{
"avg":{
"field":"age"
}
},
"sum_age":{
"sum":{
"field":"age"
}
}
}
}
14)aggs聚合查询(不显示其他数据)
GET http://192.168.56.138:9200/customs/actions/_search
{
"size": 0,
"aggs":{
"avg_age":{
"avg":{
"field":"age"
}
},
"sum_age":{
"sum":{
"field":"age"
}
}
}
}
15)使用terms查询name分组
GET http://192.168.56.138:9200/mydemo2/teststore/_search
{
"size":0,
"aggs":{
"mybulk":{
"terms":{
"field":"name"
}
}
}
}
16)使用terms查询say的分组
GET http://192.168.56.138:9200/mydemo2/teststore/_search
{
"size":0,
"aggs":{
"mycount":{
"terms":{
"field":"say"
}
}
}
}
2.添加语句
范例
1)创建索引
PUT http://192.168.56.138:9200/mydemo2/teststore
{
"setting":{
"index":{
"number_of_shards":2,
"number_of_replicas":1
}
},
"mappings":{
"userinfos":{
"properties":{
"userid":{
"type":"string"
},
"username":{
"type":"string"
},
"birthday":{
"type":"date",
"format":"yyyy-MM-dd"
}
}
}
}
}
2)添加数据
POST http://192.168.56.138:9200/mydemo/userinfos
{
"userid":"112",
"username":"wang",
"birthday":"1980-08-08",
"say":"he is"
}
3.删除语句
1)删除索引
DELETE http://192.168.56.150:9200/mydemo2/teststore
2)根据id删除数据(id为1)
DELETE http://192.168.56.138:9200/mydemo/userinfos/1
3)按查询结果删除
POST http://192.168.56.138:9200/mydemo/userinfos/_delete_by_query
{
"query":{
"match":{
"userid":"112"
}
}
}
3.修改语句
1)修改类型
POST http://192.168.56.138:9200/mydemo/_mapping/userinfos
{
"properties":{
"userid":{
"type":"integer"
}
}
}
2)根据id修改数据
POST http://192.168.56.138:9200/mydemo/userinfos/1/_update
{
"doc":{
"birthday":"1999-06-08"
}
}
3)更改fielddata数据结构
PUT http://192.168.56.138:9200/customs/actions
{
"properties":{
"eventCategory":{
"type":"text",
"fielddata":true
}
}
}