Elasticsearch7基礎2

I.基礎概念:
1. Node與 Cluster
Elasticsearch本質上是一個分佈式數據庫,允許多臺服務器協同工作,每臺服務器可以運行多個 Elasticsearch實例。
單個 Elasticsearch實例稱爲一個節點(node)。一組節點構成一個集羣( cluster)。
2. index索引
Elasticsearch會索引所有字段,經過處理後寫入一個倒排索引( Inverted Index)。查找數據的時侯,直接查找該索引。
所以, Elasticsearch數據管理的頂層單位就叫做 Index(索引)。它是單個數據庫的同義詞。每個 Index(即數據庫)的名字必須是小寫。
可以將index理解爲mysql中的數據庫( database),方便我們理解後續的概念。
3.document文檔
index裏面單條的記錄稱爲 Document(文檔)。許多條 Document構成了一個 Index。
Document使用JsoN格式表示,下面是一個例子

{
   "user":"張三",
   "desc":"數據庫管理
}

同一個 Index裏面的 Document,不要求有相同的結構( scheme),但是最好保持相同,這樣有利於提高搜索效率。

4.type類型
Document可以分組,比如 shouji 這個index裏面,可以按系統分組(安卓和ios),也可以按品牌分組(小米和華爲)。這種分組就叫做Type,它是虛擬的邏輯分組,用來過濾Document.
不同的Type應該有相似的結構(schema),舉例來說,id字段不能在這個組是字符串,在另一個組是數值。這是與關係型數據庫的表的一個區別。性質完全不同的數據(比如products和logs)應該存成兩個index,而不是一個index裏面的兩個Type(雖然可以做到)根據 elastcisearch官方規劃, Elastic6.x版只允許每個Index包含個type, 7.x版將會徹底移除Type. 因此在6.x版本中,我們將type統一設置爲默認值_doc.


II.操作ES

瀏覽器:127.0.0.1:5601 找到Dev Tools

1.集羣運行狀況檢查

GET /_cat/health?v

2.獲取集羣中的節點列表

GET /_cat/nodes?v

3.列出所有索引

GET /_cat/indices?v

III.index索引操作
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-create-index.html
1.創建索引
1.1新建 Index,可以直接向 Elastic服務器發出PUT請求。下面的例子是新建一個名叫 goods的 Index。

PUT /goods

服務器返回一個JSON對象,裏面的acknowledged字段表示操作成功

{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "goods"
}

命名規則:
僅小寫
不能包括\ / * ? " < > | ``(空格字符) , #
7.0之前的索引可能包含冒號(:),但已過時,並且在7.0+中不支持
不能以 - _ + 開始
不能爲.或..
不能超過255個字節

1.2創建索引的同時指定索引配置參數

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

相關參數說明:
"number_of_shards" : 3,//分片數
"number_of_replicas" : 2//每個分片的備份數
"field1" : { "type" : "text" }//字段類型

2.刪除索引
delete請求 刪除index

DELETE /goods

IV.Document文檔操作
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html
1. 創建文檔

1.1 手動指定id, body必填

PUT /customer/_doc/1 
{
    "name" : "zhangsan",
    "age" : 18
}

返回結果:

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

1.2 自動指定id, body必填

POST /customer/_doc/ 
{
  "name" : "lisi",
  "age" : 23
}

返回結果:

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "Mnpky28BnRKpK68FOSu4",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}

您可以使用_doc或_create資源爲新的JSON文檔建立索引。使用 _create保證僅在文檔不存在時纔對其建立索引。要更新現有文檔,必須使用該_doc資源。
如果指定的索引不存在,默認情況下索引操作將自動創建它並應用任何配置的索引模板。如果不存在映射,則索引操作創建一個動態映射。默認情況下,如果需要,新字段和對象將自動添加到映射中。
自動索引創建由該action.auto_create_index 設置控制。此設置默認爲true,它允許自動創建任何索引。您可以修改此設置以顯式允許或阻止與指定模式匹配的索引的自動創建,或者將其設置爲 false完全禁用自動索引的創建。指定您要允許的模式的逗號分隔列表,或在每個模式之前加上 +或 - 以指示是應允許還是禁止它。指定列表後,默認行爲是不允許。

