
- Elasticsearch 教程
- Elasticsearch - 首頁
- Elasticsearch - 基本概念
- Elasticsearch - 安裝
- Elasticsearch - 資料填充
- 版本遷移
- Elasticsearch - API 約定
- Elasticsearch - 文件 API
- Elasticsearch - 搜尋 API
- Elasticsearch - 聚合
- Elasticsearch - 索引 API
- Elasticsearch - CAT API
- Elasticsearch - 叢集 API
- Elasticsearch - 查詢 DSL
- Elasticsearch - 對映
- Elasticsearch - 分析
- Elasticsearch - 模組
- Elasticsearch - 索引模組
- Elasticsearch - Ingest 節點
- Elasticsearch - 管理索引生命週期
- Elasticsearch - SQL 訪問
- Elasticsearch - 監控
- Elasticsearch - 資料彙總
- Elasticsearch - 凍結索引
- Elasticsearch - 測試
- Elasticsearch - Kibana 儀表盤
- Elasticsearch - 按欄位過濾
- Elasticsearch - 資料表
- Elasticsearch - 區域地圖
- Elasticsearch - 餅圖
- Elasticsearch - 面積圖和柱狀圖
- Elasticsearch - 時間序列
- Elasticsearch - 標籤雲
- Elasticsearch - 熱力圖
- Elasticsearch - Canvas
- Elasticsearch - 日誌 UI
- Elasticsearch 有用資源
- Elasticsearch - 快速指南
- Elasticsearch - 有用資源
- Elasticsearch - 討論
Elasticsearch - 分析
在搜尋操作期間處理查詢時,任何索引中的內容都會由分析模組進行分析。此模組包含分析器、分詞器、標記過濾器和字元過濾器。如果未定義分析器,則預設情況下,內建的分析器、標記、過濾器和分詞器將註冊到分析模組。
在以下示例中,我們使用標準分析器,在未指定其他分析器時使用。它將根據語法分析句子並生成句子中使用的單詞。
POST _analyze { "analyzer": "standard", "text": "Today's weather is beautiful" }
執行上述程式碼後,我們將獲得如下所示的響應:
{ "tokens" : [ { "token" : "today's", "start_offset" : 0, "end_offset" : 7, "type" : "", "position" : 0 }, { "token" : "weather", "start_offset" : 8, "end_offset" : 15, "type" : "", "position" : 1 }, { "token" : "is", "start_offset" : 16, "end_offset" : 18, "type" : "", "position" : 2 }, { "token" : "beautiful", "start_offset" : 19, "end_offset" : 28, "type" : "", "position" : 3 } ] }
配置標準分析器
我們可以使用各種引數配置標準分析器以滿足我們的自定義需求。
在以下示例中,我們將標準分析器的 max_token_length 配置為 5。
為此,我們首先建立一個索引,該索引的分析器具有 max_length_token 引數。
PUT index_4_analysis { "settings": { "analysis": { "analyzer": { "my_english_analyzer": { "type": "standard", "max_token_length": 5, "stopwords": "_english_" } } } } }
接下來,我們應用分析器並使用如下所示的文字。請注意標記是如何不出現的,因為它在開頭有兩個空格,在結尾有兩個空格。對於單詞“is”,它開頭有一個空格,結尾有一個空格。將所有這些加起來,它就變成了帶有空格的 4 個字母,這使得它不是一個單詞。至少在開頭或結尾處應該有一個非空格字元,才能使其成為一個要計數的單詞。
POST index_4_analysis/_analyze { "analyzer": "my_english_analyzer", "text": "Today's weather is beautiful" }
執行上述程式碼後,我們將獲得如下所示的響應:
{ "tokens" : [ { "token" : "today", "start_offset" : 0, "end_offset" : 5, "type" : "", "position" : 0 }, { "token" : "s", "start_offset" : 6, "end_offset" : 7, "type" : "", "position" : 1 }, { "token" : "weath", "start_offset" : 8, "end_offset" : 13, "type" : "", "position" : 2 }, { "token" : "er", "start_offset" : 13, "end_offset" : 15, "type" : "", "position" : 3 }, { "token" : "beaut", "start_offset" : 19, "end_offset" : 24, "type" : "", "position" : 5 }, { "token" : "iful", "start_offset" : 24, "end_offset" : 28, "type" : "", "position" : 6 } ] }
下表列出了各種分析器及其說明:
序號 | 分析器及說明 |
---|---|
1 |
標準分析器 (standard) 可以為該分析器設定停用詞和 max_token_length 設定。預設情況下,停用詞列表為空,max_token_length 為 255。 |
2 |
簡單分析器 (simple) 該分析器由小寫分詞器組成。 |
3 |
空格分析器 (whitespace) 該分析器由空格分詞器組成。 |
4 |
停用詞分析器 (stop) 可以配置 stopwords 和 stopwords_path。預設情況下,stopwords 初始化為英語停用詞,stopwords_path 包含停用詞文字檔案的路徑。 |
分詞器
分詞器用於在 Elasticsearch 中從文字生成標記。可以透過考慮空格或其他標點符號將文字分解成標記。Elasticsearch 擁有大量內建分詞器,可用於自定義分析器。
以下顯示了一個分詞器的示例,該分詞器在遇到非字母字元時將文字分解成術語,但它還會將所有術語小寫:
POST _analyze { "tokenizer": "lowercase", "text": "It Was a Beautiful Weather 5 Days ago." }
執行上述程式碼後,我們將獲得如下所示的響應:
{ "tokens" : [ { "token" : "it", "start_offset" : 0, "end_offset" : 2, "type" : "word", "position" : 0 }, { "token" : "was", "start_offset" : 3, "end_offset" : 6, "type" : "word", "position" : 1 }, { "token" : "a", "start_offset" : 7, "end_offset" : 8, "type" : "word", "position" : 2 }, { "token" : "beautiful", "start_offset" : 9, "end_offset" : 18, "type" : "word", "position" : 3 }, { "token" : "weather", "start_offset" : 19, "end_offset" : 26, "type" : "word", "position" : 4 }, { "token" : "days", "start_offset" : 29, "end_offset" : 33, "type" : "word", "position" : 5 }, { "token" : "ago", "start_offset" : 34, "end_offset" : 37, "type" : "word", "position" : 6 } ] }
下表列出了分詞器及其說明:
序號 | 分詞器及說明 |
---|---|
1 |
標準分詞器 (standard) 它基於語法分詞器構建,並且可以為該分詞器配置 max_token_length。 |
2 |
邊緣 N 元語法分詞器 (edgeNGram) 可以為該分詞器設定 min_gram、max_gram、token_chars 等設定。 |
3 |
關鍵字分詞器 (keyword) 它將整個輸入作為輸出生成,並且可以為此設定 buffer_size。 |
4 |
字母分詞器 (letter) 它捕獲整個單詞,直到遇到非字母字元。 |