ElasticSearch教程——Kibana简单操作ES

ElasticSearch汇总请查看ElasticSearch教程——汇总篇

运行、打开kibana相关工具

要先运行ElasticSearch

/usr/elasticsearch/kibana/kibana-6.4.0-linux-x86_64/bin
sh kibana

打开对应的dev Tools

 

获取所有数据

GET /_search

返回结果

{
  "took": 76,
  "timed_out": false,
  "_shards": {
    "total": 16,
    "successful": 16,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 1,
    "hits": [
      {
        "_index": ".kibana",
        "_type": "doc",
        "_id": "config:6.4.0",
        "_score": 1,
        "_source": {
          "type": "config",
          "updated_at": "2018-09-18T09:30:18.949Z",
          "config": {
            "buildNum": 17929,
            "telemetry:optIn": true
          }
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "eTmX5mUBtZGWutGW0TNs",
        "_score": 1,
        "_source": {
          "title": "New version of Elasticsearch released!",
          "content": "Version 1.0 released today!",
          "priority": 10,
          "tags": [
            "announce",
            "elasticsearch",
            "release"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "2",
        "_score": 1,
        "_source": {
          "name": "jiajieshi yagao",
          "desc": "youxiao fangzhu",
          "price": 25,
          "producer": "jiajieshi producer",
          "tags": [
            "fangzhu"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "J3fLFWYBBoLynJN1-kOG",
        "_score": 1,
        "_source": {
          "name": "test yagao",
          "desc": "youxiao fangzhu"
        }
      },
      {
        "_index": "blog",
        "_type": "article",
        "_id": "1",
        "_score": 1,
        "_source": {
          "id": "1",
          "title": "New version of Elasticsearch released!",
          "content": "Version 1.0 released today!",
          "priority": 10,
          "tags": [
            "announce",
            "elasticsearch",
            "release"
          ]
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "KXfSFWYBBoLynJN1TUPo",
        "_score": 1,
        "_source": {
          "name": "test yagao2",
          "desc": "youxiao fangzhu2"
        }
      },
      {
        "_index": "index",
        "_type": "fulltext",
        "_id": "1",
        "_score": 1,
        "_source": {
          "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
        }
      },
      {
        "_index": "ecommerce",
        "_type": "product",
        "_id": "3",
        "_score": 1,
        "_source": {
          "name": "zhonghua yagao",
          "desc": "caoben zhiwu",
          "price": 40,
          "producer": "zhonghua producer",
          "tags": [
            "qingxin"
          ]
        }
      }
    ]
  }
}

 

返回数据含义

 

took:耗费了几毫秒
timed_out:是否超时,false是没有,默认无timeout
_shards:shards fail的条件(primary和replica全部挂掉),不影响其他shard。默认情况下来说,一个搜索请求,会打到一个index的所有primary shard上去,当然了,每个primary shard都可能会有一个或多个replic shard,所以请求也可以到primary shard的其中一个replica shard上去。
hits.total:本次搜索,返回了几条结果
hits.max_score:score的含义,就是document对于一个search的相关度的匹配分数,越相关,就越匹配,分数也高
hits.hits:包含了匹配搜索的document的详细数据,默认查询前10条数据,按_score降序排序

timeout这边默认是没有的,也就意味着当你搜索的时候他会直到所有搜索结束才会返回结果,但是当我们做一些时间比较敏感的搜索的时候,等待时间很久,对用户来说是非常不友好的。那我们可以通过设置timeout这个值,来定时返回已经搜索到的数据。timeout机制,指定每个shard,就只能在timeout时间范围内,将搜索到的部分数据(也可能是搜索到的全部数据),直接返回给client,而不是等到所有数据全部搜索出来后再返回。

可以通过如下方式进行设置

timeout=10ms,timeout=1s,timeout=1m
GET /_search?timeout=10m

 

 

创建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" ]
}

检索文档(查询)

GET /ecommerce/product/1

返回结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

替换文档(全量替换)

PUT /ecommerce/product/1
{
    "name" : "jiaqiangban gaolujie yagao",
    "desc" :  "gaoxiao meibai",
    "price" :  30,
    "producer" :      "gaolujie producer",
    "tags": [ "meibai", "fangzhu" ]
}

返回结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

document结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

注意点

  1. document是不可变的,如果要修改document的内容,可以通过全量替换,直接对document重新建立索引,替换里面所有的内容。
  2. es会将老的document标记为deleted(逻辑删除),然后新增我们给定的一个document,当我们创建越来越多的document的时候,es会在适当的时机在后台自动删除(物理删除)标记为deleted的document。
  3. 替换必须带上所有的field,否则其他数据会丢失。

 

 

更新文档(修改)

原理参考:ElasticSearch教程——partial update(更新文档)实现原理及并发控制

POST /ecommerce/product/1/_update
{
  "doc": {
    "name": "jiaqiangban gaolujie yagao"
  }
}

返回结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 5,
  "result": "updated",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 4,
  "_primary_term": 1
}

document结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 5,
  "found": true,
  "_source": {
    "name": "jiaqiangban gaolujie yagao",
    "desc": "gaoxiao meibai",
    "price": 30,
    "producer": "gaolujie producer",
    "tags": [
      "meibai",
      "fangzhu"
    ]
  }
}

 

 

删除文档(删除)

DELETE /ecommerce/product/1

返回结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "_version": 9,
  "result": "deleted",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 8,
  "_primary_term": 1
}

document结果

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1",
  "found": false
}

注意:

在删除一个document之后,我们可以从侧面证明,它不是立即物理删除的,因为它的一些版本号等信息还是保留的。

 

请求分类

1、query string search

类似这种 搜索全部商品:GET /ecommerce/product/_search(参数直接拼接在请求url上,不带json参数的)

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

搜索商品名称中包含yagao的商品,而且按照售价降序排序:GET /ecommerce/product/_search?q=name:yagao&sort=price:desc

适用于临时的在命令行使用一些工具,比如curl,快速的发出请求,来检索想要的信息;但是如果查询请求很复杂,是很难去构建的,所以在生产环境中,几乎很少使用query string search

 

2、query DSL

DSL:Domain Specified Language,特定领域的语言
http request body:请求体,可以用json的格式来构建查询语法,比较方便,可以构建各种复杂的语法,比query string search肯定强大多了。

更加适合生产环境的使用,可以构建复杂的查询

1.查询所有的商品

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

 

2.查询名称包含yagao的商品,同时按照价格降序排序

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

 

3.分页查询

总共3条商品,假设每页就显示1条商品,现在显示第2页,所以就查出来第2个商品

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

 

4.查询指定项

指定要查询出来商品的名称和价格

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

 

 

5.过滤查询

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

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

 

6.full-text search(全文检索)

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

7.phrase search(短语搜索)

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

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

 

8.多条件查询

名字中有"yagao",描述上可以有fangzhu也可以没有,价格不能是25元

must表示一定要满足;

should表示可以满足也可以不满足;

must_not表示不能满足该条件;

"minimum_should_match": 1,表示最小匹配度,可以设置为百分百,详情看源文档Elasticsearch Reference [6.4] » Query DSL » Minimum Should Match,设置了这个值的时候就必须满足should里面的设置了,另外注意这边should里面同一字段设置的多个值(意思是当这个值等于X或者等于Y的时候都成立,务必注意格式

 

GET /ecommerce/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "yagao"
          }
        }
      ],
      "should": [
        {
          "match": {
            "desc": "fangzhu"
          }
        },
        {
          "match": {
            "desc": "caoben"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "price": 25
          }
        }
      ],
     "minimum_should_match": 1
    }
  }
}

 

  • 22
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值