使用kibana操作elasticsearch7.x 教程


由於elasticsearch7.x取消了type(類型的概念)對應數據庫表的概念

kibana的配置以及安裝地址:https://www.cnblogs.com/TJ21/p/12642219.html

添加一個索引

PUT 索引名
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
}
}

創建映射字段

analyzer:分詞器    下載地址:https://github.com/medcl/elasticsearch-analysis-ik

PUT /索引名/_mapping
{
"properties": {
"title":{
"type": "text",
"analyzer": "ik_max_word"
},
"images":{
"type": "keyword",
"index": false
},
"price":{
"type": "float"
}
}
}

查看映射關系

GET /索引名/_mapping

新增數據

隨機生成id

POST /索引庫名/_doc
{
"title":"大米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":2899.00
}

自定義id   

自定義id值不能重復,否則數據將會被覆蓋

POST /索引庫名/_doc/自定義id值
{
    "title":"超米手機",
    "images":"http://image.leyou.com/12479122.jpg",
    "price":3699.00,
    "Saleable":true
}

 

修改數據,

將上面自定義id的請求方式修改

PUT /索引庫/_doc/id值
{
"title":"超大米手機",
"images":"http://image.leyou.com/12479122.jpg",
"price":3899.00,
"stock": 100,
"saleable":true
}

刪除數據

DELETE /索引庫名/_doc/id值

查詢

查詢所有

GET /索引庫名/_search 
{
"query": {
"match_all": {}
}
}

響應內容:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米手機",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 2699.0,
          "Saleable" : true
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "mmHtSnEBVcsVh4Caiarl",
        "_score" : 1.0,
        "_source" : {
          "title" : "大米手機",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 2899.0
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "超米手機",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 3699.0,
          "Saleable" : true
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "title" : "小米電視4A",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 4699.0,
          "Saleable" : true
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "title" : "華為手機",
          "subTitle" : "小米",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 4699.0
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "title" : "oppo",
          "subTitle" : "小米",
          "images" : "http://image.leyou.com/12479122.jpg",
          "price" : 4899.0
        }
      }
    ]
  }
}
View Code

 

字段解析:

- took:查詢花費時間,單位是毫秒
- time_out:是否超時
- _shards:分片信息
- hits:搜索結果總覽對象
  - total:搜索到的總條數
  - max_score:所有結果中文檔得分的最高分
  - hits:搜索結果的文檔對象數組,每個元素是一條搜索到的文檔信息
    - _index:索引庫
    - _type:文檔類型
    - _id:文檔id
    - _score:文檔得分
    - _source:文檔的源數據
View Code

 

 

# 匹配查詢

GET /索引庫名/_search
{
"query": {
"match": {
"title": {
"query": "小米手機電視",
"minimum_should_match": "60%"
}
}
}
}

 


#多字段查詢 

title,subTitle字段名

GET /索引庫名/_search
{
"query": {
"multi_match": {
"query": "小米",
"fields":["title","subTitle"]
}
}
}

 

#1.詞條查詢     

 可分割的最小詞條單位   title為字段名  [ "字段值" ]

GET /索引庫名/_search
{
  "query": {
    "terms": {
        "title": ["小米","手機"]
    }
  }
}

 

 

#2.多詞條查詢

GET /索引庫名/_search
{
"query": {
"terms": {
"title": ["小米","手機"]
}
}
}

 

 

# 結果過濾   

excludes:不顯示的字段    includes: 顯示的字段

GET /索引庫名/_search
{
"_source": {
"excludes": "{images}"
}, 
"query": {
"terms": {
"title": ["小米","手機"]
}
}
}

 

 

#布爾查詢

標題一定有小米,或者價格為2699,4699

bool把各種其它查詢通過must(與)、must_not(非)、should(或)的方式進行組合

GET /索引庫名/_search
{
"query": {
"bool": {
"must": [
{"match": {
"title": "小米"
}
}
],
"should": [
{"terms": {
"price": [
"2699",
"2799"
]
}}
]
}
}
}

 

# 范圍查詢

價格大於等於2799 小於等於3899

GET /索引庫名/_search
{
"query": {
"range": {
"price": {
"gte": 2799,
"lte": 3899
}
}
}
}

  

