分析器、分词器与过滤器
深入了解文本分析的组成部分:字符过滤器、分词器和词元过滤器,并理解它们各自的作用。
分析器、分词器与过滤器 是 CoddyKit 上的免费 Elasticsearch & Full Text Search Systems 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Elasticsearch & Full Text Search Systems 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Unlocking Search with Text Analysis
When you search, you expect relevant results, even with typos or different word forms. This magic happens through text analysis!
Text analysis is how Elasticsearch processes raw text into a format suitable for searching. It breaks down and transforms your content to make it highly searchable and improve relevancy.
The Analyzer: Your Text Processor
At the heart of text analysis is the analyzer. Think of an analyzer as a complete pipeline, a series of steps that takes raw text and prepares it for indexing and searching.
Every analyzer consists of up to three main components:
- Character Filters: Clean up the raw text.
- Tokenizer: Breaks text into individual 'tokens' (words).
- Token Filters: Refine and modify these tokens.
Character Filters: Cleaning Up Text
Character filters are the first step in the analysis pipeline. They work on the raw text string before it's broken into tokens.
Their job is to clean up or transform the text. Common uses include:
- Removing HTML tags (e.g.,
<b>hello</b>becomeshello). - Replacing characters (e.g.,
&toand). - Mapping specific characters to others.
Char Filter Demo: HTML Strip
Let's use the _analyze API to see an html_strip character filter remove HTML tags. Notice how the text is still a single string at this stage.
POST _analyze
{
"char_filter": ["html_strip"],
"text": "<b>Hello</b> <i>world</i>!"
}The Tokenizer: Breaking into Words
After character filters, the tokenizer takes over. Its primary role is to break the cleaned text into individual 'tokens' or words. These tokens are what eventually get indexed and searched.
Different tokenizers exist for various needs:
- Standard Tokenizer: Default, good for most languages, handles punctuation.
- Whitespace Tokenizer: Splits text only by whitespace.
- Keyword Tokenizer: Treats the entire input as a single, unchangeable token (useful for IDs or specific codes).
Tokenizer Demo: Standard Tokenizer
The standard tokenizer is widely used. It intelligently splits text, removes most punctuation, and lowercases words by default (though lowercasing is technically a token filter often applied with it).
POST _analyze
{
"tokenizer": "standard",
"text": "Quick brown fox!"
}Token Filters: Refining Tokens
The final step is token filters. These filters take the tokens produced by the tokenizer and modify them. They can add, remove, or change tokens, significantly impacting search relevancy.
Examples of token filters:
- Lowercase Filter: Converts all tokens to lowercase.
- Stopword Filter: Removes common, less meaningful words (e.g., 'the', 'is', 'a').
- Synonym Filter: Replaces words with their synonyms.
- Stemmer Filter: Reduces words to their root form (e.g., 'running' to 'run').
Token Filter Demo: Lowercase & Stop
Let's see how lowercase and stop filters work. We'll use the standard tokenizer first, then apply these filters.
POST _analyze
{
"tokenizer": "standard",
"filter": ["lowercase", "stop"],
"text": "The Quick brown fox is fast."
}The Full Analysis Pipeline
Here's how all the components work together in sequence:
- Raw Text enters the analyzer.
- It passes through Character Filters (cleaning).
- The output goes to the Tokenizer (breaking into tokens).
- Finally, the tokens are processed by Token Filters (refinement).
- The result is a set of Searchable Terms.
This pipeline ensures consistent and effective text processing for your search data.
Check Your Understanding
Consider the text: <p>Learn Elasticsearch</p>
If you want to remove the HTML tags <p> and <b> before the text is split into words, which component of the analyzer pipeline would you use?
Recap: Analysis Components
Great job! You've successfully explored the building blocks of Elasticsearch's text analysis:
- Analyzers: The complete text processing pipeline.
- Character Filters: Pre-process raw text (e.g., remove HTML, replace characters).
- Tokenizers: Break text into individual tokens (words).
- Token Filters: Refine and modify tokens (e.g., lowercase, remove stop words, stem).
Understanding these components is crucial for building powerful and relevant search experiences!
常见问题解答
「分析器、分词器与过滤器」课时是免费的吗?
是的 — 「分析器、分词器与过滤器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Elasticsearch & Full Text Search Systems 课程的其余内容,请升级到 CoddyKit PRO。 Elasticsearch & Full Text Search Systems 课程共包含 4 节课。
「分析器、分词器与过滤器」这节课中我会学到什么?
深入了解文本分析的组成部分:字符过滤器、分词器和词元过滤器,并理解它们各自的作用。 你通过在浏览器中直接运行的动手代码来练习 Elasticsearch & Full Text Search Systems,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Elasticsearch & Full Text Search Systems 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Elasticsearch & Full Text Search Systems 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「分析器、分词器与过滤器」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Elasticsearch & Full Text Search Systems 课中编写并运行代码吗?
能。每节 Elasticsearch & Full Text Search Systems 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。