Persian analyzer

The built-in persian analyzer can be applied to a text field using the following command:

  1. PUT /persian-index
  2. {
  3. "mappings": {
  4. "properties": {
  5. "content": {
  6. "type": "text",
  7. "analyzer": "persian"
  8. }
  9. }
  10. }
  11. }

copy

Stem exclusion

You can use stem_exclusion with this language analyzer using the following command:

  1. PUT index_with_stem_exclusion_persian_analyzer
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "stem_exclusion_persian_analyzer": {
  7. "type": "persian",
  8. "stem_exclusion": ["حکومت", "تأیید"]
  9. }
  10. }
  11. }
  12. }
  13. }

copy

Persian analyzer internals

The persian analyzer is built using the following components:

  • Tokenizer: standard

  • Char filter: mapping

  • Token filters:

    • lowercase
    • decimal_digit
    • normalization (Arabic)
    • normalization (Persian)
    • keyword
    • stemmer (Norwegian)

Custom Persian analyzer

You can create a custom Persian analyzer using the following command:

  1. PUT /persian-index
  2. {
  3. "settings": {
  4. "analysis": {
  5. "filter": {
  6. "persian_stop": {
  7. "type": "stop",
  8. "stopwords": "_persian_"
  9. },
  10. "persian_keywords": {
  11. "type": "keyword_marker",
  12. "keywords": []
  13. }
  14. },
  15. "char_filter": {
  16. "null_width_replace_with_space": {
  17. "type": "mapping",
  18. "mappings": [ "\\u200C=>\\u0020"]
  19. }
  20. },
  21. "analyzer": {
  22. "persian_analyzer": {
  23. "type": "custom",
  24. "tokenizer": "standard",
  25. "char_filter": [ "null_width_replace_with_space" ],
  26. "filter": [
  27. "lowercase",
  28. "decimal_digit",
  29. "arabic_normalization",
  30. "persian_normalization",
  31. "persian_stop"
  32. ]
  33. }
  34. }
  35. }
  36. },
  37. "mappings": {
  38. "properties": {
  39. "content": {
  40. "type": "text",
  41. "analyzer": "persian_analyzer"
  42. }
  43. }
  44. }
  45. }

copy

Generated tokens

Use the following request to examine the tokens generated using the analyzer:

  1. POST /persian-index/_analyze
  2. {
  3. "field": "content",
  4. "text": "دانشجویان در دانشگاه‌های ایرانی تحصیل می‌کنند. شماره‌های آن‌ها ۱۲۳۴۵۶ است."
  5. }

copy

The response contains the generated tokens:

  1. {
  2. "tokens": [
  3. {"token": "دانشجويان","start_offset": 0,"end_offset": 9,"type": "<ALPHANUM>","position": 0},
  4. {"token": "دانشگاه","start_offset": 13,"end_offset": 20,"type": "<ALPHANUM>","position": 2},
  5. {"token": "ايراني","start_offset": 25,"end_offset": 31,"type": "<ALPHANUM>","position": 4},
  6. {"token": "تحصيل","start_offset": 32,"end_offset": 37,"type": "<ALPHANUM>","position": 5},
  7. {"token": "شماره","start_offset": 47,"end_offset": 52,"type": "<ALPHANUM>","position": 8},
  8. {"token": "123456","start_offset": 63,"end_offset": 69,"type": "<NUM>","position": 12}
  9. ]
  10. }