# 模糊查詢

標題為oppo 默認允許錯誤一個字母,最大為兩個字母 正確標題 oppo

fuzziness:配置篇里

GET /索引庫名/_search
{
"query": {
"fuzzy": {
"title": {
"value": "oope",
"fuzziness": 2
}
}
}
}

 


# 過濾filter

不會影響查詢的分數_score

GET /索引庫名/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "小米"
}
}
],
"filter": [
{
"range": {
"price": {
"gte": 2699,
"lte": 4999
}
}
}
]
}
}
}

 

#排序

GET /索引庫名/_search
{
"query": {
"bool": {
"filter": [
{
"range": {
"price": {
"gte": 2699,
"lte": 4999
}
}
}
]
}
},
"sort": [
{
"price": {
"order": "desc"
}
},
{
"_id":{
"order": "asc"
}
}
]
}

 

聚合 aggregations

聚合可以讓我們極其方便的實現對數據的統計、分析。例如:

  • 什么品牌的手機最受歡迎?

  • 這些手機的平均價格、最高價格、最低價格?

  • 這些手機每月的銷售情況如何?

實現這些統計功能的比數據庫的sql要方便的多,而且查詢速度非常快,可以實現實時搜索效果。

 

4.1 基本概念

Elasticsearch中的聚合,包含多種類型,最常用的兩種,一個叫,一個叫度量

桶(bucket)

桶的作用,是按照某種方式對數據進行分組,每一組數據在ES中稱為一個,例如我們根據國籍對人划分,可以得到中國桶英國桶日本桶……或者我們按照年齡段對人進行划分:0~10,10~20,20~30,30~40等。

Elasticsearch中提供的划分桶的方式有很多:

  • Date Histogram Aggregation:根據日期階梯分組,例如給定階梯為周,會自動每周分為一組

  • Histogram Aggregation:根據數值階梯分組,與日期類似

  • Terms Aggregation:根據詞條內容分組,詞條內容完全匹配的為一組

  • Range Aggregation:數值和日期的范圍分組,指定開始和結束,然后按段分組

  • ……

 

bucket aggregations 只負責對數據進行分組,並不進行計算,因此往往bucket中往往會嵌套另一種聚合:metrics aggregations即度量

 

度量(metrics)

分組完成以后,我們一般會對組中的數據進行聚合運算,例如求平均值、最大、最小、求和等,這些在ES中稱為度量

比較常用的一些度量聚合方式:

  • Avg Aggregation:求平均值

  • Max Aggregation:求最大值

  • Min Aggregation:求最小值

  • Percentiles Aggregation:求百分比

  • Stats Aggregation:同時返回avg、max、min、sum、count等

  • Sum Aggregation:求和

  • Top hits Aggregation:求前幾

  • Value Count Aggregation:求總數

  • …… 

使用聚合先加入新的索引

PUT /cars
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"color": {
"type": "keyword"
},
"make": {
"type": "keyword"
}
}
}
}

批量添加數據

POST /cars/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }

 

 

 

#聚合為桶

 

GET /cars/_search
{
"aggs": {
"color": {
"terms": {
"field": "color"
}
}
}
}

 

#桶內度量   

GET /cars/_search
{
"size": 0, 
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}

 

#桶內嵌套桶

GET /cars/_search
{
"size": 0,
"aggs": {
"color": {
"terms": {
"field": "color"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
},
"mark":{
"terms": {
"field": "make"
}
}
}
}
}
}

 

 

#階梯分組

對價格進行階梯分組,最小數量為1才顯示

GET /cars/_search
{
"size": 0,
"aggs": {
"price_histogram": {
"histogram": {
"field": "price",
"interval": 5000,
"min_doc_count": 1
}
}
}
}

 

#范圍分組

GET /cars/_search
{
"size": 0,
"aggs": {
"price_range": {
"range": {
"field": "price",
"ranges": [
{
"from": 5000,
"to": 15000
},
{
"from": 15000,
"to": 20000
},
{
"from": 20000,
"to": 25000
},
{
"from": 25000,
"to":35000
},
{
"from": 35000,
"to":40000
}
]
}
}
}
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM