Elasticsearch 7.x 深入【1】索引【四】常用屬性

1.借鑒

極客時間 阮一鳴老師的Elasticsearch核心技術與實戰
Elasticsearch 參考指南(映射參數enabled)
[翻譯]Elasticsearch重要文章之五:預加載fielddata
Elasticsearch學習之圖解Elasticsearch中的_source、_all、store和index屬性
elasticsearch 中的store 以及倒排索引的問題
Elasticsearch 關于store字段的處理
elasticsearch搜索過程分析

2. 開始

Dynamic

  • dynamic控制著索引的文檔是否可包含新增字段,默認為true。
true false strict
文檔可被索引
字段可被索引
_mapping可被更新
False
  • 我們試一下,設置dynamic為false
PUT /my_movies 
{
  "mappings": {
    "dynamic": false,
    "properties": {
      "name": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      }
    }
  }
}
  • 添加一篇文檔,帶有mapping中沒有指定的字段age
PUT /my_movies/_doc/1
{
  "name": "caiser",
  "content": "Hello Hello",
  "age": 99
}
  • 添加成功后再看一下mapping
{
  "my_movies" : {
    "mappings" : {
      "dynamic" : "false",
      "properties" : {
        "content" : {
          "type" : "text"
        },
        "name" : {
          "type" : "keyword"
        }
      }
    }
  }
}
  • 結果表明,設置為false后,文檔被索引了,但是mapping并沒有更新
    -我們再通過age查詢一下,看看字段是否被索引
GET /my_movies/_search?q=age:99
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 0,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}
  • 返回結果是空的,說明設置為false,字段不會被索引
Strict
  • 接下來我們試一下,設置dynamic為strict
DELETE /my_movies

PUT /my_movies 
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "name": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      }
    }
  }
}
  • 我們嘗試添加一篇文檔,文檔中包含mapping定義中不存在的屬性
PUT /my_movies/_doc/1
{
  "name": "caiser",
  "content": "Hello Hello",
  "age": 99
}
  • 直接報錯了
{
  "error": {
    "root_cause": [
      {
        "type": "strict_dynamic_mapping_exception",
        "reason": "mapping set to strict, dynamic introduction of [age] within [_doc] is not allowed"
      }
    ],
    "type": "strict_dynamic_mapping_exception",
    "reason": "mapping set to strict, dynamic introduction of [age] within [_doc] is not allowed"
  },
  "status": 400
}
  • 由此可見,設置dynamic為strict時,如果索引mapping中不存在的字段,會直接報錯

null_value

  • 需要對null值進行搜索
  • 只有keyword類型支持設置為null_value

例子

  • 我們來驗證一下,為text類型設置null_value
DELETE /my_movies

PUT /my_movies 
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "content": {
        "type": "text",
        "null_value": "null"
      }
    }
  }
}
  • 為text類型設置null_value則會報以下錯誤
{
  "error": {
    "root_cause": [
      {
        "type": "mapper_parsing_exception",
        "reason": "Mapping definition for [content] has unsupported parameters:  [null_value : null]"
      }
    ],
    "type": "mapper_parsing_exception",
    "reason": "Failed to parse mapping [_doc]: Mapping definition for [content] has unsupported parameters:  [null_value : null]",
    "caused_by": {
      "type": "mapper_parsing_exception",
      "reason": "Mapping definition for [content] has unsupported parameters:  [null_value : null]"
    }
  },
  "status": 400
}
  • 如果為keyword設置,則可以成功
DELETE /my_movies

PUT /my_movies 
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "null_value": "null"
      },
      "content": {
        "type": "text"
      }
    }
  }
}
  • 我們來添加數據并且查詢一下:
# 添加兩篇文檔
PUT /my_movies/_doc/1
{
  "content": "123",
  "name": null
}
PUT /my_movies/_doc/2
{
  "content": "123456"
}

# 查詢一下
GET /my_movies/_search
{
  "query": {
    "term": {
      "name": {
        "value": "null"
      }
    }
  }
}
  • 查詢結果
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_movies",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "content" : "123",
          "name" : null
        }
      }
    ]
  }
}

Copy To

  • copy_to將字段的數知拷貝到目標字段
  • copy_to的目標字段不出現在_source中
DELETE /my_users
# 創建索引
PUT /my_users
{
  "mappings": {
    "properties": {
      "fristName": {
        "type": "text",
        "copy_to": "fullName"
      },
      "lastName": {
        "type": "text",
        "copy_to": "fullName"
      }
    }
  }
}

# 索引文檔
PUT /my_users/_doc/1
{
  "fristName": "sun",
  "lastName": "ruikai"
}

# 查詢
GET /my_users/_search
{
  "query": {
    "match": {
      "fullName": {
        "query": "sun ruikai",
        "operator": "and"
      }
    }
  }
}
  • 查詢結果
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "my_users",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "fristName" : "sun",
          "lastName" : "ruikai"
        }
      }
    ]
  }
}

doc_values & fielddata

doc_values fielddata
何時創建 索引時,和倒排索引一起創建 搜索是動態創建
創建位置 磁盤文件 JVM內存
優點 避免大量內存占用 索引速度快,不占用額外的磁盤空間
缺點 降低索引速度,占用額外的磁盤空間 文檔過多,動態創建開銷大,占用過多JVM內存
缺省值 true false
  • 如果keyword字段無需排序和聚合,可以設置doc_values: false,可以增加索引的速度,減少磁盤使用量,如果重新打開,需要重建索引
  • 如果text字段需要排序和聚合,需要設置fielddata: true

