# Regexp not searching as expected

**URL:** https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155
**Category:** Elasticsearch
**Created:** [June 1, 2020, 11:45am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155 "2020-06-01T11:45:42Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Alenoosh\_Baghoomian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alenoosh_baghoomian/32/69485_2.png) [@Alenoosh\_Baghoomian](https://discuss.elastic.co/u/Alenoosh_Baghoomian)
#### Post date: [June 1, 2020, 11:45am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/1 "2020-06-01T11:45:42Z")

</div>

Hello

I'm using 'search' api of elasticsearch (7.4.1) and the following is the query I pass to this api :

```
`["size" => 10000,
 "query" => [
	"bool" => [
		"must" => [
			[
				"regexp" => [
					"name" => [
						"value" => "carrier.*",
						"flags" => "ALL"
					]
				]
			],
			[
				"term" => [
					"countryId" => 1
				]
			],
			[
				"term" => [
					"active" => 1
				]
			]
		]
	]
 ]
];`

```

What I expect is to search all documents that their 'name' field starts with 'carrier' but I get all documents that their 'name' field includes 'carrier' word even if it is in the middle, for example all of the following values are matched :

'test carrier'  
'carrier'  
'carrier 1'

But I expect to match just 'carrier' and 'carrier 1' not 'test carrier'. (The 'countryId' and 'active' terms work perfectly)

I've searched alot and couldn't find the reason for this. I've also tried to use other methods like 'wildcard', 'prefix', .... and no difference. Would you please let me know if I'm missing anything ?

Thanks in advance for your help

---

<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: [June 1, 2020, 12:48pm UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/2 "2020-06-01T12:48:11Z")

</div>

Hi Alenoosh,  
I expect the problem is you are searching a `text` field - in that case the regexp is matching tokens (individual words) rather than the full string.  
If you index the field as a `keyword` field you will produce a single token in the index which should make this search make more sense.

---

<div class="post-metadata">

### Author: ![Alenoosh\_Baghoomian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alenoosh_baghoomian/32/69485_2.png) [@Alenoosh\_Baghoomian](https://discuss.elastic.co/u/Alenoosh_Baghoomian)
#### Post date: [June 6, 2020, 4:30am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/3 "2020-06-06T04:30:06Z")

</div>

Hi Mark,  
Thank you for your reply.

I changed type of the 'name' field to 'keyword' but now I get no response. I decided to use '\_sql' api of Elasticsearch and it works for me and it is more flexible.

But I'd like to know the reason 'regexp' did not work for me.

Thanks again for your reply

---

<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: [June 6, 2020, 7:22am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/4 "2020-06-06T07:22:09Z")

</div>

Hmm. Can you share The relevant JSON for the mapping, document and query?

---

<div class="post-metadata">

### Author: ![Alenoosh\_Baghoomian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alenoosh_baghoomian/32/69485_2.png) [@Alenoosh\_Baghoomian](https://discuss.elastic.co/u/Alenoosh_Baghoomian)
#### Post date: [June 6, 2020, 7:35am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/5 "2020-06-06T07:35:23Z")

</div>

I just figured out the issue. The regexp is case sensitive, I did not know about this.

It works perfectly now.

Thanks alot for your help 👍 🙏

---

<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: [June 6, 2020, 11:29am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/6 "2020-06-06T11:29:43Z")

</div>

I’m working on adding case insensitivity 😀

---

<div class="post-metadata">

### Author: ![Alenoosh\_Baghoomian](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alenoosh_baghoomian/32/69485_2.png) [@Alenoosh\_Baghoomian](https://discuss.elastic.co/u/Alenoosh_Baghoomian)
#### Post date: [June 6, 2020, 11:41am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/7 "2020-06-06T11:41:38Z")

</div>

Great 👍 🙂

---

<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 4, 2020, 11:41am UTC](https://discuss.elastic.co/t/regexp-not-searching-as-expected/235155/8 "2020-07-04T11:41:41Z")

</div>

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