# How to get ampersand as value

**URL:** https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968
**Category:** Elasticsearch
**Created:** [January 18, 2018, 3:52am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968 "2018-01-18T03:52:05Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 3:52am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/1 "2018-01-18T03:52:05Z")

</div>

hi Team,

below is my query for searching  
[http://localhost:9200/news//\_search](http://localhost:9200/news//_search) { "query": { "bool": { "must": [{ "multi\_match": { "query": "T&L" , "fields": ["stkno", "tag" ,"content" ,"htext"] ,"operator": "and"} }] } }, "from": 0, "size": 100, "sort": [{ "CDate": { "order": "desc" } }] }

result can't get 'T&L', it will show result not related.how to get & also as value?

thanks and best regards  
Sharon

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 4:17am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/2 "2018-01-18T04:17:41Z")

</div>

Can you show the mapping you get from running the command below?

```
curl -XGET http://localhost:9200/news
```

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 4:20am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/3 "2018-01-18T04:20:49Z")

</div>

hi,

this is my mapping

> "news": {  
> "aliases": { },  
> "mappings": {  
> "trkd": {  
> "properties": {  
> "CDate": {  
> "type": "date",  
> "format": "strict\_date\_optional\_time||epoch\_millis"  
> },  
> "RDate": {  
> "type": "date",  
> "format": "strict\_date\_optional\_time||epoch\_millis"  
> },  
> "category": {  
> "type": "string"  
> },  
> "content": {  
> "type": "string"  
> },  
> "exDate": {  
> "type": "string"  
> },  
> "exgcode": {  
> "type": "string"  
> },  
> "htext": {  
> "type": "string"  
> },  
> "paymentDate": {  
> "type": "string"  
> },  
> "query": {  
> "properties": {  
> "match\_all": {  
> "type": "object"  
> }  
> }  
> },  
> "stat": {  
> "type": "string"  
> },  
> "stkno": {  
> "type": "string"  
> },  
> "tag": {  
> "type": "string"  
> },  
> "url": {  
> "type": "string"  
> }  
> }  
> }

thanks

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 4:29am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/4 "2018-01-18T04:29:01Z")

</div>

That's what I feared, your fields are all `string`, i.e. they are analyzed and thus the ampersand character is discarded at indexing time.

You need to either change the field type of those fields to being `not_analyzed` strings, i.e.

```
"stkno": {
    "type": "string",
    "index": "not_analyzed"
},

```

or add a `not_analyzed` sub-field.

```
"stkno": {
    "type": "string",
    "fields": {
        "raw": {
            "type": "string",
            "index": "not_analyzed"
        }
    }
},

```

In both cases, you need to reindex your data.  
Let us know

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 4:33am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/5 "2018-01-18T04:33:27Z")

</div>

> [@val](#):
>
> "index": "not\_analyzed"

what is the query for add index?

is it like this

PUT /news/trkd/\_mapping/  
{  
"properties" : {  
"tag" : {  
"type" : "string",  
"index": "not\_analyzed"  
}  
}  
}

need to delete the mapping first just create this mapping?

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 4:36am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/6 "2018-01-18T04:36:37Z")

</div>

Yes, delete your index, modify your mapping accordingly and recreate the index with the new mapping. Then reindex your data and try your query again.

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 7:32am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/7 "2018-01-18T07:32:45Z")

</div>

we can't create new mapping with current index?  
when delete the index, no mapping found and how to create the mapping?  
i have doing at reindex side, when reindex back to news, the mapping will same as before without that not-analysed

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 7:38am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/8 "2018-01-18T07:38:01Z")

</div>

```
//1. delete the index
DELETE news

//2 . Create the new index with the new mapping
PUT news
{
   "mappings": {
      "trkd": {
         "properties": {
            ...your fields go here...
         }
      }
   }
}

//3. Reindex your data
...

// 4. search again
POST /news/_search
{...}
```

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 9:01am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/9 "2018-01-18T09:01:33Z")

</div>

after done, it can be find, and total hits also less than before  
but some index without T&L, also show out. Why?

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 9:11am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/10 "2018-01-18T09:11:09Z")

</div>

Can you show the query you're sending and a document that is returned from your query but shouldn't?

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 9:15am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/11 "2018-01-18T09:15:40Z")

</div>

this is query  
[http://localhost:9200/news//\_search](http://localhost:9200/news//_search) { "query": { "bool": { "must": [{ "multi\_match": { "query": "E&O" , "fields": ["stkno", "tag" ,"content" ,"htext"] ,"operator": "and"} }] } }, "from": 0, "size": 500, "sort": [{ "CDate": { "order": "desc" } }] }  
[result](https://drive.google.com/file/d/1K3iueXKoyH7cSsXX5RfsQyP0ND5RBx2E/view?usp=sharing)

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 9:19am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/12 "2018-01-18T09:19:36Z")

</div>

this is mapping  
[mapping](https://drive.google.com/file/d/1ksoQAtXpTlYdSyXYx4lfBO18STdrGKCw/view?usp=sharing)

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 9:25am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/13 "2018-01-18T09:25:45Z")

</div>

Oh I see, since you've taken the second approach, you need to change your query to use the `.raw` sub-fields instead, that should do the trick.

```
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "E&O",
            "fields": [
              "stkno.raw",
              "tag.raw",
              "content.raw",
              "htext.raw"
            ],
            "operator": "and"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 500,
  "sort": [
    {
      "CDate": {
        "order": "desc"
      }
    }
  ]
}
```

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 9:30am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/14 "2018-01-18T09:30:15Z")

</div>

"stkno": {  
"type": "string",  
"index": "not\_analyzed"  
},

if i using this, mean i now need do stkno.raw right?  
cause stkno.raw i need special handle during java coding.

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 18, 2018, 9:34am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/15 "2018-01-18T09:34:20Z")

</div>

That's correct. Your query simply needs to use the field that is `not_analyzed`. So if you go with option 1, then your query stays the same, if you go with option 2, then you need to use the `.raw` fields

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 18, 2018, 10:11am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/16 "2018-01-18T10:11:43Z")

</div>

i can get the result after i use first option.

if the value without this special character, it is whether can't find?  
{ "query": { "bool": { "must": [{ "multi\_match": { "query": "Malaysia" , "fields": ["stkno", "tag" ,"content" ,"htext"] ,"operator": "and"} }] } }, "from": 0, "size": 500, "sort": [{ "CDate": { "order": "desc" } }] }

Actually Malaysia have this data in my index.  
if i find like abc.N it can search.

---

<div class="post-metadata">

### Author: ![Sharon\_wsf](https://avatars.discourse-cdn.com/v4/letter/s/7993a0/32.png) [@Sharon\_wsf](https://discuss.elastic.co/u/Sharon_wsf)
#### Post date: [January 19, 2018, 5:55am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/17 "2018-01-19T05:55:40Z")

</div>

is it by sentence it cant find if using "index": "not\_analyzed"?

---

<div class="post-metadata">

### Author: ![val](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/val/32/138203_2.png) [@val](https://discuss.elastic.co/u/val)
#### Post date: [January 19, 2018, 6:00am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/18 "2018-01-19T06:00:24Z")

</div>

If you need both exact searches and full text search, then the first strategy I highlighted is the way to go. You can then do the exact matches on the `*.raw` fields and the full text search on the normal fields.

---

<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: [February 16, 2018, 6:00am UTC](https://discuss.elastic.co/t/how-to-get-ampersand-as-value/115968/19 "2018-02-16T06:00:26Z")

</div>

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