文章目錄
概念
cluster
集羣,一個ES集羣由一個或多個節點(Node)組成,每個集羣都有一個cluster name作爲標識.
node
一個節點是你集羣中的一個服務器,作爲集羣的一部分,它存儲你的數據,參與集羣的索引和搜索功能。 和集羣類似, 一個節點也是由一個名字來標識的, 默認情況下, 這個名字是一個隨機的Marvel角色的名字,這個名字會在節點啓動時分配給它。這個名字對於管理工作來說很重要,因爲在這個管理過程中,你會去確定網絡中的哪些 服務器對應於Elasticsearch集羣中的哪些節點。
index
一個索引就是一個擁有相似特徵的文檔的集合。比如說,你可以有一個客戶數據的索引,另一個產品目錄的索引,還有一個訂單數據的索引。一個索引由一個名字來 標識(必須全部是小寫字母的),並且當我們要對這個索引中的文檔進行索引、搜索、更新和刪除的時候,都要使用到這個名字。在一個集羣中,你能夠創建任意多個索引。
type
在一個索引中,你可以定義一種或多種類型。一個類型是你的索引的一個邏輯上的分類/分區,其語義完全由你來定。通常,會爲具有一組相同字段的文檔定義一個類型。比如說,我們假設你運營一個博客平臺 並且將你所有的數據存儲到一個索引中。在這個索引中,你可以爲用戶數據定義一個類型,爲博客數據定義另一個類型,當然,也可以爲評論數據定義另一個類型。
shard
1.分片,ES是分佈式搜索引擎,每個索引有一個或多個分片,索引的數據被分配到各個分片上,相當於一桶水用了N個杯子裝。
2.ES是分佈式搜索引擎,每個索引有一個或多個分片,索引的數據被分配到各個分片上,相當於一桶水用了N個杯子裝。
3.分片是獨立的,對於一個Search Request的行爲,每個分片都會執行這個Request。
4.分片是獨立的,對於一個Search Request的行爲,每個分片都會執行這個Request。
replica
1.複製,可以理解爲備份分片,相應地有primary shard(主分片)。
2.主分片和備分片不會出現在同一個節點上(防止單點故障),默認情況下一個索引創建5個分片一個備份(5主+5從=10分片)
3.如果你只有一個節點,那麼5個replica都無法分配(unassigned),此時cluster status會變成Yellow
四種查詢模式
query and fetch
向索引的所有分片 ( shard)都發出查詢請求, 各分片返回的時候把元素文檔 ( document)和計算後的排名信息一起返回。
這種搜索方式是最快的。 因爲相比下面的幾種搜索方式, 這種查詢方法只需要去 shard查詢一次。 但是各個 shard 返回的結果的數量之和可能是用戶要求的 size 的 n 倍。
優點:這種搜索方式是最快的。因爲相比後面的幾種es的搜索方式,這種查詢方法只需要去shard查詢一次。
缺點:返回的數據量不準確, 可能返回(N*分片數量)的數據並且數據排名也不準確,同時各個shard返回的結果的數量之和可能是用戶要求的size的n倍。
query then fetch( es 默認的搜索方式)
如果你搜索時, 沒有指定搜索方式, 就是使用的這種搜索方式。 這種搜索方式, 大概分兩個步驟:
第一步, 先向所有的 shard 發出請求, 各分片只返回文檔 id(注意, 不包括文檔 document)和排名相關的信息(也就是文檔對應的分值), 然後按照各分片返回的文檔的分數進行重新排序和排名, 取前 size 個文檔。
第二步, 根據文檔 id 去相關的 shard 取 document。 這種方式返回的 document 數量與用戶要求的大小是相等的。
優點:
返回的數據量是準確的。
缺點:
性能一般,並且數據排名不準確。
DFS query and fetch
這種方式比第一種方式多了一個 DFS 步驟,有這一步,可以更精確控制搜索打分和排名。也就是在進行查詢之前, 先對所有分片發送請求, 把所有分片中的詞頻和文檔頻率等打分依據全部彙總到一塊, 再執行後面的操作、
優點:
數據排名準確
缺點:
性能一般
返回的數據量不準確, 可能返回(N*分片數量)的數據
####DFS query then fetch
比第 2 種方式多了一個 DFS 步驟。
也就是在進行查詢之前, 先對所有分片發送請求, 把所有分片中的詞頻和文檔頻率等打分依據全部彙總到一塊, 再執行後面的操作、
優點:
返回的數據量是準確的
數據排名準確
缺點:
性能最差【 這個最差只是表示在這四種查詢方式中性能最慢, 也不至於不能忍受,如果對查詢性能要求不是非常高, 而對查詢準確度要求比較高的時候可以考慮這個】
請求
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
關鍵字 | 解釋說明 |
---|---|
VERB |
適當的 HTTP 方法 或 謂詞 : GET 、 POST 、 PUT 、 HEAD 或者 DELETE 。 |
PROTOCOL |
http 或者 https (如果你在 Elasticsearch 前面有一個 https 代理) |
HOST |
Elasticsearch 集羣中任意節點的主機名,或者用 localhost 代表本地機器上的節點。 |
PORT |
運行 Elasticsearch HTTP 服務的端口號,默認是 9200 。 |
PATH |
API 的終端路徑(例如 _count 將返回集羣中文檔數量)。Path 可能包含多個組件,例如:_cluster/stats 和 _nodes/stats/jvm 。 |
QUERY_STRING |
任意可選的查詢字符串參數 (例如 ?pretty 將格式化地輸出 JSON 返回值,使其更容易閱讀) |
BODY |
一個 JSON 格式的請求體 (如果請求需要的話) |
增
插入一條記錄
// 說明
索引 : megacorp
類型名稱 : employee
特定僱員id : 1
// 輸入
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
// 輸出
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
只創建,不更新
// 輸入
POST /megacorp/employee/1?op_type=create
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 26,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
或者
// 輸入
POST /megacorp/employee/1?_create
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 26,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
刪
// 輸入
DELETE /megacorp/employee/1
// 輸出
{
"_index": "megacorp",
"_type": "employee",
"_id": "3",
"_version": 2,
"result": "deleted",
"_shards":
{
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
改
修改一條記錄
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 26,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
####部分更新
// 輸入 doc爲關鍵字
POST /megacorp/employee/1/_update
{
"doc": {
"tags": ["test"],
"views": 0
}
}
查
檢索文檔
// 輸入
GET /megacorp/employee/1
// 輸出
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_version": 1,
"_seq_no": 0,
"_primary_term": 1,
"found": true,
"_source":
{
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": ["sports", "music"]
}
}
輕量搜索
// 輸入
GET /megacorp/employee/_search
// 輸出
{
"took":70,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":2,
"relation":"eq"
},
"max_score":1,
"hits":[
{
"_index":"megacorp",
"_type":"employee",
"_id":"1",
"_score":1,
"_source":{
"first_name":"John",
"last_name":"Smith",
"age":25,
"about":"I love to go rock climbing",
"interests":[
"sports",
"music"
]
}
},
{
"_index":"megacorp",
"_type":"employee",
"_id":"2",
"_score":1,
"_source":{
"first_name":"Jane",
"last_name":"Smith",
"age":32,
"about":"I like to collect rock albums",
"interests":[
"music"
]
}
}
]
}
}
條件搜索
// 輸入
curl -X GET "localhost:9200/megacorp/employee/_search?q=first_name:John&pretty"
or
GET /megacorp/employee/_search?q=first_name:John
// 輸出
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808292,
"hits" : [
{
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 0.9808292,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [
"sports",
"music"
]
}
}
]
}
}
條件搜索-過濾器
// 輸入
GET /megacorp/employee/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"last_name" : "smith"
}
},
"filter": {
"range" : {
"age" : { "gt" : 30 }
}
}
}
}
}
條件搜索-返回需要的字段結果
GET /megacorp/employee/_search?_source=first_name,last_name
單值精確匹配
GET /role/_search
{
"query": {
"term": {
"age": 24
}
}
}
單值模糊匹配
GET /role/_search
{
"query": {
"match": {
"age": 24
}
}
}
多值匹配
GET /role/_search
{
"query": {
"multi_match": {
"query": "JK",
"fields": [
"name",
"p_info.m",
"p_info.d_info.s",
"p_info.d"
],
"operator": "and"
}
}
}
Bool查詢
GET /role/_search
{
"query": {"bool": {"must": [
{"term": {
"name.keyword": {
"value": "Lily"
}
}}
]}}
}
聚合查詢-max、min、avg、sum、stats
GET /role/_search
{
"aggs": {
"group_sum":{
"avg": {
"field": "age"
}
}
}
}
分組查詢
GET /role/_search
{
"aggs": {
"group_by_age":{
"terms": {
"field": "age",
"size": 10
}
}
}
}
去重查詢
GET /role/_search
{
"aggs": {
"agg_card": {
"cardinality": {
"field": "age"
}
}
}
}
###關鍵字
// 精確匹配
match_phrase
// 高亮
highlight
// 腳本
script
Groovy腳本編程
POST /megacorp/employee/1/_update
{
"script" : "ctx._source.views+=views",
"params" : {
"views" : "search"
}
}
問題解決
Set “Area” fielddata=True問題
PUT /py-operator-log/_mapping/doc/
{
"properties":{
"Area":{
"type": "text",
"fielddata": true
}
}
}