# Opposite of prefix query

**URL:** <https://discuss.elastic.co/t/opposite-of-prefix-query/53253>\
**Category:** Elasticsearch\
**Created:** [June 20, 2016, 12:01am UTC](https://discuss.elastic.co/t/opposite-of-prefix-query/53253 "2016-06-20T00:01:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmed](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@ahmed](https://discuss.elastic.co/u/ahmed)\
**Post date:** [June 20, 2016, 12:01am UTC](https://discuss.elastic.co/t/opposite-of-prefix-query/53253/1 "2016-06-20T00:01:11Z")

</div>

I want to make query with a value like '[http://domain.com/path1/path2](http://domain.com/path1/path2)' it should catch the documents that has the following values '[http://domain.com](http://domain.com)' or '[http://domain.com/path1](http://domain.com/path1)' or '[http://domain.com/path1/pat](http://domain.com/path1/pat)' or ''[http://domain.com/path1/path2](http://domain.com/path1/path2)'.  
but it should NOT catch '[http://domain.com/path1/path2/path3](http://domain.com/path1/path2/path3)'

in postgres i used " SELECT url FROM links WHERE ? like url || '%' " or SQL server " SELECT url FROM links WHERE @url like url + '%' ", so how possibly can achieve that using elasticsearch

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [June 20, 2016, 1:16am UTC](https://discuss.elastic.co/t/opposite-of-prefix-query/53253/2 "2016-06-20T01:16:02Z")

</div>

You could do it with a `regex` query. It'd probably be faster than a `script` query because a script query has to be run for all the documents. If you can write a `term` query on the domain part and then use a `script` query to recheck the condition you might get good performance by combining both in the `must` clause of a `bool` query. Might. It is worth trying both. I suspect the `regex` will be better but I can't be sure without trying it.

---

<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, 10:42pm UTC](https://discuss.elastic.co/t/opposite-of-prefix-query/53253/4 "2017-07-05T22:42:21Z")

</div>


