# Unable to preserve special characters in search results of ElasticSearch

**URL:** <https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820>\
**Category:** Elasticsearch\
**Created:** [March 23, 2015, 12:14pm UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820 "2015-03-23T12:14:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Anusha](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Anusha](https://discuss.elastic.co/u/Anusha)\
**Post date:** [March 23, 2015, 12:14pm UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/1 "2015-03-23T12:14:57Z")

</div>

Hi,

I want to preserve the special characters like -, /, (, ) in search  
results.

Ex: Abc/def a(bc)def a-bcd  
response: If I enter Abc/ the records containing abc/ need to come,  
similarly for abc/def records with the following need to come, in the same  
way abc/def a(bc) records with the similar combination need to come.

For this I have used different analyzers and filters and the settings and  
mappings as follows:

PUT /facebook?pretty=true  
{  
"settings" : {  
"analysis" : {  
"filter" : {  
"special\_character\_spliter" : {  
"type" : "word\_delimiter",  
"preserve\_original": "true"  
}  
},  
"analyzer" : {  
"my\_an" : {  
"type" : "pattern",  
"pattern" : "[^(_/_-\*\w\p{L}]+",  
"tokenizer" : "whitespace",  
"filter" : ["special\_character\_spliter"]  
}  
}  
}  
},  
"mappings" : {  
"face" : {  
"properties" : {  
"msg" : {  
"type" : "string",  
"analyzer": "my\_an"

```
            }, 
            "name" : { 
                "type" : "string" 
            } 
        } 
    } 
} 

```

}

and even I have tried with the following settings also:

PUT facebook?pretty=true  
{  
"settings": {  
"index.number\_of\_replicas": 0,  
"analysis": {  
"analyzer": {  
"msg\_excp\_analyzer": {  
"type": "custom",  
"tokenizer": "whitespace",  
"filters": ["my\_word\_delimiter","my\_word\_pattern",  
"lowercase",  
"asciifolding",  
"shingle",  
"standard"]  
}  
},  
"filters": {  
"my\_word\_pattern":  
{  
"type" : "pattern\_capture",  
"preserve\_original" : 1,  
"patterns" : [  
"(\w+)",  
"(\p{L}+)",  
"(\d+)",  
"@(.+)"  
]

```
            }, 
            "my_word_delimiter": { 
                "type": "word_delimiter", 
                "preserve_original": "true", 
                  "catenate_all": "true", 
                  "type_table": { 
                    "$": "DIGIT", 
                    "%": "DIGIT", 
                    ".": "DIGIT", 
                    ",": "DIGIT", 
                    ":": "DIGIT", 
                    "/": "DIGIT", 
                    "\\": "DIGIT", 
                    "=": "DIGIT", 
                    "&": "DIGIT", 
                    "(": "DIGIT", 
                    ")": "DIGIT", 
                    "<": "DIGIT", 
                    ">": "DIGIT", 
                    "\\U+000A": "DIGIT" 

            }, 
            "my_asciifolding": { 
                "type": "asciifolding", 
                "preserve_original": true 
            } 
        } 
    } 
} 
}, 
"mappings": { 
    "face": { 
        "properties": { 
            "msg": { 
                "type": "string", 
                "index": "analyzed", 
                "analyzer": "msg_excp_analyzer" 
            }, 
            "name": { 
                "type": "string", 
                "index": "analyzed", 
                "analyzer": "msg_excp_analyzer" 
            } 
        } 
    } 
} 

```

}

With above settings I have tried separately inorder to preserve the special  
characters in search results,  
and i have used query like:

GET facebook/face/\_search  
{

"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"fields": [  
"msg"  
],  
"query": "a/"  
}  
}  
]  
}

```
} 

```

}

Am getting the following error:

"error": "SearchPhaseExecutionException[Failed to execute phase [query],  
all shards failed; shardFailures {[trVZmtb4Qu-QWIqxK5hEfg][facebook][0]:  
SearchParseException[[facebook][0]:

May I know what is the problem with , whether is it with my query or  
settings?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a33a7559-3a09-4c24-a8a7-101d6e9fbda0%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a33a7559-3a09-4c24-a8a7-101d6e9fbda0%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![andavar](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@andavar](https://discuss.elastic.co/u/andavar)\
**Post date:** [March 23, 2015, 2:20pm UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/2 "2015-03-23T14:20:00Z")

</div>

Hi  
I think You need to escape those spl char in search string, like  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"fields": [  
"msg"  
],  
"query": "a\/"  
}  
}  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Anusha](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Anusha](https://discuss.elastic.co/u/Anusha)\
**Post date:** [March 24, 2015, 10:20am UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/3 "2015-03-24T10:20:52Z")

</div>

If I escape that character '/' I will get the data irrelevant to the search  
term

eg: if a/b, a/c, a/d, a(b), a(c), a-b, ab is my data

My requirement is if I enter the term a( then I have to get only a( as a  
result but not ab,a-b,...

I would like to get the results without escaping them, the result need to  
preserve the special character '/' , any query which is relevant to that  
will be helpful and the search term need to search in different fields but  
no need to match in all fields , any one field match result is required.

On Monday, March 23, 2015 at 7:50:08 PM UTC+5:30, Periyandavar wrote:

> Hi  
> I think You need to escape those spl char in search string, like  
> {  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "query\_string": {  
> "fields": [  
> "msg"  
> ],  
> "query": "a\/"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> --  
> View this message in context:  
> [http://elasticsearch-users.115913.n3.nabble.com/Unable-to-preserve-special-characters-in-search-results-of-ElasticSearch-tp4072409p4072418.html](http://elasticsearch-users.115913.n3.nabble.com/Unable-to-preserve-special-characters-in-search-results-of-ElasticSearch-tp4072409p4072418.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0c8b7093-5dae-4ea5-9b60-bf5353f6d985%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0c8b7093-5dae-4ea5-9b60-bf5353f6d985%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Anusha](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Anusha](https://discuss.elastic.co/u/Anusha)\
**Post date:** [March 26, 2015, 12:10pm UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/4 "2015-03-26T12:10:17Z")

</div>

Hi Thanks,

I resolved this issue,

In order to preserve the special characters and to search the query term in multiple fields for exact match it is better to change the settings as shown below.

Settings I updated:

PUT /my\_index/\_settings?pretty=true  
{  
"settings" : {  
"analysis": {  
"analyzer": {  
"wordAnalyzer": {  
"type": "custom",  
"tokenizer": "whitespace",  
"filter": [  
"word\_delimiter\_for\_phone","nGram\_filter"  
]  
}  
},  
"filter": {  
"word\_delimiter\_for\_phone": {  
"type": "word\_delimiter",  
"catenate\_all": true,  
"generate\_number\_parts ": false,  
"split\_on\_case\_change": false,  
"generate\_word\_parts": false,  
"split\_on\_numerics": false,  
"preserve\_original": true  
},

```
        "nGram_filter": {
           "type": "nGram",
           "min_gram": 1,
           "max_gram": 20,
           "token_chars": [
              "letter"
           ]
        }
  }
}

```

}  
}

Mapping settings:  
{  
“mappings”:{

"face" : {  
"properties" : {  
"{field-1}id" : {  
"type" : "string",  
"index\_name" : "change",  
"analyzer": "wordAnalyzer"

```
        },
        "{field-2}name" : {
            "type" : "string",
           "index_name" : "change",
           "analyzer": "wordAnalyzer"
        },
        "{field-3}Year":
        {
            "type" : "string",
           "index_name" : "change",
           "analyzer": "wordAnalyzer"
        },
        "{field-4}Make":
        {
            "type" : "string",
           "index_name" : "change",
           "analyzer": "wordAnalyzer"
        }
     }
  } 

```

and the query we can use:

GET my\_index/face/\_search  
{  
"query": {  
"match": {  
"change":  
{  
"query": "A/T o",  
"type": "phrase\_prefix"  
}  
}  
}  
}

By this we can search for that term in all the fields. In order to search in only single field we can give that field name in the place of "change" in match query.

And for to change in mappings I am able to update the analyzers, but not the index\_name, for to add index\_name I have deleted the index and again done the mapping as above.

---

<div class="post-metadata">

**Author:** ![Anusha](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@Anusha](https://discuss.elastic.co/u/Anusha)\
**Post date:** [March 26, 2015, 12:18pm UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/5 "2015-03-26T12:18:31Z")

</div>

Hi Thanks,

I resolved this issue,

In order to preserve the special characters and to search the query term in  
multiple fields for exact match it is better to change the settings as  
shown below.

Settings I updated:

PUT /my\_index/\_settings?pretty=true  
{  
"settings" : {  
"analysis": {  
"analyzer": {  
"wordAnalyzer": {  
"type": "custom",  
"tokenizer": "whitespace",  
"filter": [  
"word\_delimiter\_for\_phone","nGram\_filter"  
]  
}  
},  
"filter": {  
"word\_delimiter\_for\_phone": {  
"type": "word\_delimiter",  
"catenate\_all": true,  
"generate\_number\_parts ": false,  
"split\_on\_case\_change": false,  
"generate\_word\_parts": false,  
"split\_on\_numerics": false,  
"preserve\_original": true  
},

```
        "nGram_filter": { 
           "type": "nGram", 
           "min_gram": 1, 
           "max_gram": 20, 
           "token_chars": [ 
              "letter" 
           ] 
        } 
  } 
} 

```

}  
}

Mapping settings:  
{  
“mappings”:{

"face" : {  
"properties" : {  
"{field-1}id" : {  
"type" : "string",  
"index\_name" : "change",  
"analyzer": "wordAnalyzer"

```
        }, 
        "{field-2}name" : { 
            "type" : "string", 
           "index_name" : "change", 
           "analyzer": "wordAnalyzer" 
        }, 
        "{field-3}Year": 
        { 
            "type" : "string", 
           "index_name" : "change", 
           "analyzer": "wordAnalyzer" 
        }, 
        "{field-4}Make": 
        { 
            "type" : "string", 
           "index_name" : "change", 
           "analyzer": "wordAnalyzer" 
        } 
     } 
  } 

```

and the query we can use:

GET my\_index/face/\_search  
{  
"query": {  
"match": {  
"change":  
{  
"query": "A/T o",  
"type": "phrase\_prefix"  
}  
}  
}  
}

By this we can search for that term in all the fields. In order to search  
in only single field we can give that field name in the place of "change"  
in match query.

And for to change in mappings I am able to update the analyzers, but not  
the index\_name, for to add index\_name I have deleted the index and again  
done the mapping as above.

On Tuesday, March 24, 2015 at 3:50:52 PM UTC+5:30, Muddadi Hemaanusha wrote:

> If I escape that character '/' I will get the data irrelevant to the  
> search term
> 
> eg: if a/b, a/c, a/d, a(b), a(c), a-b, ab is my data
> 
> My requirement is if I enter the term a( then I have to get only a( as a  
> result but not ab,a-b,...
> 
> I would like to get the results without escaping them, the result need to  
> preserve the special character '/' , any query which is relevant to that  
> will be helpful and the search term need to search in different fields but  
> no need to match in all fields , any one field match result is required.
> 
> On Monday, March 23, 2015 at 7:50:08 PM UTC+5:30, Periyandavar wrote:
> 
> > Hi  
> > I think You need to escape those spl char in search string, like  
> > {  
> > "query": {  
> > "bool": {  
> > "must": [  
> > {  
> > "query\_string": {  
> > "fields": [  
> > "msg"  
> > ],  
> > "query": "a\/"  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > --  
> > View this message in context:  
> > [http://elasticsearch-users.115913.n3.nabble.com/Unable-to-preserve-special-characters-in-search-results-of-ElasticSearch-tp4072409p4072418.html](http://elasticsearch-users.115913.n3.nabble.com/Unable-to-preserve-special-characters-in-search-results-of-ElasticSearch-tp4072409p4072418.html)  
> > Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/db074925-217f-48a9-8d19-6960282e4a7e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/db074925-217f-48a9-8d19-6960282e4a7e%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:23am UTC](https://discuss.elastic.co/t/unable-to-preserve-special-characters-in-search-results-of-elasticsearch/22820/6 "2017-07-06T00:23:45Z")

</div>


