Pattern replace character filter

The pattern_replace character filter uses a regular expression to match characters which should be replaced with the specified replacement string. The replacement string can refer to capture groups in the regular expression.

Beware of Pathological Regular Expressions

The pattern replace character filter uses Java Regular Expressions.

A badly written regular expression could run very slowly or even throw a StackOverflowError and cause the node it is running on to exit suddenly.

Read more about pathological regular expressions and how to avoid them.

Configuration

The pattern_replace character filter accepts the following parameters:

pattern

A Java regular expression. Required.

replacement

The replacement string, which can reference capture groups using the $1..$9 syntax, as explained here.

flags

Java regular expression flags. Flags should be pipe-separated, eg “CASE_INSENSITIVE|COMMENTS”.

Example configuration

In this example, we configure the pattern_replace character filter to replace any embedded dashes in numbers with underscores, i.e 123-456-789123_456_789:

  1. PUT my-index-00001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_analyzer": {
  7. "tokenizer": "standard",
  8. "char_filter": [
  9. "my_char_filter"
  10. ]
  11. }
  12. },
  13. "char_filter": {
  14. "my_char_filter": {
  15. "type": "pattern_replace",
  16. "pattern": "(\\d+)-(?=\\d)",
  17. "replacement": "$1_"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. POST my-index-00001/_analyze
  24. {
  25. "analyzer": "my_analyzer",
  26. "text": "My credit card is 123-456-789"
  27. }

The above example produces the following terms:

  1. [ My, credit, card, is, 123_456_789 ]

Using a replacement string that changes the length of the original text will work for search purposes, but will result in incorrect highlighting, as can be seen in the following example.

This example inserts a space whenever it encounters a lower-case letter followed by an upper-case letter (i.e. fooBarBazfoo Bar Baz), allowing camelCase words to be queried individually:

  1. PUT my-index-00001
  2. {
  3. "settings": {
  4. "analysis": {
  5. "analyzer": {
  6. "my_analyzer": {
  7. "tokenizer": "standard",
  8. "char_filter": [
  9. "my_char_filter"
  10. ],
  11. "filter": [
  12. "lowercase"
  13. ]
  14. }
  15. },
  16. "char_filter": {
  17. "my_char_filter": {
  18. "type": "pattern_replace",
  19. "pattern": "(?<=\\p{Lower})(?=\\p{Upper})",
  20. "replacement": " "
  21. }
  22. }
  23. }
  24. },
  25. "mappings": {
  26. "properties": {
  27. "text": {
  28. "type": "text",
  29. "analyzer": "my_analyzer"
  30. }
  31. }
  32. }
  33. }
  34. POST my-index-00001/_analyze
  35. {
  36. "analyzer": "my_analyzer",
  37. "text": "The fooBarBaz method"
  38. }

The above returns the following terms:

  1. [ the, foo, bar, baz, method ]

Querying for bar will find the document correctly, but highlighting on the result will produce incorrect highlights, because our character filter changed the length of the original text:

  1. PUT my-index-00001/_doc/1?refresh
  2. {
  3. "text": "The fooBarBaz method"
  4. }
  5. GET my-index-00001/_search
  6. {
  7. "query": {
  8. "match": {
  9. "text": "bar"
  10. }
  11. },
  12. "highlight": {
  13. "fields": {
  14. "text": {}
  15. }
  16. }
  17. }

The output from the above is:

  1. {
  2. "timed_out": false,
  3. "took": $body.took,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped" : 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total" : {
  12. "value": 1,
  13. "relation": "eq"
  14. },
  15. "max_score": 0.2876821,
  16. "hits": [
  17. {
  18. "_index": "my-index-00001",
  19. "_type": "_doc",
  20. "_id": "1",
  21. "_score": 0.2876821,
  22. "_source": {
  23. "text": "The fooBarBaz method"
  24. },
  25. "highlight": {
  26. "text": [
  27. "The foo<em>Ba</em>rBaz method"
  28. ]
  29. }
  30. }
  31. ]
  32. }
  33. }

Note the incorrect highlight.