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

在建立索引時,能夠指定如下內容:node

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

建立的每一個索引均可以有特定的相關設置,在主體中定義:git

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 容許提供一個映射定義,映射定義用於包含類型名稱github

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,不推薦)。docker

term 查詢會查找咱們指定的精確值。做爲其自己, term 查詢是簡單的。它接受一個字段名以及咱們但願查找的數值:shell

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

一般當查找一個精確值的時候,咱們不但願對查詢進行評分計算。只但願對文檔進行包括或排除的計算,因此咱們會使用 constant_score 查詢以非評分模式來執行 term 查詢並以一做爲統一評分。json

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

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

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

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

  • 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 >elasticsearch

,目標索引的名稱。默認狀況下,若是索引不存在,則自動建立索引。

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

刪除文檔

DELETE /<index>/_doc/<_id>
# 刪除bank索引中id爲1的文檔
DELETE /bank/_doc/1
相關文章
相關標籤/搜索