ElasticSearch7.8.0Docker安裝及入門最基本操作


Docker 安裝 ElasticSearch

拉取鏡像
# 最新版本7,8.0
docker pull elasticsearch:7.8.0
啟動集群
# 基本啟動命令
# -e "discovery.type=single-node" \  單節點集群
# -e ES_JAVA_OPTS="-Xms512m -Xmx512m" \ 制定運行參數,不然如果機器內存太小,啟動后會非常卡頓
# --name 起個別名
docker run -p 9200:9200 -p 9300:9300 --name es7.8 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-d elasticsearch:7.8.0

# 查看集群健康狀態
GET /_cat/health?v
# 查看節點健康狀態
GET /_cat/nodes?v
# 查看索引信息
GET /_cat/indices?v
進入容器
# 進入容器中
docker exec -it es7.8 /bin/bash
# 查看文件夾及權限
[root@87e29ba6ef1e elasticsearch]# ll
total 588
-rw-r--r--  1 elasticsearch root  13675 Jun 14 19:34 LICENSE.txt
-rw-r--r--  1 elasticsearch root 544318 Jun 14 19:37 NOTICE.txt
-rw-r--r--  1 elasticsearch root   8165 Jun 14 19:34 README.asciidoc
drwxr-xr-x  2 elasticsearch root   4096 Jun 14 19:39 bin
drwxrwxr-x  1 elasticsearch root   4096 Jul  9 04:31 config #775權限
drwxrwxr-x  3 root          root   4096 Jul  9 04:29 data #775權限
drwxr-xr-x  1 elasticsearch root   4096 Jun 14 19:38 jdk 
drwxr-xr-x  3 elasticsearch root   4096 Jun 14 19:38 lib
drwxrwxr-x  1 elasticsearch root   4096 Jul  9 04:31 logs #775權限
drwxr-xr-x 47 elasticsearch root   4096 Jun 14 19:39 modules
drwxr-xr-x  2 root          root   4096 Jul  9 04:17 plugins #775權限
可在啟動容器的時候指定數據卷映射
docker run -p 9200:9200 -p 9300:9300 --name es7.8 \
-e "discovery.type=single-node" \
-e ES_JAVA_OPTS="-Xms512m -Xmx512m" \
-v /dockerfile/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
-v /dockerfile/elasticsearch/data:/usr/share/elasticsearch/data \
-v /dockerfile/elasticsearch/logs:/usr/share/elasticsearch/logs \
-d elasticsearch:7.8.0
# 做映射之前賦予文件夾相應權限,默認創建的文件夾權限就是755,所	吃   以/dockerfile/elasticsearch/plugins不用改權限
chmod -R 775 /dockerfile/elasticsearch/data
chmod -R 775 /dockerfile/elasticsearch/logs

# data和logs文件夾剛開始是空的,數據是啟動時自己加進去的,所以可以做映射,
# plugins文件夾剛開始是沒有安裝插件的,也是空的,所以也可以做映射
# config文件夾是配置文件,不能用空目錄做映射,否則啟動直接退出,除非自己創建的文件夾中有相關配置文件
# 可以先進入容器,講config文件夾拷貝出來,得到一份配置文件
-v /dockerfile/elasticsearch/config:/usr/share/elasticsearch/config \
插入一些數據
# 官方提供的account.json數據,包含1000條數據
wget https://raw.githubusercontent.com/elastic/elasticsearch/master/docs/src/test/resources/accounts.json#
# 每個Document內容
{
    "account_number": 0,
    "balance": 16623,
    "firstname": "Bradshaw",
    "lastname": "Mckenzie",
    "age": 29,
    "gender": "F",
    "address": "244 Columbus Place",
    "employer": "Euron",
    "email": "bradshawmckenzie@euron.com",
    "city": "Hobucken",
    "state": "CO"
}
# 使用以下 _ bulk 批量操作請求將賬戶數據存儲到到bank索引中:
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_bulk?pretty&refresh" --data-binary "@accounts.json"
# 查看索引狀態
curl "localhost:9200/_cat/indices?v"
Docker 安裝 Kkibana
# 拉取鏡像
# kibana版本必須和elasticsearch版本保持一致
docker pull kibana:7.8.0

# 啟動容器
# YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_ID 正在運行的ES容器ID或name
docker run --link YOUR_ELASTICSEARCH_CONTAINER_NAME_OR_ID:elasticsearch -p 5601:5601 {docker-repo}:{version}
docker run --link es7.8:elasticsearch -p 5601:5601 -d kibana:7.8.0
安裝IK分詞器
# Ik分詞器版本要和ES和Kibana版本保持一致

# 進入容器
docker exec -it elasticsearch /bin/bash
#此命令需要在容器中運行
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.8.0/elasticsearch-analysis-ik-7.8.0.zip
# 退出容器,重啟容器
docker restart elasticsearch

索引的操作

創建索引

# 創建一個index,名為twitter
PUT /twitter
# 查看索引狀態
GET /_cat/indices?v

在創建索引時,可以指定以下內容:

  • Settings for the index 索引的設置
  • Mappings for fields in the index 索引中字段的映射
  • Index aliases 索引別名

創建的每個索引都可以有特定的相關設置,在主體中定義:

PUT /twitter
{
    "settings" : {
        "index" : {
            "number_of_shards" : 3, 
            "number_of_replicas" : 2 
        }
    }
}
# _ shards 的默認值是1
# _ replica 的默認值是1(即每個主碎片有一個副本)

#或者更簡單,不必在設置部分中顯式指定索引部分
PUT /twitter
{
    "settings" : {
        "number_of_shards" : 3,
        "number_of_replicas" : 2
    }
}

創建索引 API 允許提供一個映射定義,映射定義用於包含類型名稱

PUT /test
{
    "settings" : {
        "number_of_shards" : 1
    },
    "mappings" : {
        "properties" : {
            "field1" : { "type" : "text" } 
        }
    }
}

刪除索引

DELETE /twitter
# 查看索引狀態
GET /_cat/indices?v

查看索引

GET /twitter

文檔的操作

查詢文檔

查看文檔類型
GET /bank/_mapping
使用 match_all查詢全部
# 查詢索引bank中的全部文檔
GET /bank/_search
{
  "query": { "match_all": {} }
}
使用sort指定排序規則,默認按匹配度(得分score)排序
# 檢索銀行索引中的所有文檔,按account_number字段排序 asc 是升序,desc是降序
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}

# 查找指定范圍的文檔
# 下面的請求會得到從第10條文檔到第19條文檔的結果:
GET /bank/_search
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ],
  "from": 10,  # 起始位置
  "size": 10   # 大小/數目
}

# 默認情況下,響應的命中部分hits包含與搜索條件匹配的前10個文檔:
# took – 運行查詢需要多長時間(毫秒)
# timed_out – 搜尋請求是否超時
# _shards – 搜索了多少碎片,並分別列出成功、失敗或跳過的碎片數量,創建索引時默認分片數是5
# max_score – 找到的所有文檔中最相關文檔(匹配程度最高)的分數
# hits.total.value - 找到多少相符的文檔
# hits.sort -  文檔的排序位置(按哪個字段排序,不指定排序規則時按相關性得分排序)
# hits._score -  文件的相關性得分(不適用於使用match_all)
{
  "took" : 63,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {                        # 命中部分
    "total" : {
        "value": 1000,              # 查到的總數
        "relation": "eq"
    },
    "max_score" : null,
    "hits" : [ {
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "0",
      "sort": [0],
      "_score" : null,
      "_source" : {"account_number":0,"balance":16623,"firstname":"Bradshaw","lastname":"Mckenzie","age":29,"gender":"F","address":"244 Columbus Place","employer":"Euron","email":"bradshawmckenzie@euron.com","city":"Hobucken","state":"CO"}
    }, {
      "_index" : "bank",
      "_type" : "_doc",
      "_id" : "1",
      "sort": [1],
      "_score" : null,
      "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
    }, ...
    ]
  }
}

