1、kibana常用es命令

 

一、cluster

       es提供了一套api,叫cat api, 可以查看es中各种各样的数据

1、查看cluster集群的健康状况:get /_cat/healt?v

二、index---CRUD

1、查看cluster中有哪些index:get /_cat/indices?v

2、创建index:put /test_index?pretty

3、删除索引:delete /test_index?pretty

4、创建index,指定primary shard和replica shard的个数

PUT /test_index
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}

三、document---CRUD

    1、新增document--->PUT

PUT /index/type/id--->/索引名/类型名/doucument_Id/{document内容}
{
  "json数据"
}

es自动生成id(如:AVp4RN0bhjxldOOnBxaE)

POST /index/type
{
  "json数据"
}

注:es会自动建立index和type,不需要提前创建,而且es默认会对document每个field都建立倒排索引,让其可以被搜索

    2、查询document:检索document--->GET

GET /index/type/id

查询_source中指定的field:GET /index/type/id?_source=field1,field2

批量查询:_mget

GET /_mget
{
   "docs" : [
      {
         "_index" : "test_index",
         "_type" :  "test_type",
         "_id" :    1
      },
      {
         "_index" : "test_index",
         "_type" :  "test_type",
         "_id" :    2
      }
   ]
}

    3、替换document--->PUT

PUT /index/type/id--->/索引名/类型名/doucument_Id/{document内容}
{
  "json数据"
}

利用version进行并发控制:

内部version:PUT /index/type/id?version=n

外部version:PUT /index/type/id?version=n&version_type=external

注:替换方式有一个不好,即使必须带上所有的field,否则field就会丢失。此方式version会加1

    4、更新document--->POST

POST /index/type/document_id/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

遇到并发冲突时,重试:post /index/type/id/_update?retry_on_conflict=5

注:此方式version不变

    5、删除document--->DELETE 

DELETE /index/type/id

    6、基于groovy操作document

(1)、内置脚本:

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1"
}

(2)、外部脚本

a、在 E:\ES\elasticsearch-5.2.0\config\scripts 添加a.groovy文件。文件内容:ctx._source.tags+=new_tag

b、kibana操作脚本

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",  ----------------->脚本语言
    "file": "a",-------------------------->文件名
    "params": {
      "new_tag": "tag1"---------------->参数
    }
  }
}

(3)、用脚本删除文档

a、脚本内容:ctx.op = ctx._source.num == count ? 'delete' : 'none'

b、kibana操作脚本

POST /test_index/test_type/11/_update
{
  "script": {
    "lang": "groovy",
    "file": "test-delete-document",
    "params": {
      "count": 1
    }
  }
}

(4)upsert操作

a、如果指定的document不存在,就执行upsert中的初始化操作;如果指定的document存在,就执行doc或者script指定的partial update操作

b、kibana操作

POST /test_index/test_type/11/_update
{
   "script" : "ctx._source.num+=1",
   "upsert": {
       "num": 0,
       "tags": []
   }
}

6、bulk语法:批量增删改

(1)、例如

POST /_bulk
{ "delete": { "_index": "test_index", "_type": "test_type", "_id": "3" }} 
{ "create": { "_index": "test_index", "_type": "test_type", "_id": "12" }}
{ "test_field":    "test12" }
{ "index":  { "_index": "test_index", "_type": "test_type", "_id": "2" }}
{ "test_field":    "replaced test2" }
{ "update": { "_index": "test_index", "_type": "test_type", "_id": "1", "_retry_on_conflict" : 3} }
{ "doc" : {"test_field2" : "bulk test1"} }

(2)、操作类型

a、delete:删除一个文档,只要1个json串就可以了
b、create:PUT /index/type/id/_create,强制创建
c、index:普通的put操作,可以是创建文档,也可以是全量替换文档
d、update:执行的partial update操作

(3)说明

a、bulk api对json的语法,有严格的要求,每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行
b、bulk操作中,任意一个操作失败,是不会影响其他的操作的,但是在返回结果里,会告诉你异常日志

 

四、六种查询方法

    0、名词解析

查询所有document:get /index/type/_search

took:耗费了几毫秒
timed_out:是否超时,这里是没有
_shards:数据拆成了5个分片,所以对于搜索请求,会打到所有的primary shard(或者是它的某个replica shard也可以)
hits.total:查询结果的数量,3个document
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据

    前提

