Opposite of prefix query

I want to make query with a value like 'http://domain.com/path1/path2' it should catch the documents that has the following values 'http://domain.com' or 'http://domain.com/path1' or 'http://domain.com/path1/pat' or ''http://domain.com/path1/path2'.
but it should NOT catch '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

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.