0Pricing
Elasticsearch & Full Text Search Systems · درس

تخصيص محللات النصوص

أنشئ محللات مخصصة وطبّقها على حقول محددة للتحكم في كيفية معالجة النصوص واختزالها وفهرستها للبحث

تخصيص محللات النصوص درس مجاني في Elasticsearch & Full Text Search Systems على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Elasticsearch & Full Text Search Systems، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Elasticsearch & Full Text Search Systems 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Custom Analyzers?

Elasticsearch comes with powerful default text analyzers, but sometimes your data needs a special touch. This is where custom analyzers shine!

They allow you to precisely control how your text fields are processed for search, ensuring optimal relevancy and accuracy for your specific use case.

The Analyzer Recipe

Recall that every analyzer, custom or built-in, follows a three-step process to transform raw text into searchable tokens:

  • Character Filters: Clean up the raw input string (e.g., remove HTML tags).
  • Tokenizer: Breaks the processed string into individual words or tokens.
  • Token Filters: Modifies, adds, or removes tokens (e.g., lowercase, remove stop words, apply stemming).

A custom analyzer lets you pick and choose these ingredients!

Defining Custom Analyzers

You define custom analyzers within an index's settings block, under analysis. This tells Elasticsearch how to process text for that index.

Here's the basic structure for creating a custom analyzer:

PUT /my_custom_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_custom_analyzer": {
          "type": "custom",
          "char_filter": [],
          "tokenizer": "standard",
          "filter": []
        }
      }
    }
  }
}

Custom Character Filters

Character filters are the first step, acting on the raw text. They can remove or replace characters before tokenization. You can define your own or use built-in ones.

  • html_strip: Removes HTML tags.
  • mapping: Replaces specified characters or strings.

Here's how to define a custom mapping filter:

PUT /my_index_with_char_filter
{
  "settings": {
    "analysis": {
      "char_filter": {
        "ampersand_to_and": {
          "type": "mapping",
          "mappings": ["& => and "]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "char_filter": ["ampersand_to_and"],
          "tokenizer": "standard",
          "filter": ["lowercase"]
        }
      }
    }
  }
}

Selecting a Tokenizer

The tokenizer breaks the stream of characters from the character filters into individual tokens (words). Your choice here is crucial for how words are identified.

Common built-in tokenizers you can use in custom analyzers include:

  • standard: Good for most languages, grammar-based.
  • whitespace: Splits text only on whitespace.
  • keyword: Treats the entire input as a single token (useful for exact values).
  • pattern: Splits text based on a regular expression.

Custom Token Filters

Token filters refine the tokens generated by the tokenizer. This is where most of the search logic resides, like handling synonyms or stemming.

You can define custom versions of filters or use built-in ones:

  • lowercase: Converts tokens to lowercase.
  • stop: Removes common, less meaningful words (stop words).
  • synonym: Replaces tokens with their synonyms.
  • stemmer: Reduces words to their root form.

Let's define a custom stop word filter:

PUT /my_index_with_token_filter
{
  "settings": {
    "analysis": {
      "filter": {
        "my_custom_stop_words": {
          "type": "stop",
          "stopwords": ["a", "the", "is", "and", "are"]
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "my_custom_stop_words"]
        }
      }
    }
  }
}

Building a Full Custom Analyzer

Now, let's combine these concepts to create a practical custom analyzer for blog post content. It will:

  • Remove HTML tags.
  • Tokenize standard text.
  • Lowercase all tokens.
  • Remove common English stop words.

This analyzer is then applied to the content field.

PUT /blog_posts_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "html_strip_char_filter": {
          "type": "html_strip"
        }
      },
      "filter": {
        "english_stop_words": {
          "type": "stop",
          "stopwords": ["the", "a", "an", "is", "are"]
        }
      },
      "analyzer": {
        "blog_content_analyzer": {
          "type": "custom",
          "char_filter": ["html_strip_char_filter"],
          "tokenizer": "standard",
          "filter": ["lowercase", "english_stop_words"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "content": {
        "type": "text",
        "analyzer": "blog_content_analyzer"
      }
    }
  }
}

Applying to Field Mappings

Once your custom analyzer is defined in the index settings, you apply it to a text field within your index's mapping. This tells Elasticsearch to use your custom logic when indexing and searching that specific field.

You simply specify the analyzer parameter with the name of your custom analyzer:

PUT /blog_posts_index/_mapping
{
  "properties": {
    "content": {
      "type": "text",
      "analyzer": "blog_content_analyzer"
    },
    "title": {
      "type": "text",
      "analyzer": "standard" 
    }
  }
}

Testing with _analyze API

How can you be sure your custom analyzer works as expected? Use the _analyze API! It lets you simulate how text will be processed by any analyzer.

This is an indispensable tool for debugging and validating your text analysis setup.

GET /blog_posts_index/_analyze
{
  "analyzer": "blog_content_analyzer",
  "text": "The <b>quick</b> brown fox jumps over the lazy dog."
}

Quiz Time!

You've learned about the components of a custom analyzer and how to define them. Let's test your knowledge!

Custom Analyzers: Your Search Superpower

Congratulations! You've learned how to harness the power of custom analyzers in Elasticsearch.

  • You can now define custom character filters, tokenizers, and token filters.
  • You know how to combine these components to create a tailor-made analyzer for your data.
  • You understand how to apply this analyzer to specific fields in your mappings.
  • And importantly, you know how to test your analyzer using the _analyze API.

This skill is crucial for building highly relevant and accurate search experiences!

الأسئلة الشائعة

هل درس «تخصيص محللات النصوص» مجاني؟

نعم — نص درس «تخصيص محللات النصوص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Elasticsearch & Full Text Search Systems، انتقل إلى CoddyKit PRO. تتضمن دورة Elasticsearch & Full Text Search Systems 4 دروس في المجموع.

ماذا ستتعلم في «تخصيص محللات النصوص»؟

أنشئ محللات مخصصة وطبّقها على حقول محددة للتحكم في كيفية معالجة النصوص واختزالها وفهرستها للبحث تتمرن على Elasticsearch & Full Text Search Systems مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Elasticsearch & Full Text Search Systems؟

لا تُشترط خبرة سابقة. Elasticsearch & Full Text Search Systems على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «تخصيص محللات النصوص»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Elasticsearch & Full Text Search Systems هذا؟

نعم. كل درس في Elasticsearch & Full Text Search Systems يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. المحللات والمجزئات والمرشحات
  2. تخصيص محللات النصوص
  3. التعزيز وحساب الصلة
  4. المرادفات والاشتقاق الصرفي
← العودة إلى Elasticsearch & Full Text Search Systems