Regexp not searching as expected

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

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.

1 Like

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

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

1 Like

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 :+1: :pray:

1 Like

I’m working on adding case insensitivity :grinning:

1 Like

Great :+1: :slightly_smiling_face:

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