# 搜索並返回指定字段內容,使用_source表示,例如只返回account_number和balance兩個字段內容:
GET /bank/_search
{
  "query": { "match_all": {} },
  "_source": ["account_number", "balance"]
}
使用match,單詞查詢
# 會查出字所有文檔中,address字段包含 mill 【或者】 lane 的文檔
GET /bank/_search
{
  "query": {
    "match": {
      "address": "mill lane"
    }
  }
}
# 對於數值類型match操作使用的是精確匹配,對於文本類型使用的是模糊匹配;
使用match_phrase,短語搜索
# 只會查出字所有文檔中,address字段包含 "mill lane" 的文檔
GET /bank/_search
{
  "query": {
    "match_phrase": {
      "address": "mill lane"
    }
  }
}
bool 組合多個查詢條件
  • must match 必須匹配
  • should match 應該匹配(一般用於組合 或 關系)
  • must not 必須不滿足,相當於排序filter
  • 布爾查詢中的每個 must、 should 和 must _ not 元素都稱為查詢子句。文檔滿足每個 必須或應該條款 中的標准的程度決定了文檔的相關性得分。得分越高,文檔就越符合你的搜索條件。默認情況下,Elasticsearch 返回按照相關性得分排序后的文檔。
# 在bank索引中搜索40歲客戶(age=40)的賬戶,但不包括住在愛達荷州的任何人,州名是ID (state=ID):
GET /bank/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "age": "40" } }
      ],
      "must_not": [
        { "match": { "state": "ID" } }
      ]
    }
  }
}
# 還可以借助 must_not 顯示地指定篩選條件
# 使用范圍過濾器filter將結果限制為余額(balance字段)在 $20,000和 $30,000之間的帳戶。
# gte是大於等於,gt是大於;lte是小於等於,lt是小於
GET /bank/_search
{
  "query": {
    "bool": {
      "must": { "match_all": {} },
      "filter": {
        "range": {
          "balance": {
            "gte": 20000,
            "lte": 30000
          }
        }
      }
    }
  }
}

# should表示滿足其中任意一個,搜索address字段中包含mill或者lane的文檔;
GET /bank/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "address": "mill" } },
        { "match": { "address": "lane" } }
      ]
    }
  }
}
term精確值查找

term 查詢, 可以用它處理數字(numbers)、布爾值(Booleans)、日期(dates)以及文本(text,不推薦)。

term 查詢會查找我們指定的精確值。作為其本身, term 查詢是簡單的。它接受一個字段名以及我們希望查找的數值:

{
    "term" : {
        "price" : 20
    }
}

通常當查找一個精確值的時候,我們不希望對查詢進行評分計算。只希望對文檔進行包括或排除的計算,所以我們會使用 constant_score 查詢以非評分模式來執行 term 查詢並以一作為統一評分。

最終組合的結果是一個 constant_score 查詢,它包含一個 term 查詢:

GET /bank/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "term": {
          "age": {
            "value": 20
          }
        }
      }
    }
  }
}

默認情況下,Elasticsearch 作為分析的一部分更改text類型字段的值。這可能使查找文本字段值的精確匹配變得困難。若要搜索文本字段值,請使用match查詢。

默認情況下,Elasticsearch 會在分析期間更改文本字段的值。例如,默認的標准分析器更改文本字段值如下:

  • Removes most punctuation 刪除大部分標點符號
  • Divides the remaining content into individual words 將剩下的內容分成單獨的單詞,稱為tokens
  • Lowercases the tokens 小寫tokens
# 創建一個名為my_index的索引,其中 full _ text 字段為文本類型。
PUT my_index
{
    "mappings" : {
        "properties" : {
            "full_text" : { "type" : "text" }
        }
    }
}
# 在插入一個值為 Quick Brown Foxes! 的文檔。
PUT my_index/_doc/1
{
  "full_text":   "Quick Brown Foxes!"
}
# 因為full_text字段是一個文本字段,Elasticsearch 在分析期間將 Quick Brown Foxes! 更改為[ Quick,Brown,fox ]。
# 使用term搜索這條文檔
GET my_index/_search?pretty
{
  "query": {
    "term": {
      "full_text": "Quick Brown Foxes!"
    }
  }
}
# 因為full_text字段不再包含確切的術語 Quick BrwnFoxes! ,術語查詢搜索不返回任何結果。

增加文檔

# 指定索引customer,指定id,可選,若不指定則隨機生成,_doc是默認參數,可不寫
PUT /bank/_doc/1
{
  "email": "test@test.com"
}

修改文檔

POST /< index >/_ update/< _ id >

,目標索引的名稱。默認情況下,如果索引不存在,則自動創建索引。

<_id> 必需,字符串,文檔更新的唯一標識符

刪除文檔

DELETE /<index>/_doc/<_id>
# 刪除bank索引中id為1的文檔
DELETE /bank/_doc/1


免責聲明!

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



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