Elasticsearch的安裝與簡單使用

1、安裝

Elasticsearch是一個基於Apache Lucene(TM)的開源搜索引擎。它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。web

(一)Elasticsearch下載與安裝

一、Elasticsearch的下載

官網下載有時太慢,這裏分享一個連接:連接:https://pan.baidu.com/s/1u-3M4yr7zTjioYQZQKCeYQ 提取碼:vfr3api

 下載完成後進行解壓,進入到以下目錄:服務器

 能夠看到裏面有一個jdk的文件夾,因此你先須要將jdk的環境配置好:less

二、配置jdk環境jvm

  • JAVA_HOME配置

在系統環境變量中配置JAVA_HOME變量elasticsearch

  • 配置classpath
.;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar

 

  •  編輯Path
%JAVA_HOME%\bin\;%JAVA_HOME%\jre\bin;

  • 測試是否安裝成功

三、啓動Elasticsearch分佈式

能夠進入到其bin目錄下,點擊elasticsearch.bat直接運行:ide

 可是不出意外的話,應該很大可能失敗,此時咱們須要看看錯誤信息,須要在cmd串口中進行運行,錯誤信息有如下狀況:測試

  •  錯誤一
OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in ve rsion 9.0 and will likely be removed in a future release.

此時,咱們須要修改elasticsearch目錄下的congfig目錄下的jvm.options文件:ui

 將-XX:+UseConcMarkSweepGC修改成-XX:+UseG1GC便可。

  • 錯誤二
Error occurred during initialization of VM Initial heap size set to a larger value than the maximum heap size

此時咱們仍是修改上述配置文件,只須要將堆容量設置的小一些就能夠了:

 這裏我將以前的1G修改成500M,而後保存文件並從新啓動就ok了,成功啓動後咱們能夠進行驗證,訪問9200端口:

其詳細配置信息位於config目錄下的elasticsearch.yml文件中。

(二)kibana的下載與安裝

  kibana是elasticsearch的web版客戶端,咱們能夠經過它來鏈接已經啓動的elasticsearch,這樣操做起來就會更方便,注意的是kibana的版本必須與elasticsearch的版本保持一致,這裏提供了資源地址:

連接:https://pan.baidu.com/s/1sBaGUzxYxe5nX8SKPK9AKQ
提取碼:6ba4
下載完成而且進行解壓,而後進入到bin目錄下:

直接點擊kibana.bat就會自動啓動運行,它會自動鏈接elasticsearch服務器,咱們經過默認端口5601能夠進行訪問:

kibana的詳細配置在其config目錄的kibana.yml文件中,包括鏈接elasticsearch的地址等信息。

2、簡單使用

(一)cued操做

一、增長數據

#格式:
 PUT 索引名稱/類型/文檔id { "name":"zhangsan", "age":23 }

例如:

PUT crm/user/1 { "name":"zhangsan", "age":23 }

執行結果爲:

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the 
typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}). { "_index" : "crm", "_type" : "user", "_id" : "1", "_version" : 1, "result" : "created", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 0, "_primary_term" : 1 }
View Code

二、刪除數據

  • 刪除指定文檔
#格式:
 DELETE 索引名稱/類型名稱/文檔id

例如:

DELETE crm/user/1

執行結果:

#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the /{index}/_doc/{id} endpoint instead.
{ "_index" : "crm", "_type" : "user", "_id" : "1", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
View Code
  • 刪除索引
# 格式
 DELETE 索引名稱

例如:

DELETE crm

執行結果爲:

{ "acknowledged" : true }
View Code

三、更新數據

# 格式
 POST 索引名稱/類型/文檔id/_update { "doc":{"age":25} }

例如:

POST crm/user/1/_update { "doc":{ "age":25 } }

執行結果:

#! Deprecation: [types removal] Specifying types in document update requests is deprecated, use the endpoint /{index}/_update/{id} instead.
{ "_index" : "crm", "_type" : "user", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
View Code

四、查詢

 查詢有兩種方式,其一爲參數查詢(query string),其二爲結構化查詢(DSL)。

  • query string
# 格式
 GET 索引名/類型/_search?q=age:25

實例:

GET crm/user/_search?q=age:25

執行結果爲:

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "crm", "_type" : "user", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "zhangsan", "age" : 25 } } ] } }
View Code
  • DSL
# 格式
 GET 索引名/類型/_search { 「query」:{ "match":{"age":25} } }

實例:

GET crm/user/_search { "query":{ "match":{"age":25} } }

執行結果:

