在Kibana中,小写的get等命令也是可以的,不过会出现x号,但是运行不会出错
得到所有的索引
GET /_cat/indices
得到某个索引默认配置
GET /index1
创建某个索引默认配置
PUT /index2
创建某个索引
PUT /index2
{
"settings" : {
"index" : {
"number_of_shards" : "3",
"number_of_replicas" : "1"
}
}
}
删除某个索引的默认配置
GET /_cat/indices
文档操作
添加文档
如果之前索引不存在,也是直接可以通过PUT或者POST进行创建/更改文档的
PUT /index1/type1/1
{
"first_name":"first_ldd",
"last_name":"last_ldd",
"sex":"male"
}
修改文档,将id改为2
其实就是在type1上再加一个文档,这个时候类型type1有两个id 1和2
POST /index1/type1/2
等价于
PUT /index1/type1/2
{
"first_name":"first_ldd",
"last_name":"last_ldd",
"sex":"male"
}
添加文档使用默认的id
POST /index1/type1/
{
"first_name":"first_ldd",
"last_name":"last_ldd",
"sex":"male"
}
修改刚刚创建的默认id的文档,发现id又重新生成了
POST /index1/type1/
{
"first_name":"first_ldd1",
"last_name":"last_ldd1",
"sex":"male1"
}
修改指定的id文档,也可以使用PUT重新创建
POST /index1/type1/1
{
"first_name":"first_ldd-1",
"last_name":"last_ldd-1",
"sex":"male-1"
}
PUT /index1/type1/1
{
"first_name":"first_ldd-put",
"last_name":"last_ldd-put",
"sex":"male-put"
}
修改某个文档的某个字段
POST /index1/type1/1/_update
{
"doc": {
"first_name":"new-first_name"
}
}
批量查询
GET /_mget
{
"docs":[
{
"_index":"index1",
"_type":"type1",
"_id":1
},
{
"_index":"index1",
"_type":"type1",
"_id":2
}
]
}
ES6.x版本之后一个索引里面只能有一个type
批量添加和查看
POST /index2/type2/_bulk
{"index":{"_id":1}}
{"title":"Title1","Price":"1"}
{"index":{"_id":2}}
{"title":"Title2","Price":"2"}
{"index":{"_id":3}}
{"title":"Title3","Price":"3"}
{"index":{"_id":4}}
{"title":"Title4","Price":"4"}
GET /index2/type2/_mget
{
"ids": ["1","2","3","4"]
}
批量增删该查
POST /index2/type2/_bulk
{"delete":{"_index":"index2","_type":"type2","_id":"1"}}
{"create":{"_index":"index3","_type":"type3","_id":"5"}}
{"name":"ldd"}//没有属性这个报异常
{"index":{"_index":"index1","_type":"type1"}}
{"last_name" : "last_ldd-put"}//没有属性这个报异常
{"update":{"_index":"index2","_type":"type2","_id":"3"}}
{"doc":{"title":"update-title"}}
查询
term
查询type1下的所有id
GET /index1/type1/_search
term是精确查询,不知道分词器的存在,不会把单词分开
此外如果值为first-1则不可能查到 term也是模糊查询
如果值为 first seconde,那么tern中写入值为first second则不会被查询到,
如果值为
GET /index1/type1/_search
{
"query": {
"term": { "first_name" : "first"}
}
}
上述的first second 可以通过terms查询,同时有first或者second都将被查到
只要满足其中一个词语即可
查询字段的多个值
GET /index1/type1/_search
{
"query": {
"terms": {
"sex" : ["first","second"]
}
}
}
如果我们想要精确查询到first second的词,可以通过bool进行操作
bool中格式
{bool:{“must”:[] , “must_not”:[] “should”:[]}
- must :必须满足类似于and
- must_not 不需要满足,类似于 not
- should 可以满足也可以不满足类似于or
GET /index1/type1/_search
{
"query": {
"bool": {
"must": [
{
"term":{
"first_name":"first"
}
},
{
"term":{
"first_name":"second"
}
}
]
}
}
}
只有一个term时候,也可以这样写
GET /index1/type1/_search
{
"query": {
"bool": {
"must": {
"term":{
"first_name":"first"
}
}
}
}
}
也可以指定开始查找的起始位置以及查找的数量,版本
GET /index1/type1/_search
{
"from": 0,
"size": 2,
"version": false,
"query": {
"terms": {
"sex" : ["maleput1","male1"]
}
}
}
match查询
match知道查询器的存在
精确查询到包含first,second的所有字段
GET /index1/type1/_search
{
"query": {
"match_phrase": {
"first_name": "first,second"
}
}
}
_source只包含first_name字段
GET /index1/type1/_search
{
"_source": ["first_name"],
"query": {
"match_phrase": {
"first_name": "first_ldd"
}
}
}
包含排除字段
GET /index1/type1/_search
{
"query": {"match_all": {}},
"_source":{
"includes": ["first_name","aa"]
, "excludes": "dd"
}
}
按照字段排序
GET /index2/type2/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}
字段前缀匹配
GET /index1/type1/_search
{
"query": {
"match_phrase_prefix": {
"first_name": "fir"
}
}
}
范围匹配
GET /index2/type2/_search
{
"query": {
"range": {
"age": {
"from": 15,
"to": 20
}
}
}
}
上下界包含
GET /index2/type2/_search
{
"query": {
"range": {
"age": {
"from": 10,
"to": 12,
"include_lower":true,
"include_upper":true
}
}
}
}
模糊查询
GET /index1/type1/_search
{
"query": {
"wildcard": {
"first_name":"first_ldd*"
# "first_name":"first_ldd?" ? 一个字符 # 多个字符
}
}
}
fuzzy,也是一种模糊查询,写错一点可以查出来
GET /index1/type1/_search
{
"query": {
"fuzzy": {
"sex": "malee"
}
}
}
高亮显示
GET /index2/type2/_search
{
"query": {
"match": {
"age": 10
}
},
"highlight":{
"fields": {
"age": {}
}
}
}
查询指定的词在指定的多个字段
GET /index1/type1/_search
{
"query": {
"multi_match": {
"query": "sansan",
"fields": ["name","discribe"]
}
}
}
filte过滤,比较快,不计算相关性,并且可以cache,比一般查询速度块
GET /index2/type2/_search
{
"query": {
"bool": {
"filter": [
{
"terms":{"age":[10,15]}
}
]
}
}
}
范围过滤
主要是一些操作符和shell的一样
gt big than 大于
lt small than 小于
gte big than equal 大于等于
lte small than equal 小于等于
GET /index2/type2/_search
{
"query": {
"range": {
"age": {
"gt": 10,
"lt": 15
}
}
}
}