Search code examples
elasticsearchtokenizeelasticsearch-analyzers

How to ignore double slashes while making a search query in elastic search?


For example, in the elastic search document, the string is stored as \\(\\log_4(3x^2+11x)=1\\) The search query what I want to make it work is (log_4(3x^2+11x)=1).

What is the best way to make this work?


Solution

  • You can use the pattern_replace char filter which can replace the \\ with empty string, below is the working example.

    Index Settings with custom analyzer using pattern_replace char filter

    {
        "settings": {
            "analysis": {
                "analyzer": {
                    "my_analyzer": {
                        "filter": [
                            "lowercase"
                        ],
                        "char_filter": [
                            "my_char_filter"
                        ],
                        "type": "custom",
                        "tokenizer": "whitespace"
                    }
                },
                "char_filter": {
                    "my_char_filter": {
                        "type": "pattern_replace",
                        "pattern": "\\\\(.*?)",
                        "replacement": ""
                    }
                }
            }
        }
    }
    

    Analyze API

    {
        "analyzer": "my_analyzer",
        "text": "\\(\\log_4(3x^2+11x)=1\\)"
    }
    

    Generated tokens

    {
        "tokens": [
            {
                "token": "(log_4(3x^2+11x)=1)",
                "start_offset": 1,
                "end_offset": 22,
                "type": "word",
                "position": 0
            }
        ]
    }