Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i add char_filter for a single field

I have this index which contains filds "firstName", "lastName", "patronymic", "birthDate" and i need to add char_filter for field "birthDate".
For example, 1939-02-21 have to be 1939.02.21.
All fields are text.

Is it possible to create char_filter that will affect on "birthDate" and not other fields?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In Elasticsearch, you can achieve this by using a Char Filter. However, please note that Char Filters in Elasticsearch work at the time of indexing, not at the time of querying. This means that the changes made by Char Filters are applied to the stored data, and not to the query terms.

Here’s an example of how you might create an index with a custom Char Filter for the "birthDate" field:

PUT /your_index
{
  "settings": {
    "analysis": {
      "char_filter": {
        "birthDate_filter": {
          "type": "pattern_replace",
          "pattern": "(\\d{4})-(\\d{2})-(\\d{2})",
          "replacement": "$1.$2.$3"
        }
      },
      "analyzer": {
        "custom_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "char_filter": ["birthDate_filter"],
          "filter": ["lowercase"]
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "firstName": {
        "type": "text",
        "analyzer": "custom_analyzer"
      },
      "lastName": {
        "type": "text",
        "analyzer": "custom_analyzer"
      },
      "patronymic": {
        "type": "text",
        "analyzer": "custom_analyzer"
      },
      "birthDate": {
        "type": "text",
        "analyzer": "custom_analyzer"
      }
    }
  }
}

Explanation:

  • The char_filter named "birthDate_filter" uses the pattern_replace type to replace the hyphens in the "birthDate" field with dots.
  • The analyzer named "custom_analyzer" is configured to use the "birthDate_filter" Char Filter for the "birthDate" field, and it also uses the standard tokenizer and lowercase filter.

This way, when you index a document, the "birthDate" field will be transformed according to the Char Filter pattern, and the analyzer will be applied to all text fields.

Remember to adjust the index name, field names, and any other settings based on your actual requirements.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading