# Match with type phrase\_prefix doesn't work when words mix numbers and letters

**URL:** <https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743>\
**Category:** Elasticsearch\
**Created:** [March 28, 2023, 6:13pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743 "2023-03-28T18:13:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raphael\_Fidelis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raphael_fidelis/32/119076_2.png) [@Raphael\_Fidelis](https://discuss.elastic.co/u/Raphael_Fidelis)\
**Post date:** [March 28, 2023, 6:13pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743/1 "2023-03-28T18:13:41Z")

</div>

I'm trying to query as such:

```auto
{
   "match": {
      "query": "100",
      "fields": "description",
      "type": "phrase_prefix"
   }
}

```

However, if I search for "100" wanting to find, for example, a document with the "description" as "pipe 100mm", it doesn't bring me this document as a result, it only matches "100" as a prefix for other number (i.e. 10056) or as a full word.

I am using the Javascript client. Description is a field mapped as text.

What are the limitations of phrase\_prefix on this case, and what could I change in the query so I can get the results as wanted?

My field mapping is

```auto
{
   "description": {
      "type": "text",
      "fields": {
         "keyword": {
            "type": "keyword"
         }
      }
   },
   "analyzer": "ac_filter"
}

```

The analyzer indicated is built as

```auto
"analysis": {
	"analyzer": {
		"ac_filter": {
			"filter": [
				"lowercase",
				"asciifolding"
			],
			"char_filter": [
				"alphabets_char_filter"
			],
			"type": "custom",
			"tokenizer": "standard"
			}
		},
		"char_filter": {
			"alphabets_char_filter": {
				"pattern": "[^\\p{L}\\p{Nd}]",
				"type": "pattern_replace",
				"replacement": " "
			}
		}
}

```

I haven't tried to use ngrams or search\_as\_you\_type yet as I wanted to understand why the above does not work. If there isn't a workaround, let me know.

Thanks.

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 28, 2023, 8:11pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743/2 "2023-03-28T20:11:01Z")

</div>

Hi @Raphael_Fidelis

I believe there is no problem. Look at the tokens generated for the search "pipe 100mm"

```auto
GET idx_test/_analyze
{
  "analyzer": "ac_filter", 
  "text": ["tube 100mm"]
}

```

There are no matches for the term 100, using the match query, because its token is "100mm".

I believe that if you use the match\_phrase\_prefix or wildcard query it will return the documents that start with 100.

---

<div class="post-metadata">

**Author:** ![Raphael\_Fidelis](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/raphael_fidelis/32/119076_2.png) [@Raphael\_Fidelis](https://discuss.elastic.co/u/Raphael_Fidelis)\
**Post date:** [March 28, 2023, 8:28pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743/3 "2023-03-28T20:28:16Z")

</div>

I tried match\_phrase\_prefix, but with no luck, maybe there's a detail that changes it's behaviour, since I've got a pretty big index and mapping. I was wondering why phrase\_prefix is working fine for queries like "smart" returning documents such as "smartphone" or "1520" returning documents such as "15203289", but "50" isn't recognized as a prefix for "50cm".

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [March 28, 2023, 10:44pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743/4 "2023-03-28T22:44:17Z")

</div>

I informed the settings to simulate searches. In all tests I got a result (tests with smart, 100, 1502 and 50).  
Below are the test codes. You say that the match\_phrase\_prefix did not work, maybe there is some information missing from your question.

```auto
PUT /idx_test
{
  "mappings": {
    "properties": {
      "description": {
        "type": "text",
        "analyzer": "ac_filter"
      }
    }
  },
  "settings": {
    "analysis": {
      "analyzer": {
        "ac_filter": {
          "filter": [
            "lowercase",
            "asciifolding"
          ],
          "char_filter": [
            "alphabets_char_filter"
          ],
          "type": "custom",
          "tokenizer": "standard"
        }
      },
      "char_filter": {
        "alphabets_char_filter": {
          "pattern": """[^\p{L}\p{Nd}]""",
          "type": "pattern_replace",
          "replacement": " "
        }
      }
    }
  }
}

POST idx_test/_bulk
{"index":{}}
{"description": "10056"}
{"index":{}}
{"description": "pipe 100mm"}
{"index":{}}
{"description": "smartphone"}
{"index":{}}
{"description": "15203289"}
{"index":{}}
{"description": "50cm"}

POST idx_test/_search
{
  "query": {
    "match_phrase_prefix": {
      "description": "50"
    }
  }
}

```

Ouput:

```auto
 "hits" : [
      {
        "_index" : "idx_test",
        "_type" : "_doc",
        "_id" : "OYZjKocBev_z0rRh2Tkm",
        "_score" : 0.2876821,
        "_source" : {
          "description" : "50cm"
        }
      }
    ]

```

---

<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:** [April 25, 2023, 10:44pm UTC](https://discuss.elastic.co/t/match-with-type-phrase-prefix-doesnt-work-when-words-mix-numbers-and-letters/328743/5 "2023-04-25T22:44:45Z")

</div>

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