PUT /ecommerce/product/1
{
    "name" : "gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}
PUT /ecommerce/product/2
{
    "name" : "jiajieshi yagao",
    "desc" :  "youxiao fangzhu",
    "price" :  25,
    "producer" :      "jiajieshi producer",
    "tags": [ "fangzhu" ]
}
PUT /ecommerce/product/3
{
    "name" : "zhonghua yagao",
    "desc" :  "caoben zhiwu",
    "price" :  40,
    "producer" :      "zhonghua producer",
    "tags": [ "qingxin" ]
}
PUT /ecommerce/product/4
{
    "name" : "special yagao",
    "desc" :  "special meibai",
    "price" :  50,
    "producer" :      "special yagao producer",
    "tags": [ "meibai" ]
}

    1、query string search

a、由来:因为search参数都是以http请求的query string来附带的

b、例:GET /ecommerce/product/_search?q=name:yagao&sort=price:desc

     2、query DSL

DSL:Domain Specified Language,特定领域的语言
http request body:请求体,可以用json的格式来构建查询语法

a、查询所有的商品

GET /ecommerce/product/_search
{
  "query": { "match_all": {} }
}

b、查询名称包含yagao的商品,同时按照价格降序排序--->sort

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "name" : "yagao"
        }
    },
    "sort": [
        { "price": "desc" }
    ]
}

c、分页查询商品,总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查出来第2个商品--->from、size

GET /ecommerce/product/_search
{
  "query": { "match_all": {} },
  "from": 1,
  "size": 1
}

d、指定要查询出来商品的名称和价格--->_source

GET /ecommerce/product/_search
{
  "query": { "match_all": {} },
  "_source": ["name", "price"]
}

    3、query filter--->filter

a、搜索商品名称包含yagao,而且售价大于25元的商品

GET /ecommerce/product/_search
{
    "query" : {
        "bool" : {
            "must" : {
                "match" : {
                    "name" : "yagao" 
                }
            },
            "filter" : {
                "range" : {
                    "price" : { "gt" : 25 } 
                }
            }
        }
    }
}

注:bool中包含多个查询条件

    4、full-text search(全文检索)--->match

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "yagao producer"
        }
    }
}

注:producer这个字段,会先被拆解,建立倒排索引。。。查询条件:yagao producer ---> yagao和producer

special        4
yagao         4
producer    1,2,3,4
gaolujie      1
zhognhua   3
jiajieshi       2

    5、phrase search(短语搜索)--->match_phrase

GET /ecommerce/product/_search
{
    "query" : {
        "match_phrase" : {
            "producer" : "yagao producer"
        }
    }
}

注:跟全文检索相对应,相反,全文检索会将输入的搜索串拆解开来,去倒排索引里面去一一匹配,只要能匹配上任意一个拆解后的单词,就可以作为结果返回
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才可以算匹配,才能作为结果返回

    6、highlight search(高亮搜索结果)--->highlight

GET /ecommerce/product/_search
{
    "query" : {
        "match" : {
            "producer" : "producer"
        }
    },
    "highlight": {
        "fields" : {
            "producer" : {}
        }
    }
}

 

五、聚合查询

     terms:按照field名称分组
     range:按照value的范围分组
     avg:分组后求平均数

1、计算每个tag下的商品数量

将文本field的fielddata属性设置为true

PUT /ecommerce/_mapping/product
{
  "properties": {
    "tags": {
      "type": "text",
      "fielddata": true
    }
  }
}

GET /ecommerce/product/_search
{
  "size": 0,
  "aggs": {
    "all_tags": {
      "terms": { "field": "tags" }
    }
  }
}

2、对名称中包含yagao的商品,计算每个tag下的商品数量

GET /ecommerce/product/_search
{
  "size": 0,
  "query": {
    "match": {
      "name": "yagao"
    }
  },
  "aggs": {
    "all_tags": {
      "terms": {
        "field": "tags"
      }
    }
  }

}

3、计算每个tag下的商品的平均价格,并且按照平均价格降序排序

GET /ecommerce/product/_search
{
    "size": 0,
    "aggs" : {
        "all_tags" : {
            "terms" : { "field" : "tags", "order": { "avg_price": "desc" } },
            "aggs" : {
                "avg_price" : {
                    "avg" : { "field" : "price" }
                }
            }
        }
    }
}

4、按照指定的价格范围区间进行分组,然后在每组内再按照tag进行分组,最后再计算每组的平均价格

GET /ecommerce/product/_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"
              }
            }
          }
        }
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值