# Custom analyzer returning wrong document

**URL:** <https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074>\
**Category:** Elasticsearch\
**Created:** [August 26, 2015, 8:12am UTC](https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074 "2015-08-26T08:12:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjohnst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjohnst/32/4102_2.png) [@mjohnst](https://discuss.elastic.co/u/mjohnst)\
**Post date:** [August 26, 2015, 8:12am UTC](https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074/1 "2015-08-26T08:12:09Z")

</div>

Hello,

I am writing some custom analyzers to handle specific special characters that I care about, and still use the standard tokenizer (can't use whitespace or pattern for my documents).

This is the scenario I'm trying to get working:

A document contains the text `AT&amp;T` which gets character mapped to `ATAMPERSAND_SYMBOLampSEMICOLON_SYMBOLT` via the `index_analyzer` (_should_ be single token with standard tokenizer).

Then, by searching `AT&T` (_not_ `AT&amp;T`) the same document will be returned by converting `AT&T` to `ATAMPERSAND_SYMBOLampSEMICOLON_SYMBOLT` with the `search_analyzer`.

Here's my Sense code trying this out, but the wrong document is being returned... Any help is much appreciated!

The custom analyzers and mapping:

```
PUT /index
{
    "settings": {
        "analysis": {
            "analyzer": {
                "TextIndexAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "char_filter": ["SpecialCharactersIndexFilter"]
                },
                "TextSearchAnalyzer": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "char_filter": ["SpecialCharactersSearchFilter"]
                }
            },
            "char_filter": {
                "SpecialCharactersIndexFilter": {
                    "type": "mapping",
                    "mappings": [
                        "&=>AMPERSAND_SYMBOL",
                        ";=>SEMICOLON_SYMBOL"
                    ]
                },
                "SpecialCharactersSearchFilter": {
                    "type": "mapping",
                    "mappings": [
                        "&=>AMPERSAND_SYMBOLampSEMICOLON_SYMBOL"
                    ]
                }
            }
        }
   },
   "mappings": {
       "Doc": {
           "properties": {
               "Text": {
                    "type": "string",
                    "index_analyzer": "TextIndexAnalyzer",
                    "search_analyzer": "TextSearchAnalyzer"
                }
           }
       }
   }
}

```

Two example documents:

```
PUT /index/Doc/1
{
    "Doc.Text": "AT&amp;T"
}

PUT /index/Doc/2
{
    "Doc.Text": "AT&T"
}

```

Want to get document `_id: 1` with this search, but `_id: 2` is returned:

```
GET /index/Doc/_search
{
    "query": {
        "match_phrase": {
           "Doc.Text": "AT&T"
        }
    }
}

```

Showing that the two analyzers return the same tokens:

```
POST /index/_analyze?analyzer=TextIndexAnalyzer
{
  AT&amp;T
}
POST /index/_analyze?analyzer=TextSearchAnalyzer
{
  AT&T
}

```

Thanks again for any help!

---

<div class="post-metadata">

**Author:** ![Sarwar](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@Sarwar](https://discuss.elastic.co/u/Sarwar)\
**Post date:** [August 26, 2015, 12:02pm UTC](https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074/2 "2015-08-26T12:02:26Z")

</div>

You are using match\_phrase but is "AT&T" a phrase? The way you have set it up, it's a term being matched not a phrase and so the closest match is AT&T

---

<div class="post-metadata">

**Author:** ![mjohnst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjohnst/32/4102_2.png) [@mjohnst](https://discuss.elastic.co/u/mjohnst)\
**Post date:** [August 26, 2015, 8:57pm UTC](https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074/3 "2015-08-26T20:57:05Z")

</div>

Yeah, AT&T is still a phrase I think.

I've figured out my problem. I had some incorrectness with the index and type parts of the URI for what I had in the mapping.

I have it working now.

---

<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 5, 2017, 11:53pm UTC](https://discuss.elastic.co/t/custom-analyzer-returning-wrong-document/28074/4 "2017-07-05T23:53:39Z")

</div>


