# Problems with match\_phrase

**URL:** https://discuss.elastic.co/t/problems-with-match-phrase/306934
**Category:** Elasticsearch
**Created:** [June 11, 2022, 9:54am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934 "2022-06-11T09:54:15Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 11, 2022, 9:54am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/1 "2022-06-11T09:54:15Z")

</div>

Hi  
I have a query that looks like this:

```auto
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "fullAddress": "To-Bjerg 2"
          }
        }
      ]
    }
  },
  "from": 0,
  "size": 25
}

```

I want it to match both To-Bjerg 2 and To-Bjerg 22, but I seem to be unable to do that.  
What am I doing wrong ?

My index mapping looks like this:

```auto
{
    "addresses": {
        "mappings": {
            "properties": {
                "addressId": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "city": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "country": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "door": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "etrs89CoordinatEast": {
                    "type": "double"
                },
                "etrs89CoordinatNorth": {
                    "type": "double"
                },
                "floor": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "fullAddress": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "houseNumber": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "postalCode": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "postalCodeAndCity": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "status": {
                    "type": "integer"
                },
                "street": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "streetAddress": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "streetAndPostal": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "updated": {
                    "type": "date"
                }
            }
        }
    }
}

```

What am I doing wrong ?

---

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 11, 2022, 9:56am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/2 "2022-06-11T09:56:43Z")

</div>

I only get an excact match back.

---

<div class="post-metadata">

### Author: ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)
#### Post date: [June 11, 2022, 11:46am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/3 "2022-06-11T11:46:29Z")

</div>

`match_phrase` query matches the text contains the same array of analyzed tokens but not characters. The behavior depends on the analyzer you use.

If "To-Bjerg 2" is analyzed to ["to", "bjerg", "2"] and  
"To-Bjerg 22" is analyzed to ["to", "bjerg", "22"],  
the query phrase "To-Bjerg 2", which is analyzed to ["to", "bjerg", "2"], may only match the former.

You can check the behavior of the analyzer via [Analyze API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-analyze.html).

---

<div class="post-metadata">

### Author: ![Mikhail\_Khludnev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mikhail_khludnev/32/59591_2.png) [@Mikhail\_Khludnev](https://discuss.elastic.co/u/Mikhail_Khludnev)
#### Post date: [June 11, 2022, 6:44pm UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/4 "2022-06-11T18:44:53Z")

</div>

Hmm... it sounds like [Match phrase prefix query | Elasticsearch Guide [8.2] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html) ha?

---

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 12, 2022, 7:00am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/5 "2022-06-12T07:00:00Z")

</div>

Almost it returns To-Bjerg 2 and To-Bjerg 20 but not To-Bjerg 21.  
Any idea why ?

---

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 12, 2022, 7:01am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/6 "2022-06-12T07:01:44Z")

</div>

I am very unsure what analyzer to use.  
It is a bit of a jungle to me.  
Any pointers ?

---

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 12, 2022, 8:08am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/7 "2022-06-12T08:08:21Z")

</div>

Thanks for the help.  
I managed to use your pointers to create a custom ngram analyzer and use that with a match query.

```auto
"analysis": {

                    "analyzer": {

                        "my_analyzer": {

                            "type": "custom",

                            "tokenizer": "my_tokenizer"

                        }

                    },

                    "tokenizer": {

                        "my_tokenizer": {

                            "type": "ngram",

                            "min_gram": "2",

                            "max_gram": "50"

                        }

                    }

                }

{

    "query": {

        "match": {

            "fullAddress": {

                "query": "To-Bjerg 2"

            }

        }

    }

}

```

---

<div class="post-metadata">

### Author: ![Nicolai\_Jee](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nicolai_jee/32/98662_2.png) [@Nicolai\_Jee](https://discuss.elastic.co/u/Nicolai_Jee)
#### Post date: [June 13, 2022, 4:12am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/8 "2022-06-13T04:12:43Z")

</div>

I ended up with these setting for my index:

```auto
"analysis": {
                    "analyzer": {
                        "my_edge_ngram_analyzer": {
                            "filter": [
                                "lowercase"
                            ],
                            "type": "custom",
                            "tokenizer": "edge_ngram_tokenizer"
                        },
                        "my_ngram_analyzer": {
                            "type": "custom",
                            "tokenizer": "ngram_tokenizer"
                        }
                    },
                    "tokenizer": {
                        "edge_ngram_tokenizer": {
                            "token_chars": [
                                "letter",
                                "digit"
                            ],
                            "custom_token_chars": "-",
                            "min_gram": "1",
                            "type": "edge_ngram",
                            "max_gram": "50"
                        },
                        "ngram_tokenizer": {
                            "type": "ngram",
                            "min_gram": "1",
                            "max_gram": "50"
                        }
                    }
                }

```

And I used my\_edge\_ngram\_analyzer on the address field.

---

<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 11, 2022, 4:13am UTC](https://discuss.elastic.co/t/problems-with-match-phrase/306934/9 "2022-07-11T04:13:10Z")

</div>

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