# Serch text with special character

**URL:** <https://discuss.elastic.co/t/serch-text-with-special-character/242040>\
**Category:** Elasticsearch\
**Created:** [July 21, 2020, 2:12pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040 "2020-07-21T14:12:13Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Katia](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@Katia](https://discuss.elastic.co/u/Katia)\
**Post date:** [July 21, 2020, 2:12pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/1 "2020-07-21T14:12:13Z")

</div>

I really need help.  
I indexed my data using logstash.  
The data is as follows:

```auto
20200807	00:10:02.934 Mes émis à l'appli Hôte 3 3 -1

```

The issue is that, I need to search "3 3-1", but it seems that the caracter "-" cause problem !

```auto
POST /topia72-gest2019.07.12/_search
{
  "size": 10000,
  "query": {
    "match_phrase": {
      "data": "3 3 -1"
    }
  }
}

```

but I also get the lines that contain 3 3 1 !!!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 21, 2020, 2:43pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/2 "2020-07-21T14:43:56Z")

</div>

Two options here:

1. Use a custom choice of Analyzer on your `text` field that preserves the characters you want to keep or
2. Use a `keyword` field that keeps your string as a single token

---

<div class="post-metadata">

**Author:** ![Katia](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@Katia](https://discuss.elastic.co/u/Katia)\
**Post date:** [July 21, 2020, 2:57pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/3 "2020-07-21T14:57:32Z")

</div>

> [@Mark\_Harwood](#):
>
> zer on your `text` field that preserves the characters you want to

can you explain more please ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 21, 2020, 4:05pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/4 "2020-07-21T16:05:30Z")

</div>

Your choice of [Analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/7.8/analysis-analyzers.html) dictates what characters are kept in the index and what characters are punctuation that can be thrown away. [This blog](https://www.elastic.co/blog/found-text-analysis-part-1) is old but provides some background.

If your strings are very short structured fields like a product code you may decide an Analyzer is not useful and want to use a simpler `keyword` field that keeps all the characters.

---

<div class="post-metadata">

**Author:** ![Katia](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@Katia](https://discuss.elastic.co/u/Katia)\
**Post date:** [July 23, 2020, 8:50am UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/5 "2020-07-23T08:50:14Z")

</div>

Do you think that specifying `"index": "not_analyzed"` could be a solution?  
I've read several articles, but I still can't find a solution!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 23, 2020, 8:59am UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/6 "2020-07-23T08:59:50Z")

</div>

> [@Katia](#):
>
> Do you think that specifying `"index": "not_analyzed"` could be a solution?

A `keyword` field is effectively the preferred way of saying that.

---

<div class="post-metadata">

**Author:** ![Katia](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@Katia](https://discuss.elastic.co/u/Katia)\
**Post date:** [July 23, 2020, 12:46pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/7 "2020-07-23T12:46:03Z")

</div>

I tried with this:

```auto
PUT demo_index
{
  "mappings": {
      "properties": {
        "field": {
          "type": "keyword"
        }
      }
    }
}

PUT demo_index/_doc/1
{
  "field": "Msg 3 1 -1"
}

POST demo_index/_search
{
  "query": {
    "match": {
      "field": "3 1 -1"
    }
  }
}

```

But it's not working!

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 23, 2020, 1:25pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/8 "2020-07-23T13:25:18Z")

</div>

The choice between matching in `text` fields and matching `keyword` is a choice between matching words or full-values.  
If your content doesn't hold what most people would consider to be a word ("quick brown foxes" etc) then the text field is probably not for you. No one can agree where one word begins and one word ends so your index and the values people type into the search engine are not underpinned by a shared understanding of the vocabulary.  
So, we switched tack to the `keyword` field, treating the string as a whole value. You didn't match because your query is missing part of the value ("Msg"). You will need to use a RegExp or wildcard query to declare that you're only providing part of a value.

---

<div class="post-metadata">

**Author:** ![Katia](https://avatars.discourse-cdn.com/v4/letter/k/b5e925/32.png) [@Katia](https://discuss.elastic.co/u/Katia)\
**Post date:** [July 23, 2020, 1:37pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/9 "2020-07-23T13:37:53Z")

</div>

Do you have an example?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [July 23, 2020, 1:38pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/10 "2020-07-23T13:38:56Z")

</div>

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html)

---

<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:** [August 20, 2020, 1:38pm UTC](https://discuss.elastic.co/t/serch-text-with-special-character/242040/11 "2020-08-20T13:38:57Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
