# Limit results in query

**URL:** <https://discuss.elastic.co/t/limit-results-in-query/6202>\
**Category:** Elasticsearch\
**Created:** [December 20, 2011, 11:53pm UTC](https://discuss.elastic.co/t/limit-results-in-query/6202 "2011-12-20T23:53:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_S](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/brian_s/32/3043_2.png) [@Brian\_S](https://discuss.elastic.co/u/Brian_S)\
**Post date:** [December 20, 2011, 11:53pm UTC](https://discuss.elastic.co/t/limit-results-in-query/6202/1 "2011-12-20T23:53:26Z")

</div>

In MySQL I can do something like:

SELECT id FROM table WHERE field = 'foo' LIMIT 5

If the table has 10,000 rows, then this query is way way faster than  
if I left out the LIMIT part.

In ElasticSearch, I've got the following:

{  
"query":{  
"fuzzy\_like\_this\_field":{  
"body":{  
"like\_text":"REALLY LONG (snip) TEXT HERE",  
"max\_query\_terms":1,  
"min\_similarity":0.95,  
"ignore\_tf":true  
}  
}  
}  
}

When I run this search, it takes a few seconds, whereas mysql can  
return results for the same query in far, far less time.

If I pass in the size parameter (set to 1), it successfully only  
returns 1 result, but the query itself doesn't appear to be any faster  
than if I had set the size to unlimited and returned all the results.  
I suspect the query is being run in its entirety and only 1 result is  
being returned after the query is done processing. This means the  
"size" attribute is useless for my purposes.

Is there any way to have my search stop searching as soon as it finds  
a single record that matches the fuzzy search, rather than processing  
every record in the index before returning a response? Am I  
misunderstanding something more fundamental about this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![James\_Cook\_3](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@James\_Cook\_3](https://discuss.elastic.co/u/James_Cook_3)\
**Post date:** [December 21, 2011, 1:12am UTC](https://discuss.elastic.co/t/limit-results-in-query/6202/2 "2011-12-21T01:12:18Z")

</div>

I think you are misunderstanding how MySQL works. It calculates the entire  
result set prior to limiting the number of results in a response. This is  
understood clearly when you add an ORDER BY clause to the SQL query. It is  
true with MySQL that if your ORDER BY and WHERE clauses include only fields  
which are indexed, the indicies can be used to prevent a table scan  
situation.

Your comparison of a fuzzy search using a "REALLY LONG..." text query in es  
vs. a query by an exact match on a field that might be indexed in MySQL is  
truly an apples to oranges question. You might as well of asked "How come  
it takes my Mini Cooper only 15 seconds to do the 1/4 mile, when it takes  
your Maserati Quattroporte an hour to drive from LA to San  
Diego. Relational databases are very fast if they can avoid table scans and  
just use indicies for queries.

I don't know much about how ES handles a fuzzy search, so someone can  
correct me if I am way off base here. I suspect es will use its indicies to  
perform the search, except it has to calculate the levenshtein distance  
between the terms in each document against each term in your search  
criteria. Something that gets exponentially (?) more time consuming the  
more terms you have in your search query or the more terms in each document.

---

<div class="post-metadata">

**Author:** ![mattweber](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mattweber/32/44940_2.png) [@mattweber](https://discuss.elastic.co/u/mattweber)\
**Post date:** [December 21, 2011, 1:33am UTC](https://discuss.elastic.co/t/limit-results-in-query/6202/3 "2011-12-21T01:33:02Z")

</div>

Your SQL is equivalent to this query:

curl -XGET '[http://localhost:9200/index/type/\_search?q=field:foo&size=5&fields=](http://localhost:9200/index/type/_search?q=field:foo&size=5&fields=)'

Anyways, have you tried using "query and fetch"?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Thanks,  
Matt Weber

On Tue, Dec 20, 2011 at 5:12 PM, James Cook [jcook@pykl.com](mailto:jcook@pykl.com) wrote:

> I think you are misunderstanding how MySQL works. It calculates the entire  
> result set prior to limiting the number of results in a response. This is  
> understood clearly when you add an ORDER BY clause to the SQL query. It is  
> true with MySQL that if your ORDER BY and WHERE clauses include only fields  
> which are indexed, the indicies can be used to prevent a table scan  
> situation.
> 
> Your comparison of a fuzzy search using a "REALLY LONG..." text query in es  
> vs. a query by an exact match on a field that might be indexed in MySQL is  
> truly an apples to oranges question. You might as well of asked "How come it  
> takes my Mini Cooper only 15 seconds to do the 1/4 mile, when it takes your  
> Maserati Quattroporte an hour to drive from LA to San Diego. Relational  
> databases are very fast if they can avoid table scans and just use indicies  
> for queries.
> 
> I don't know much about how ES handles a fuzzy search, so someone can  
> correct me if I am way off base here. I suspect es will use its indicies to  
> perform the search, except it has to calculate the levenshtein distance  
> between the terms in each document against each term in your search  
> criteria. Something that gets exponentially (?) more time consuming the more  
> terms you have in your search query or the more terms in each document.

---

<div class="post-metadata">

**Author:** ![Karussell1](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@Karussell1](https://discuss.elastic.co/u/Karussell1)\
**Post date:** [December 21, 2011, 7:53am UTC](https://discuss.elastic.co/t/limit-results-in-query/6202/4 "2011-12-21T07:53:40Z")

</div>

> Your comparison of a fuzzy search using a "REALLY LONG..." text query in es  
> vs. a query by an exact match

Thats exactly the point. @Brian: To do an exact string match like in  
MySql just do not analyze the field and I bet you'll get identical if  
not better response times.

Peter.

---

<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 6, 2017, 3:44am UTC](https://discuss.elastic.co/t/limit-results-in-query/6202/5 "2017-07-06T03:44:46Z")

</div>


