第09篇-Elasticsearch中構(gòu)建自定義分析器

我的Elasticsearch系列文章,逐漸更新中,歡迎關(guān)注

0A.關(guān)于Elasticsearch及實例應(yīng)用

00.Solr與ElasticSearch對比

01.ElasticSearch能做什么?

02.Elastic Stack功能介紹

03.如何安裝與設(shè)置Elasticsearch API

04.如果通過elasticsearch的head插件建立索引_CRUD操作

05.Elasticsearch多個實例和head plugin使用介紹

06.當(dāng)Elasticsearch進(jìn)行文檔索引時,它是怎樣工作的?

07.Elasticsearch中的映射方式—簡潔版教程

08.Elasticsearch中的分析和分析器應(yīng)用方式

09.Elasticsearch中構(gòu)建自定義分析器

10.Kibana科普-作為Elasticsearhc開發(fā)工具

11.Elasticsearch查詢方法

另外ES入門,我強(qiáng)烈推薦Elasticsearch基礎(chǔ)入門教程給你,非常想盡的入門指南手冊。

介紹

在此階段的上一篇博客中,我已經(jīng)解釋了有關(guān)常規(guī)分析器結(jié)構(gòu)和組件的更多信息。我也解釋了每個組件的功能。在此博客中,我們將通過構(gòu)建自定義分析器,然后查詢并查看差異來了解實現(xiàn)方面。

定制分析儀的外殼

因此,讓我們考慮定制分析儀的情況。假設(shè)我們輸入到Elasticsearch的文本包含以下內(nèi)容

1. html標(biāo)簽

html標(biāo)簽在索引時可能會出現(xiàn)在我們的文本中,其實這在大多數(shù)情況下是不需要的。所以我們需要刪除這些。

2.停止詞

像the,and,or等這樣的詞,在搜索內(nèi)容時意義不大,一般被稱為停止詞。

3.大寫字母。

4.簡寫形式如`H2O、$、%`。

在某些情況下,像這樣的簡式應(yīng)該用英文原詞代替。

應(yīng)用自定義分析器

在上面的示例文本中,下表列出了需要執(zhí)行的操作以及自定義分析器的相應(yīng)組件

```

Arun has 100 $ which accounts to 3 % of the total <h2> money </h2>

```

“ settings”中的層次結(jié)構(gòu)如下所示:


應(yīng)用所有組件

現(xiàn)在應(yīng)用上述所有組件創(chuàng)建一個自定義分析器,如下所示:

curl -XPUT localhost:9200/testindex_0204 -d '{

? "settings": {

? ? "analysis": {

? ? ? "char_filter": {

? ? ? ? "subsitute": {

? ? ? ? ? "type": "mapping",

? ? ? ? ? "mappings": [

? ? ? ? ? ? "$=> dollar",

? ? ? ? ? ? "%=> percentage"

? ? ? ? ? ]

? ? ? ? },

? ? ? ? "html-strip": {

? ? ? ? ? "type": "html_strip"

? ? ? ? }

? ? ? },

? ? ? "tokenizer": "standard",

? ? ? "filter": {

? ? ? ? "stopwords_removal": {

? ? ? ? ? "type": "stop",

? ? ? ? ? "stopwords": [

? ? ? ? ? ? "has",

? ? ? ? ? ? "which",

? ? ? ? ? ? "to",

? ? ? ? ? ? "of",

? ? ? ? ? ? "the"

? ? ? ? ? ]

? ? ? ? }

? ? ? },

? ? ? "analyzer": {

? ? ? ? "custom_analyzer_type_01": {

? ? ? ? ? "type": "custom",

? ? ? ? ? "char_filter": [

? ? ? ? ? ? "subsitute",

? ? ? ? ? ? "html_strip"

? ? ? ? ? ],

? ? ? ? ? "tokenizer": "standard",

? ? ? ? ? "filter": [

? ? ? ? ? ? "stopwords_removal",

? ? ? ? ? ? "lowercase"

? ? ? ? ? ]

? ? ? ? }

? ? ? }

? ? }

? },

? "mappings": {

? ? "test_type": {

? ? ? "properties": {

? ? ? ? "text": {

? ? ? ? ? "type": "string",

? ? ? ? ? "analyzer": "custom_analyzer_type_01"

? ? ? ? }

? ? ? }

? ? }

? }

}'

這將使用名為“ custom_analyzer_01” 的自定義分析器創(chuàng)建索引。

詳細(xì)說明了此映射

使用自定義分析器生成令牌

使用分析器可以看到使用此分析器生成的令牌,如下所示:

```

curl -XGET "localhost:9200/testindex_0204/_analyze?analyzer=custom_analyzer_type_01&pretty=true" -d 'Arun has 100 $ which accounts to 3 % of the total <h2> money </h2>'

```

令牌列表如下:


在這里您可以進(jìn)行一些觀察:

令牌號3和6最初是$ 和%,但隨后如本節(jié)中所指定的那樣被替換為“ dollar”和“ percentage” char_filter 。

還有html標(biāo)記`<h2> ,</h2>`也被html_strip 過濾器從令牌列表中刪除

過濾器 "to","the","which","has"中提到的術(shù)語等stopwords 已從令牌列表中刪除。令牌編號1最初看起來應(yīng)該像是“ Arun”,但已被應(yīng)用的過濾器小寫。

結(jié)論

在此博客中,我們看到了如何構(gòu)建自定義分析器并將其應(yīng)用于Elasticsearch中的字段。通過這個博客,我打算結(jié)束博客系列的第二階段(索引,映射和分析)。從現(xiàn)在開始,此階段是理解Elasticsearch的基礎(chǔ)部分之一,我們可能會將此階段的輸入用于許多目的。從階段03開始,我將向您介紹elasticsearch的查詢DSL世界。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容