enable

如果一個字段不需要被檢索,排序以及集合分析,enable設置為false
需要注意的是:enabled只能設置在頂層mapping中,以及type為object的屬性中
以下兩種為合法的設置

DELETE my_movies
PUT /my_movies
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      },
      "url": {
        "enabled": false,
        "type": "object"
      }
    }
  }
}
DELETE my_movies
PUT /my_movies
{
  "mappings": {
    "enabled": false,
    "properties": {
      "name": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      },
      "url": {
        "type": "object"
      }
    }
  }
}

eager_global_ordinals

預加載
如果更新頻繁,聚合查詢頻繁的keyword類型的字段推薦將該選項設置為true

DELETE my_movies
PUT /my_movies
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "eager_global_ordinals": true
      },
      "content": {
        "type": "text"
      },
      "url": {
        "type": "object"
      }
    }
  }
}

_source && index && store 圖例

_source

翻譯官網如下:

_source字段包含索引時傳遞的原始JSON文檔主體。_source字段本身沒有索引(因此不能搜索),但是會被存儲,以便在執行fetch請求(如get或search)時返回。

設置_source為false可節約磁盤,適用于指標型數據,一般優先考慮增加壓縮比(index.codec),但是關閉了_source就不支持以下操作

  • update, update_by_query, reindex
  • 高亮
  • 無法在_source字段中獲得

我們可以指定_source全部禁用,或者指定包含哪些,不包含哪些
舉個栗子

# 全部禁用_source
PUT /song_of_ice_and_fire
{
  "mappings": {
    "_source": {
      "enabled": false
    }, 
    "properties": {
      "title": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      }
    }
  }
}

# 包含title,不包含content
PUT /song_of_ice_and_fire
{
  "mappings": {
    "_source": {
      "includes": ["title"],
      "excludes": ["content"]
    }, 
    "properties": {
      "title": {
        "type": "keyword"
      },
      "content": {
        "type": "text"
      }
    }
  }
}

store

翻譯官網如下:

默認情況下,字段值被索引以使其可搜索,但不存儲它們。這意味著可以查詢字段,但不能檢索原始字段值。

通常這并不重要。字段值已經是_source字段的一部分,該字段默認存儲。如果只想檢索單個字段或幾個字段的值,而不是整個_source,那么可以通過源過濾來實現。

在某些情況下,存儲字段是有意義的。例如,如果你有一個有標題的文檔,一個日期,和一個非常大的內容字段,你可能想檢索僅僅標題和日期,而不必從一個大_source字段提取這些字段

store屬性用于指定原始字段是否存儲,一般不與_source中的字段重疊

PUT /song_of_ice_and_fire
{
  "mappings": {
    "_source": {
      "includes": ["title"],
      "excludes": ["content"]
    }, 
    "properties": {
      "title": {
        "type": "keyword"
      },
      "content": {
        "type": "text",
        "store": true
      }
    }
  }
}

Index

  • index的設置控制著字段是否被索引,默認為true
true false
是否會創建倒排索引
字段是否可被搜索
  • 我們舉個栗子,設置name的index屬性為false
DELETE /my_movies

PUT /my_movies 
{
  "mappings": {
    "properties": {
      "name": {
        "type": "keyword",
        "index": false
      },
      "content": {
        "type": "text"
      }
    }
  }
}
  • 索引一篇文檔
PUT /my_movies/_doc/1
{
  "name": "caiser",
  "content": "Hello Hello",
  "age": 99
}
  • 查詢一下
GET /my_movies/_search
{
  "query": {
    "term": {
      "name": {
        "value": "caiser"
      }
    }
  }
}
  • 結果直接報錯了,es的返回也說明了問題:“Cannot search on field [name] since it is not indexed.”
{
  "error": {
    "root_cause": [
      {
        "type": "query_shard_exception",
        "reason": "failed to create query: {\n  \"term\" : {\n    \"name\" : {\n      \"value\" : \"caiser\",\n      \"boost\" : 1.0\n    }\n  }\n}",
        "index_uuid": "uLkZEGRuRCKVWyik8Z8VCQ",
        "index": "my_movies"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "my_movies",
        "node": "M4LyTpueT--40-oJaXKvfA",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: {\n  \"term\" : {\n    \"name\" : {\n      \"value\" : \"caiser\",\n      \"boost\" : 1.0\n    }\n  }\n}",
          "index_uuid": "uLkZEGRuRCKVWyik8Z8VCQ",
          "index": "my_movies",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Cannot search on field [name] since it is not indexed."
          }
        }
      }
    ]
  },
  "status": 400
}

Index Option

  • index_option控制者倒排索引記錄的級別
序號 級別 描述
1 doc 記錄doc id
2 freqs 記錄doc id 和 term 頻率
3 positions 記錄doc id,term頻率,term位置
4 offsets 記錄doc id,term頻率,term位置,字符偏移量
  • text 默認級別為positions,其他默認為doc

3. 大功告成

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,825評論 6 546
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,814評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,980評論 0 384
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 64,064評論 1 319
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,779評論 6 414
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 56,109評論 1 330
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,099評論 3 450
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,287評論 0 291
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,799評論 1 338
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,515評論 3 361
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,750評論 1 375
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,221評論 5 365
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,933評論 3 351
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,327評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,667評論 1 296
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,492評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,703評論 2 380