ElasticSearch权威指南学习(索引管理)
生活随笔
收集整理的這篇文章主要介紹了
ElasticSearch权威指南学习(索引管理)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
創(chuàng)建索引
- 當(dāng)我們需要確保索引被創(chuàng)建在適當(dāng)數(shù)量的分片上,在索引數(shù)據(jù)之前設(shè)置好分析器和類型映射。
- 手動創(chuàng)建索引,在請求中加入所有設(shè)置和類型映射,如下所示:
PUT /my_index
{
"settings": { ... any settings ... },
"mappings": {
"type_one": { ... any mappings ... },
"type_two": { ... any mappings ... },
...
}
- 你可以通過在 config/elasticsearch.yml 中添加下面的配置來防止自動創(chuàng)建索引。
action.auto_create_index: false
刪除索引
- 使用以下的請求來刪除索引:
DELETE /my_index
- 用下面的方式刪除多個索引
DELETE /index_one,index_two
DELETE /index_*
- 甚至可以刪除所有索引
DELETE /_all
索引設(shè)置
- 下面是兩個最重要的設(shè)置:
- number_of_shards
- 定義一個索引的主分片個數(shù),默認(rèn)值是
5。這個配置在索引創(chuàng)建后不能修改。
- 定義一個索引的主分片個數(shù),默認(rèn)值是
- number_of_replicas
- 每個主分片的復(fù)制分片個數(shù),默認(rèn)是
1。這個配置可以隨時在活躍的索引上修改。
- 每個主分片的復(fù)制分片個數(shù),默認(rèn)是
- number_of_shards
- 例如,我們可以創(chuàng)建只有一個主分片,沒有復(fù)制分片的小索引。
PUT /my_temp_index
{
"settings": {
"number_of_shards" : 1,
"number_of_replicas" : 0
}
}
- 然后,我們可以用 update-index-settings API 動態(tài)修改復(fù)制分片個數(shù)
PUT /my_temp_index/_settings
{
"number_of_replicas": 1
}
配置分析器
- 第三個重要的索引設(shè)置是 analysis 部分,用來配置已存在的分析器或創(chuàng)建自定義分析器來定制化你的索引。
- 在下面的例子中,我們創(chuàng)建了一個新的分析器,叫做 es_std,并使用預(yù)定義的西班牙語停用詞:
PUT /spanish_docs
{
"settings": {
"analysis": {
"analyzer": {
"es_std": {
"type": "standard",
"stopwords": "_spanish_"
}
}
}
}
}
- es_std 分析器不是全局的,它僅僅存在于我們定義的 spanish_docs 索引中
自定義分析器
- 雖然 Elasticsearch 內(nèi)置了一系列的分析器,但是真正的強(qiáng)大之處在于定制你自己的分析器。你可以通過在配置文件中組合字符過濾器,分詞器和標(biāo)記過濾器,來滿足特定數(shù)據(jù)的需求。
創(chuàng)建自定義分析器
- 與索引設(shè)置一樣,我們預(yù)先配置好 es_std 分析器,我們可以再 analysis 字段下配置字符過濾器,分詞器和標(biāo)記過濾器:
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": { ... custom character filters ... },
"tokenizer": { ... custom tokenizers ... },
"filter": { ... custom token filters ... },
"analyzer": { ... custom analyzers ... }
}
}
}
- 作為例子,我們來配置一個這樣的分析器:
- 用 html_strip 字符過濾器去除所有的 HTML 標(biāo)簽
- 將 & 替換成 and,使用一個自定義的 mapping 字符過濾器
"char_filter": {
"&_to_and": {
"type": "mapping",
"mappings": [ "&=> and "]
}
}
- 使用 standard 分詞器分割單詞
- 使用 lowercase 標(biāo)記過濾器將詞轉(zhuǎn)為小寫
- 用 stop 標(biāo)記過濾器去除一些自定義停用詞。
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords": [ "the", "a" ]
}
}
- 根據(jù)以上描述來將預(yù)定義好的分詞器和過濾器組合成我們的分析器:
"analyzer": {
"my_analyzer": {
"type": "custom",
"char_filter": [ "html_strip", "&_to_and" ],
"tokenizer": "standard",
"filter": [ "lowercase", "my_stopwords" ]
}
}
- 用下面的方式可以將以上請求合并成一條:
PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"&_to_and": {
"type": "mapping",
"mappings": [ "&=> and "]
}},
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords": [ "the", "a" ]
}},
"analyzer": {
"my_analyzer": {
"type": "custom",
"char_filter": [ "html_strip", "&_to_and" ],
"tokenizer": "standard",
"filter": [ "lowercase", "my_stopwords" ]
}}
}}}
- 然后查看下(es5.0版本后的查詢格式)
GET /my_index/_analyze
{
"analyzer": "my_analyzer",
"text":"The quick & brown fox"
}
- 5.0前老版本
GET /my_index/_analyze?analyzer=my_analyzer
The quick & brown fox
- 結(jié)果
{
"tokens": [
{
"token": "quick",
"start_offset": 4,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "and",
"start_offset": 10,
"end_offset": 11,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "brown",
"start_offset": 12,
"end_offset": 17,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "fox",
"start_offset": 18,
"end_offset": 21,
"type": "<ALPHANUM>",
"position": 4
}
]
}
元數(shù)據(jù):_source 字段
- 在搜索請求中你可以通過限定 _source 字段來請求指定字段:
GET /_search
{
"query": { "match_all": {}},
"_source": [ "title", "created" ]
}
- 元數(shù)據(jù):_all 字段
- 如果你決定不再使用 _all 字段,你可以通過下面的映射禁用它:
PUT /my_index/_mapping/my_type
{
"my_type": {
"_all": { "enabled": false }
}
}
默認(rèn)映射
- 我們可以使用 default 映射對所有類型禁用 _all 字段,而只在 blog 字段上開啟它:
PUT /my_index
{
"mappings": {
"_default_": {
"_all": { "enabled": false }
},
"blog": {
"_all": { "enabled": true }
}
}
}
- default 映射也是定義索引級別的動態(tài)模板的好地方。
總結(jié)
- 一口氣學(xué)到這里,這章開始已經(jīng)有點(diǎn)力不從心了,很多東西已經(jīng)理解不了了,需要實(shí)際工作中,不斷查找資料深入學(xué)習(xí)理解才能掌控了,索引管理的內(nèi)容這里并不全面,我理解不了的地方這里我也不寫了~
參考 https://es.xiaoleilu.com/070_Index_Mgmt/25_Mappings.html
總結(jié)
以上是生活随笔為你收集整理的ElasticSearch权威指南学习(索引管理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [PAMI2013] Guided Im
- 下一篇: 总结 | 计算机视觉领域最常见几中损失函