PUT _cluster/settings
{
    "persistent": {
        "action.auto_create_index": "twitter,index10,-index1*,+ind*"① 
    }
}

允許自動創建名爲twitter或index10的索引,阻止創建與index1*模式匹配的索引,並允許創建與ind*模式匹配的任何其他索引。模式是按照指定的順序匹配的。

PUT _cluster/settings
{
    "persistent": {
        "action.auto_create_index": "false" ②
    }
}

完全禁用自動創建索引。

PUT _cluster/settings
{
    "persistent": {
        "action.auto_create_index": "true" ③
    }
}

允許自動創建任何索引。這是默認設置。

您可以通過使用_create資源或將op_type參數設置爲create來強制執行創建操作。在這種情況下,如果索引中已經存在具有指定ID的文檔,則索引操作將失敗。


2. 查詢文檔

GET /customer/_doc/1

3.修改文檔

put 更新時原有的值也要傳入,否則沒有傳入字段的就消失了

PUT /customer/_doc/1/
{
    "name" : "zhangsan",
      "age" : 30
}

3.2 POST

POST /customer/_update/1
{
    "name" : "123",
    "age" : 30
}

報錯爲x_content_parse_exception
解決:

POST /customer/_update/1
{
      "doc" : {
          "name" : "123",
          "age" : 30
      }
}

es文檔的局部更新其實是在更新時傳遞一個叫doc的文檔對象參數,裏面寫着你要修改的文檔json數據,然後到了es的內部,把舊文檔讀出來,文檔參數和舊文檔做一個數據上的合併,字段相同就覆蓋,字段不同就新增,生成新的文檔,把舊文檔標記成deleted,新文檔重新寫入es

4. 刪除文檔

DELETE /customer/_doc/1

5.批量操作

5.1 批量添加 更新 刪除

POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }

5.2 批量查找

重新創建一個
POST /goods/_doc/_bulk
{"index" : {"_id" : 1}}
{"title": "MI5", "des" : "zuixinxMI5"}
{"index" : {"_id" : 2}}
{"title": "iphonex", "des" : "zuixinxiphonex"}
{"index" : {"_id" : 3}}
{"title": "meizu6pro", "des" : "zuixinxmeizu6pro"}

查詢
GET /goods/_doc/_mget
{"ids" : [1,2,3]}

V.基本搜索

GET /goods/_search

查詢結果:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "title" : "MI5",
          "des" : "zuixinxMI5"
        }
      },
      {
        "_index" : "goods",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "title" : "iphonex",
          "des" : "zuixinxiphonex"
        }
      }
    ]
  }
}

查詢結果說明:
took - Elasticsearch執行搜索的時間 (以毫秒爲單位)
timed-out - 告訴我們搜索是否超時
_shards - 告訴我們搜索了多少個分片,以及搜索成功/失敗分片的計數
hits - 搜索結果
hits.total - 符合我們搜索條件的文檔總數
hits.hits - 實際的搜索結果數組(默認爲前10個文檔)
hits.sort - 對結果進行排序(如果按分數排序則丟失)
hits.score_max_score - 暫時忽略這些字段

真的標題搜索:

GET /goods/_search?q=title:MI5

查詢所有:

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

查詢MI5

GET /goods/_search
{
    "query" : {
        "match" : {"title" : "MI5"}
    }
}

查詢其它
官方文檔:https://www.elastic.co/guide/en/elasticsearch/reference/current/search.html
 

GET /goods/_search
{
    "query" : {
        "term" : { "title" : "MI5" }
    }
}
GET /goods/_search
{
    "from" : 0, 
    "size" : 10,
    "sort" : {"id" : "asc"}
    "query" : {
        "term" : {"title" : "MI5"}
    }
}

from--size 相當於mysql的limit
sort--排序

發佈了104 篇原創文章 · 獲贊 52 · 訪問量 30萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章