#! Deprecation: [types removal] Specifying types in search requests is deprecated.
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "crm", "_type" : "user", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "zhangsan", "age" : 25 } } ] } }
View Code

(二)通用查詢操做

一、判斷索引是否存在

# 格式
 HEAD 索引名稱

例如:

#輸入
HEAD crm #輸出
200 - OK

二、查詢全部索引

#命令
 GET _cat/indices

例如:

#輸入
GET _cat/indices #輸出
yellow open crm                  Z2cstTIpSvi0nmjHSgMjYA 1 1 1 0  8.3kb  8.3kb green open .kibana_1 2MWTky02QrqSJLVtfPD9aA 1 0 4 0 17.5kb 17.5kb green open .kibana_task_manager Yw1dT1Q_RtyqW2huAUkROQ 1 0 2 0 45.4kb 45.4kb

三、查詢全部內容

# 命令
 GET _search { "query": { "match_all": {} } }

例如:

GET _search { "query": { "match_all": {} } }

輸出:

{ "took" : 2, "timed_out" : false, "_shards" : { "total" : 3, "successful" : 3, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 7, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : ".kibana_1", "_type" : "_doc", "_id" : "space:default", "_score" : 1.0, "_source" : { "space" : { "name" : "Default", "description" : "This is your default space!", "color" : "#00bfb3", "_reserved" : true }, "type" : "space", "references" : [ ], "updated_at" : "2020-01-09T15:21:14.811Z" } }, { "_index" : ".kibana_1", "_type" : "_doc", "_id" : "config:7.0.0", "_score" : 1.0, "_source" : { "config" : { "buildNum" : 23117 }, "type" : "config", "references" : [ ], "updated_at" : "2020-01-09T15:21:41.656Z" } }, { "_index" : ".kibana_1", "_type" : "_doc", "_id" : "telemetry:telemetry", "_score" : 1.0, "_source" : { "telemetry" : { "enabled" : false }, "type" : "telemetry", "references" : [ ], "updated_at" : "2020-01-09T15:29:26.961Z" } }, { "_index" : ".kibana_1", "_type" : "_doc", "_id" : "maps-telemetry:maps-telemetry", "_score" : 1.0, "_source" : { "maps-telemetry" : { "mapsTotalCount" : 0, "timeCaptured" : "2020-01-11T11:51:14.760Z", "attributesPerMap" : { "dataSourcesCount" : { "min" : 0, "max" : 0, "avg" : 0 }, "layersCount" : { "min" : 0, "max" : 0, "avg" : 0 }, "layerTypesCount" : { }, "emsVectorLayersCount" : { } } }, "type" : "maps-telemetry", "references" : [ ], "updated_at" : "2020-01-11T11:51:14.761Z" } }, { "_index" : ".kibana_task_manager", "_type" : "_doc", "_id" : "Maps-maps_telemetry", "_score" : 1.0, "_source" : { "type" : "task", "task" : { "taskType" : "maps_telemetry", "state" : """{"runs":1,"stats":{"mapsTotalCount":0,"timeCaptured":"2020-01-11T11:51:14.760Z","attributesPerMap":{"dataSourcesCount":{"min":0,"max":0,"avg":0},"layersCount":{"min":0,"max":0,"avg":0},"layerTypesCount":{},"emsVectorLayersCount":{}}}}""", "params" : "{}", "attempts" : 0, "scheduledAt" : "2020-01-11T11:51:13.743Z", "runAt" : "2020-01-11T16:00:00.000Z", "status" : "idle" }, "kibana" : { "uuid" : "68f24dc0-cb43-430d-b601-005e146852c8", "version" : 7000099, "apiVersion" : 1 } } }, { "_index" : ".kibana_task_manager", "_type" : "_doc", "_id" : "oss_telemetry-vis_telemetry", "_score" : 1.0, "_source" : { "type" : "task", "task" : { "taskType" : "vis_telemetry", "state" : """{"runs":1}""", "params" : "{}", "attempts" : 1, "scheduledAt" : "2020-01-11T11:51:14.185Z", "runAt" : "2020-01-11T16:00:00.000Z", "status" : "idle" }, "kibana" : { "uuid" : "68f24dc0-cb43-430d-b601-005e146852c8", "version" : 7000099, "apiVersion" : 1 } } }, { "_index" : "crm", "_type" : "user", "_id" : "1", "_score" : 1.0, "_source" : { "name" : "zhangsan", "age" : 25 } } ] } }
View Code 
相關文章
相關標籤/搜索