1. 程式人生 > 其它 >ElasticSearch+Kibana(7.13.1) 簡單操作

ElasticSearch+Kibana(7.13.1) 簡單操作

#建立簡單索引,number_of_shards分片數量,number_of_replicas副本數量
PUT /users
{
   "settings" : {
      "number_of_shards" : 3,
      "number_of_replicas" : 1
   }
}

#檢視setting
GET users/_settings

#檢視mapping
GET users/_mapping


#建立索引並新增資料
#也用來更新整個文件,若已有id為1的資料,es會將原文件標記為已刪除狀態,並新增id為1的新文件,
#原文件並不會立即消失,但是卻無法訪問,在後續檢索操作過程中,ES會慢慢處理這些已刪除資料
PUT starts/_doc/1
{
    "first_name" : "xiao",
    "last_name" :  "zhan",
    "age" :        31,
    "about" :      "A handsome boy.",
    "interests": [ "actors", "singer" ]
}

PUT starts/_doc/2
{
    "first_name" : "zhang",
    "last_name" :  "zhehan",
    "age" :        31,
    "about" :      "Another handsome boy.",
    "interests": [ "actors", "singer" ]
}

#更新部分文件
#es會先根據id為1的文件構建新JSON,然後更新整個JSON,之後會對原文件進行標記刪除,並且構建新文件
POST starts/_update/1
{
  "doc":{
    "age" : 32
    
  }
}

#更新部分文件,使用指令碼
POST starts/_update/1
{
  "script":"ctx._source.age=ctx._source.age+1"
}


#建立新文件,不指定ID
POST starts/_doc
{
    "first_name" : "gong",
    "last_name" :  "jun",
    "age" :        30,
    "about" :      "A very good friend of Zhang Zhehan.",
    "interests": [ "actors", "gamer" ]
}

#建立新文件,指定ID
POST starts/_doc/6
{
    "first_name" : "gong",
    "last_name" :  "jun",
    "age" :        30,
    "about" :      "A very good friend of Zhang Zhehan.",
    "interests": [ "actors", "gamer" ]
}

#刪除文件
DELETE starts/_doc/6

#檢視索引文件的內容
GET starts/_search
{
    "query": {
        "match_all": {}
    }
}

#檢視索引文件的數量
GET starts/_count
{
    "query": {
        "match_all": {}
    }
}

#獲取某一個文件資訊
GET starts/_doc/1

#獲取某一個文件的_source欄位
GET starts/_source/1


#獲取first_name為zhang的文件資訊
GET starts/_search?q=first_name:zhang


#使用查詢表示式獲取first_name為xiao的文件資訊
GET starts/_search
{
    "query" : {
        "match" : {
            "first_name" : "xiao"
        }
    }
}

#增加first_name為zhang的文件
PUT starts/_doc/3
{
    "first_name" : "zhang",
    "last_name" :  "mingyu",
    "age" :        25,
    "about" :      "My boyfriend.",
    "interests": [ "programmer", "gamer" ]
}

PUT starts/_doc/4
{
    "first_name" : "zhang",
    "last_name" :  "dejun",
    "age" :        8,
    "about" :      "The son of gongjun and zhangzhehan.",
    "interests": [ "programmer", "gamer" ]
}

#含過濾器的查詢,年齡大於30的first_name為zhang的文件資訊
GET starts/_search
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "first_name" : "zhang" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}

#全文檢索,分詞檢索,結果按相關度自動降序排列
GET starts/_search
{
    "query" : {
        "match" : {
            "about" : "a handsome boy"
        }
    }
}


#短語檢索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome boy"
        }
    }
}

#短語檢索,對比全文檢索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "a handsome boy"
        }
    }
}

#高亮檢索
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome"
        }
    },
    "highlight": {
        "fields" : {
            "about" : {}
        }
    }
}

#高亮檢索,自定義高亮標籤
GET starts/_search
{
    "query" : {
        "match_phrase" : {
            "about" : "handsome"
        }
    },
    "highlight": {
        "pre_tags" : ["
<tag1>"], "post_tags" : ["</tag1>"], "fields" : { "about" : {} } } } #聚合,被聚合的欄位 GET starts/_search { "aggs": { "all_interests": { "terms": { "field": "interests.keyword" } } } } #聚合+全文檢索 GET starts/_search { "query": { "match": { "first_name": "zhang" } }, "aggs": { "all_interests": { "terms": { "field": "interests.keyword" } } } } #聚合+分級彙總 GET starts/_search { "query": { "match": { "first_name": "zhang" } }, "aggs" : { "all_interests" : { "terms" : { "field" : "interests.keyword" }, "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } } }