# How to stop searching on first match?

**URL:** <https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507>\
**Category:** Elasticsearch\
**Created:** [January 30, 2014, 7:41pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507 "2014-01-30T19:41:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Derf](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@Derf](https://discuss.elastic.co/u/Derf)\
**Post date:** [January 30, 2014, 7:41pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/1 "2014-01-30T19:41:18Z")

</div>

Hi,

I want to know how can I make a search stop when it find just one document.  
I have 5million documents and I'm looking for documents that have some  
numbers. I don't need sorting or scoring.

I think I can filter the result, but I guess that this way, all documents  
will be considered on the search. I need to speed up the search, taking  
only the first match and ignoring the other ones. Is there any way to stop  
a search on the first match?

Now I'm looking this way:  
curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
-d'{"query":{"query\_string":{"query": "09 OR 11"}}}'

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [January 30, 2014, 7:45pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/2 "2014-01-30T19:45:24Z")

</div>

"size": 1  
will only return the top match. Elasticsearch will still count all the  
other matches and order them though. It doesn't have a search type that  
says "get me the first N of these you see I don't care about order". This  
is generally OK because Lucene is really fast at ordering the documents.

Nik

On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:

> Hi,
> 
> I want to know how can I make a search stop when it find just one document.  
> I have 5million documents and I'm looking for documents that have some  
> numbers. I don't need sorting or scoring.
> 
> I think I can filter the result, but I guess that this way, all documents  
> will be considered on the search. I need to speed up the search, taking  
> only the first match and ignoring the other ones. Is there any way to stop  
> a search on the first match?
> 
> Now I'm looking this way:  
> curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derf](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@Derf](https://discuss.elastic.co/u/Derf)\
**Post date:** [January 30, 2014, 8:00pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/3 "2014-01-30T20:00:22Z")

</div>

Thank you.

So there is no way to speed up a search like that one, taking just the  
first match?  
Is there any other way to do that?

May be saying "relax, you job is done" after the first match?  
🙂

I don't know if I was clear. I'm trying to make it work faster by reducing  
it's work.

On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com) wrote:

> "size": 1  
> will only return the top match. Elasticsearch will still count all the  
> other matches and order them though. It doesn't have a search type that  
> says "get me the first N of these you see I don't care about order". This  
> is generally OK because Lucene is really fast at ordering the documents.
> 
> Nik
> 
> On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> 
> > Hi,
> > 
> > I want to know how can I make a search stop when it find just one  
> > document.  
> > I have 5million documents and I'm looking for documents that have some  
> > numbers. I don't need sorting or scoring.
> > 
> > I think I can filter the result, but I guess that this way, all documents  
> > will be considered on the search. I need to speed up the search, taking  
> > only the first match and ignoring the other ones. Is there any way to stop  
> > a search on the first match?
> > 
> > Now I'm looking this way:  
> > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > 
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > .  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
01000110011100100110010101100100

Frederick Dark  
Rede Wireless - Fábrica de Software

Cel.: +55 81 9945-3328

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [January 30, 2014, 8:08pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/4 "2014-01-30T20:08:24Z")

</div>

Not unless you want to send a pull request 🙂 I've wanted one for a bit  
but not badly enough to implement one myself. You can probably save a bit  
of time by disabling scoring by using a filters and a filtered query rather  
than a query string. That disables scoring and in most cases lets  
Elasticsearch cache the individual portions of the query. If you wrote  
your query\_string below with term filters I imagine it'd be faster.

Nik

On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:

> Thank you.
> 
> So there is no way to speed up a search like that one, taking just the  
> first match?  
> Is there any other way to do that?
> 
> May be saying "relax, you job is done" after the first match?  
> 🙂
> 
> I don't know if I was clear. I'm trying to make it work faster by reducing  
> it's work.
> 
> On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> 
> > "size": 1  
> > will only return the top match. Elasticsearch will still count all the  
> > other matches and order them though. It doesn't have a search type that  
> > says "get me the first N of these you see I don't care about order". This  
> > is generally OK because Lucene is really fast at ordering the documents.
> > 
> > Nik
> > 
> > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > 
> > > Hi,
> > > 
> > > I want to know how can I make a search stop when it find just one  
> > > document.  
> > > I have 5million documents and I'm looking for documents that have some  
> > > numbers. I don't need sorting or scoring.
> > > 
> > > I think I can filter the result, but I guess that this way, all  
> > > documents will be considered on the search. I need to speed up the search,  
> > > taking only the first match and ignoring the other ones. Is there any way  
> > > to stop a search on the first match?
> > > 
> > > Now I'm looking this way:  
> > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > 
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > .  
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 01000110011100100110010101100100
> 
> Frederick Dark  
> Rede Wireless - Fábrica de Software
> 
> Cel.: +55 81 9945-3328
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derf](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@Derf](https://discuss.elastic.co/u/Derf)\
**Post date:** [January 30, 2014, 8:17pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/5 "2014-01-30T20:17:29Z")

</div>

Sorry, I'm really novice with ES.  
I don't know how to do that to make it faster.  
Could you give me a clue?

I have 5 million documents, every document have 20 numbers, from 01 to 60,  
like this:  
record 1 = 01 03 06 08 13 19 22 25 28 29 30 31 40 42 44 46 53 55 57 59  
record 2 = 02 05 06 08 11 12 23 25 29 31 34 36 37 43 44 52 54 55 57 58  
record 3 = 04 05 12 14 19 20 24 29 31 35 38 39 40 44 47 53 54 56 57 58  
...

They are all unique. Unique combinations.  
I usually look for documents with certain numbers.  
It takes roughly 0.06 now. How can I make it faster?

If my taking too much of your time, forget it.  
Thank you very much, anyway 🙂

On Thu, Jan 30, 2014 at 5:08 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com) wrote:

> Not unless you want to send a pull request 🙂 I've wanted one for a bit  
> but not badly enough to implement one myself. You can probably save a bit  
> of time by disabling scoring by using a filters and a filtered query rather  
> than a query string. That disables scoring and in most cases lets  
> Elasticsearch cache the individual portions of the query. If you wrote  
> your query\_string below with term filters I imagine it'd be faster.
> 
> Nik
> 
> On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> 
> > Thank you.
> > 
> > So there is no way to speed up a search like that one, taking just the  
> > first match?  
> > Is there any other way to do that?
> > 
> > May be saying "relax, you job is done" after the first match?  
> > 🙂
> > 
> > I don't know if I was clear. I'm trying to make it work faster by  
> > reducing it's work.
> > 
> > On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > 
> > > "size": 1  
> > > will only return the top match. Elasticsearch will still count all the  
> > > other matches and order them though. It doesn't have a search type that  
> > > says "get me the first N of these you see I don't care about order". This  
> > > is generally OK because Lucene is really fast at ordering the documents.
> > > 
> > > Nik
> > > 
> > > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > > 
> > > > Hi,
> > > > 
> > > > I want to know how can I make a search stop when it find just one  
> > > > document.  
> > > > I have 5million documents and I'm looking for documents that have some  
> > > > numbers. I don't need sorting or scoring.
> > > > 
> > > > I think I can filter the result, but I guess that this way, all  
> > > > documents will be considered on the search. I need to speed up the search,  
> > > > taking only the first match and ignoring the other ones. Is there any way  
> > > > to stop a search on the first match?
> > > > 
> > > > Now I'm looking this way:  
> > > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > 
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > > .  
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 01000110011100100110010101100100
> > 
> > Frederick Dark  
> > Rede Wireless - Fábrica de Software
> > 
> > Cel.: +55 81 9945-3328
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
01000110011100100110010101100100

Frederick Dark  
Rede Wireless - Fábrica de Software

Cel.: +55 81 9945-3328

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncEB2u6U9A7e19PH0sOZe\_vOxmo-Ns\_Y%3DM%2Bah53S59xt-w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncEB2u6U9A7e19PH0sOZe_vOxmo-Ns_Y%3DM%2Bah53S59xt-w%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [January 30, 2014, 8:33pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/6 "2014-01-30T20:33:39Z")

</div>

Try something like this:  
{  
"fields": ,  
"query": {  
"filtered": {  
"filter": {  
"terms": {  
"field\_name": ["09", "11"]  
}  
}  
}  
},  
"size": 1  
}

The empty array if fields tells it you don't want any fields from the  
result object. If you leave it out you'll get them all. Use the "\_id"  
that it returns.  
Using a filtered query with a filter like that stops Elasticsearch from  
trying to figure out the score which might save it some math operations and  
some priority queue maintenance.  
You could also switch the field to a number which might make it faster to  
compare. I dunno about that.

Finally, instead of a single query you could use a multi  
search[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html)  
.

Without a change to Elasticsearch you aren't going to get too much faster  
than that. It is still "reasonably" fast though I'll agree you wouldn't  
want to do in a loop. Typically I think of Elasticsearch as something I  
hit once, maybe twice for each request.

See, still pretty fast:  
manybubbles@elastic1008:~$ curl localhost:9200/enwiki/page/\_search?pretty  
-d '{  
"fields": ,  
"query": {  
"filtered": {  
"filter": {  
"terms": {  
"namespace": [0, 1]  
}  
}  
}  
},  
"size": 1  
}'  
{  
"took" : 78,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 40,  
"successful" : 40,  
"failed" : 0  
},  
"hits" : {  
"total" : 9216713,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "enwiki\_general\_first",  
"\_type" : "page",  
"\_id" : "20019",  
"\_score" : 1.0  
} ]  
}  
}

Oh! You might want to think about  
routing[http://www.elasticsearch.org/blog/customizing-your-document-routing/](http://www.elasticsearch.org/blog/customizing-your-document-routing/).  
Adding routing cuts the request above from 78 millis in Elasticsearch to  
17. It isn't really useful for my use case but you might be able to figure  
it out for yours.

Nik

On Thu, Jan 30, 2014 at 3:17 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:

> Sorry, I'm really novice with ES.  
> I don't know how to do that to make it faster.  
> Could you give me a clue?
> 
> I have 5 million documents, every document have 20 numbers, from 01 to 60,  
> like this:  
> record 1 = 01 03 06 08 13 19 22 25 28 29 30 31 40 42 44 46 53 55 57 59  
> record 2 = 02 05 06 08 11 12 23 25 29 31 34 36 37 43 44 52 54 55 57 58  
> record 3 = 04 05 12 14 19 20 24 29 31 35 38 39 40 44 47 53 54 56 57 58  
> ...
> 
> They are all unique. Unique combinations.  
> I usually look for documents with certain numbers.  
> It takes roughly 0.06 now. How can I make it faster?
> 
> If my taking too much of your time, forget it.  
> Thank you very much, anyway 🙂
> 
> On Thu, Jan 30, 2014 at 5:08 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> 
> > Not unless you want to send a pull request 🙂 I've wanted one for a bit  
> > but not badly enough to implement one myself. You can probably save a bit  
> > of time by disabling scoring by using a filters and a filtered query rather  
> > than a query string. That disables scoring and in most cases lets  
> > Elasticsearch cache the individual portions of the query. If you wrote  
> > your query\_string below with term filters I imagine it'd be faster.
> > 
> > Nik
> > 
> > On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> > 
> > > Thank you.
> > > 
> > > So there is no way to speed up a search like that one, taking just the  
> > > first match?  
> > > Is there any other way to do that?
> > > 
> > > May be saying "relax, you job is done" after the first match?  
> > > 🙂
> > > 
> > > I don't know if I was clear. I'm trying to make it work faster by  
> > > reducing it's work.
> > > 
> > > On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > 
> > > > "size": 1  
> > > > will only return the top match. Elasticsearch will still count all the  
> > > > other matches and order them though. It doesn't have a search type that  
> > > > says "get me the first N of these you see I don't care about order". This  
> > > > is generally OK because Lucene is really fast at ordering the documents.
> > > > 
> > > > Nik
> > > > 
> > > > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > > > 
> > > > > Hi,
> > > > > 
> > > > > I want to know how can I make a search stop when it find just one  
> > > > > document.  
> > > > > I have 5million documents and I'm looking for documents that have some  
> > > > > numbers. I don't need sorting or scoring.
> > > > > 
> > > > > I think I can filter the result, but I guess that this way, all  
> > > > > documents will be considered on the search. I need to speed up the search,  
> > > > > taking only the first match and ignoring the other ones. Is there any way  
> > > > > to stop a search on the first match?
> > > > > 
> > > > > Now I'm looking this way:  
> > > > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to the Google  
> > > > > Groups "elasticsearch" group.  
> > > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > > 
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > > > .  
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > 01000110011100100110010101100100
> > > 
> > > Frederick Dark  
> > > Rede Wireless - Fábrica de Software
> > > 
> > > Cel.: +55 81 9945-3328
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 01000110011100100110010101100100
> 
> Frederick Dark  
> Rede Wireless - Fábrica de Software
> 
> Cel.: +55 81 9945-3328
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncEB2u6U9A7e19PH0sOZe\_vOxmo-Ns\_Y%3DM%2Bah53S59xt-w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncEB2u6U9A7e19PH0sOZe_vOxmo-Ns_Y%3DM%2Bah53S59xt-w%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe\_oSCV9TiySuYGLA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe_oSCV9TiySuYGLA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derf](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@Derf](https://discuss.elastic.co/u/Derf)\
**Post date:** [January 30, 2014, 9:11pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/7 "2014-01-30T21:11:41Z")

</div>

humm.. the problem now is that it could not find any document.

My documents are like this:  
"numbers": "01 02 03 04 ..."

Maybe this is the problema, right?

On Thu, Jan 30, 2014 at 5:33 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com) wrote:

> Try something like this:  
> {  
> "fields": ,  
> "query": {  
> "filtered": {  
> "filter": {  
> "terms": {  
> "field\_name": ["09", "11"]  
> }  
> }  
> }  
> },  
> "size": 1  
> }
> 
> The empty array if fields tells it you don't want any fields from the  
> result object. If you leave it out you'll get them all. Use the "\_id"  
> that it returns.  
> Using a filtered query with a filter like that stops Elasticsearch from  
> trying to figure out the score which might save it some math operations and  
> some priority queue maintenance.  
> You could also switch the field to a number which might make it faster to  
> compare. I dunno about that.
> 
> Finally, instead of a single query you could use a multi search[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html)  
> .
> 
> Without a change to Elasticsearch you aren't going to get too much faster  
> than that. It is still "reasonably" fast though I'll agree you wouldn't  
> want to do in a loop. Typically I think of Elasticsearch as something I  
> hit once, maybe twice for each request.
> 
> See, still pretty fast:  
> manybubbles@elastic1008:~$ curl localhost:9200/enwiki/page/\_search?pretty  
> -d '{  
> "fields": ,  
> "query": {  
> "filtered": {  
> "filter": {  
> "terms": {  
> "namespace": [0, 1]  
> }  
> }  
> }  
> },  
> "size": 1  
> }'  
> {  
> "took" : 78,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 40,  
> "successful" : 40,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 9216713,  
> "max\_score" : 1.0,  
> "hits" : [ {  
> "\_index" : "enwiki\_general\_first",  
> "\_type" : "page",  
> "\_id" : "20019",  
> "\_score" : 1.0  
> } ]  
> }  
> }
> 
> Oh! You might want to think about routing[http://www.elasticsearch.org/blog/customizing-your-document-routing/](http://www.elasticsearch.org/blog/customizing-your-document-routing/).  
> Adding routing cuts the request above from 78 millis in Elasticsearch to  
> 17. It isn't really useful for my use case but you might be able to figure  
> it out for yours.
> 
> Nik
> 
> On Thu, Jan 30, 2014 at 3:17 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> 
> > Sorry, I'm really novice with ES.  
> > I don't know how to do that to make it faster.  
> > Could you give me a clue?
> > 
> > I have 5 million documents, every document have 20 numbers, from 01 to  
> > 60, like this:  
> > record 1 = 01 03 06 08 13 19 22 25 28 29 30 31 40 42 44 46 53 55 57 59  
> > record 2 = 02 05 06 08 11 12 23 25 29 31 34 36 37 43 44 52 54 55 57 58  
> > record 3 = 04 05 12 14 19 20 24 29 31 35 38 39 40 44 47 53 54 56 57 58  
> > ...
> > 
> > They are all unique. Unique combinations.  
> > I usually look for documents with certain numbers.  
> > It takes roughly 0.06 now. How can I make it faster?
> > 
> > If my taking too much of your time, forget it.  
> > Thank you very much, anyway 🙂
> > 
> > On Thu, Jan 30, 2014 at 5:08 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > 
> > > Not unless you want to send a pull request 🙂 I've wanted one for a bit  
> > > but not badly enough to implement one myself. You can probably save a bit  
> > > of time by disabling scoring by using a filters and a filtered query rather  
> > > than a query string. That disables scoring and in most cases lets  
> > > Elasticsearch cache the individual portions of the query. If you wrote  
> > > your query\_string below with term filters I imagine it'd be faster.
> > > 
> > > Nik
> > > 
> > > On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> > > 
> > > > Thank you.
> > > > 
> > > > So there is no way to speed up a search like that one, taking just the  
> > > > first match?  
> > > > Is there any other way to do that?
> > > > 
> > > > May be saying "relax, you job is done" after the first match?  
> > > > 🙂
> > > > 
> > > > I don't know if I was clear. I'm trying to make it work faster by  
> > > > reducing it's work.
> > > > 
> > > > On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > > 
> > > > > "size": 1  
> > > > > will only return the top match. Elasticsearch will still count all  
> > > > > the other matches and order them though. It doesn't have a search type  
> > > > > that says "get me the first N of these you see I don't care about order".  
> > > > > This is generally OK because Lucene is really fast at ordering the  
> > > > > documents.
> > > > > 
> > > > > Nik
> > > > > 
> > > > > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > > > > 
> > > > > > Hi,
> > > > > > 
> > > > > > I want to know how can I make a search stop when it find just one  
> > > > > > document.  
> > > > > > I have 5million documents and I'm looking for documents that have  
> > > > > > some numbers. I don't need sorting or scoring.
> > > > > > 
> > > > > > I think I can filter the result, but I guess that this way, all  
> > > > > > documents will be considered on the search. I need to speed up the search,  
> > > > > > taking only the first match and ignoring the other ones. Is there any way  
> > > > > > to stop a search on the first match?
> > > > > > 
> > > > > > Now I'm looking this way:  
> > > > > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > > > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > > > > 
> > > > > > --  
> > > > > > You received this message because you are subscribed to the Google  
> > > > > > Groups "elasticsearch" group.  
> > > > > > To unsubscribe from this group and stop receiving emails from it,  
> > > > > > send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > > > 
> > > > > > To view this discussion on the web visit  
> > > > > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > > > > .  
> > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to a topic in the  
> > > > > Google Groups "elasticsearch" group.  
> > > > > To unsubscribe from this topic, visit  
> > > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > > .  
> > > > > To unsubscribe from this group and all its topics, send an email to  
> > > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > > > > .
> > > > > 
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > --  
> > > > 01000110011100100110010101100100
> > > > 
> > > > Frederick Dark  
> > > > Rede Wireless - Fábrica de Software
> > > > 
> > > > Cel.: +55 81 9945-3328
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 01000110011100100110010101100100
> > 
> > Frederick Dark  
> > Rede Wireless - Fábrica de Software
> > 
> > Cel.: +55 81 9945-3328
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncEB2u6U9A7e19PH0sOZe\_vOxmo-Ns\_Y%3DM%2Bah53S59xt-w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncEB2u6U9A7e19PH0sOZe_vOxmo-Ns_Y%3DM%2Bah53S59xt-w%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe\_oSCV9TiySuYGLA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe_oSCV9TiySuYGLA%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
01000110011100100110010101100100

Frederick Dark  
Rede Wireless - Fábrica de Software

Cel.: +55 81 9945-3328

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0\_3s%3DvQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0_3s%3DvQ%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [January 30, 2014, 9:13pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/8 "2014-01-30T21:13:23Z")

</div>

You'd have to fiddle with it, I think. I'm not sure what analyzer you are  
using.

On Thu, Jan 30, 2014 at 4:11 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:

> humm.. the problem now is that it could not find any document.
> 
> My documents are like this:  
> "numbers": "01 02 03 04 ..."
> 
> Maybe this is the problema, right?
> 
> On Thu, Jan 30, 2014 at 5:33 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> 
> > Try something like this:  
> > {  
> > "fields": ,  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "terms": {  
> > "field\_name": ["09", "11"]  
> > }  
> > }  
> > }  
> > },  
> > "size": 1  
> > }
> > 
> > The empty array if fields tells it you don't want any fields from the  
> > result object. If you leave it out you'll get them all. Use the "\_id"  
> > that it returns.  
> > Using a filtered query with a filter like that stops Elasticsearch from  
> > trying to figure out the score which might save it some math operations and  
> > some priority queue maintenance.  
> > You could also switch the field to a number which might make it faster to  
> > compare. I dunno about that.
> > 
> > Finally, instead of a single query you could use a multi search[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html)  
> > .
> > 
> > Without a change to Elasticsearch you aren't going to get too much faster  
> > than that. It is still "reasonably" fast though I'll agree you wouldn't  
> > want to do in a loop. Typically I think of Elasticsearch as something I  
> > hit once, maybe twice for each request.
> > 
> > See, still pretty fast:  
> > manybubbles@elastic1008:~$ curl  
> > localhost:9200/enwiki/page/\_search?pretty -d '{  
> > "fields": ,  
> > "query": {  
> > "filtered": {  
> > "filter": {  
> > "terms": {  
> > "namespace": [0, 1]  
> > }  
> > }  
> > }  
> > },  
> > "size": 1  
> > }'  
> > {  
> > "took" : 78,  
> > "timed\_out" : false,  
> > "\_shards" : {  
> > "total" : 40,  
> > "successful" : 40,  
> > "failed" : 0  
> > },  
> > "hits" : {  
> > "total" : 9216713,  
> > "max\_score" : 1.0,  
> > "hits" : [ {  
> > "\_index" : "enwiki\_general\_first",  
> > "\_type" : "page",  
> > "\_id" : "20019",  
> > "\_score" : 1.0  
> > } ]  
> > }  
> > }
> > 
> > Oh! You might want to think about routing[http://www.elasticsearch.org/blog/customizing-your-document-routing/](http://www.elasticsearch.org/blog/customizing-your-document-routing/).  
> > Adding routing cuts the request above from 78 millis in Elasticsearch to  
> > 17. It isn't really useful for my use case but you might be able to figure  
> > it out for yours.
> > 
> > Nik
> > 
> > On Thu, Jan 30, 2014 at 3:17 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> > 
> > > Sorry, I'm really novice with ES.  
> > > I don't know how to do that to make it faster.  
> > > Could you give me a clue?
> > > 
> > > I have 5 million documents, every document have 20 numbers, from 01 to  
> > > 60, like this:  
> > > record 1 = 01 03 06 08 13 19 22 25 28 29 30 31 40 42 44 46 53 55 57 59  
> > > record 2 = 02 05 06 08 11 12 23 25 29 31 34 36 37 43 44 52 54 55 57 58  
> > > record 3 = 04 05 12 14 19 20 24 29 31 35 38 39 40 44 47 53 54 56 57 58  
> > > ...
> > > 
> > > They are all unique. Unique combinations.  
> > > I usually look for documents with certain numbers.  
> > > It takes roughly 0.06 now. How can I make it faster?
> > > 
> > > If my taking too much of your time, forget it.  
> > > Thank you very much, anyway 🙂
> > > 
> > > On Thu, Jan 30, 2014 at 5:08 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > 
> > > > Not unless you want to send a pull request 🙂 I've wanted one for a  
> > > > bit but not badly enough to implement one myself. You can probably save a  
> > > > bit of time by disabling scoring by using a filters and a filtered query  
> > > > rather than a query string. That disables scoring and in most cases lets  
> > > > Elasticsearch cache the individual portions of the query. If you wrote  
> > > > your query\_string below with term filters I imagine it'd be faster.
> > > > 
> > > > Nik
> > > > 
> > > > On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> > > > 
> > > > > Thank you.
> > > > > 
> > > > > So there is no way to speed up a search like that one, taking just the  
> > > > > first match?  
> > > > > Is there any other way to do that?
> > > > > 
> > > > > May be saying "relax, you job is done" after the first match?  
> > > > > 🙂
> > > > > 
> > > > > I don't know if I was clear. I'm trying to make it work faster by  
> > > > > reducing it's work.
> > > > > 
> > > > > On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > > > 
> > > > > > "size": 1  
> > > > > > will only return the top match. Elasticsearch will still count all  
> > > > > > the other matches and order them though. It doesn't have a search type  
> > > > > > that says "get me the first N of these you see I don't care about order".  
> > > > > > This is generally OK because Lucene is really fast at ordering the  
> > > > > > documents.
> > > > > > 
> > > > > > Nik
> > > > > > 
> > > > > > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > > > > > 
> > > > > > > Hi,
> > > > > > > 
> > > > > > > I want to know how can I make a search stop when it find just one  
> > > > > > > document.  
> > > > > > > I have 5million documents and I'm looking for documents that have  
> > > > > > > some numbers. I don't need sorting or scoring.
> > > > > > > 
> > > > > > > I think I can filter the result, but I guess that this way, all  
> > > > > > > documents will be considered on the search. I need to speed up the search,  
> > > > > > > taking only the first match and ignoring the other ones. Is there any way  
> > > > > > > to stop a search on the first match?
> > > > > > > 
> > > > > > > Now I'm looking this way:  
> > > > > > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > > > > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > > > > > 
> > > > > > > --  
> > > > > > > You received this message because you are subscribed to the Google  
> > > > > > > Groups "elasticsearch" group.  
> > > > > > > To unsubscribe from this group and stop receiving emails from it,  
> > > > > > > send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > > > > 
> > > > > > > To view this discussion on the web visit  
> > > > > > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > > > > > .  
> > > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > 
> > > > > > --  
> > > > > > You received this message because you are subscribed to a topic in  
> > > > > > the Google Groups "elasticsearch" group.  
> > > > > > To unsubscribe from this topic, visit  
> > > > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > > > .  
> > > > > > To unsubscribe from this group and all its topics, send an email to  
> > > > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > > To view this discussion on the web visit  
> > > > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > > > > > .
> > > > > > 
> > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > 
> > > > > --  
> > > > > 01000110011100100110010101100100
> > > > > 
> > > > > Frederick Dark  
> > > > > Rede Wireless - Fábrica de Software
> > > > > 
> > > > > Cel.: +55 81 9945-3328
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to the Google  
> > > > > Groups "elasticsearch" group.  
> > > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> > > > > .
> > > > > 
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to a topic in the  
> > > > Google Groups "elasticsearch" group.  
> > > > To unsubscribe from this topic, visit  
> > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > .  
> > > > To unsubscribe from this group and all its topics, send an email to  
> > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > 01000110011100100110010101100100
> > > 
> > > Frederick Dark  
> > > Rede Wireless - Fábrica de Software
> > > 
> > > Cel.: +55 81 9945-3328
> > > 
> > > --  
> > > You received this message because you are subscribed to the Google  
> > > Groups "elasticsearch" group.  
> > > To unsubscribe from this group and stop receiving emails from it, send  
> > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncEB2u6U9A7e19PH0sOZe\_vOxmo-Ns\_Y%3DM%2Bah53S59xt-w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncEB2u6U9A7e19PH0sOZe_vOxmo-Ns_Y%3DM%2Bah53S59xt-w%40mail.gmail.com)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe\_oSCV9TiySuYGLA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe_oSCV9TiySuYGLA%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 01000110011100100110010101100100
> 
> Frederick Dark  
> Rede Wireless - Fábrica de Software
> 
> Cel.: +55 81 9945-3328
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0\_3s%3DvQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0_3s%3DvQ%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0BV\_XtvHCVt27xpseHezXwJRAZvnKeGj8fv9F8LgJm4g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0BV_XtvHCVt27xpseHezXwJRAZvnKeGj8fv9F8LgJm4g%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Derf](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@Derf](https://discuss.elastic.co/u/Derf)\
**Post date:** [January 30, 2014, 9:15pm UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/9 "2014-01-30T21:15:46Z")

</div>

OK, I think I'm gonna have some fun with it.  
Thank you very much, Nikolas.  
😉

On Thu, Jan 30, 2014 at 6:13 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com) wrote:

> You'd have to fiddle with it, I think. I'm not sure what analyzer you are  
> using.
> 
> On Thu, Jan 30, 2014 at 4:11 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> 
> > humm.. the problem now is that it could not find any document.
> > 
> > My documents are like this:  
> > "numbers": "01 02 03 04 ..."
> > 
> > Maybe this is the problema, right?
> > 
> > On Thu, Jan 30, 2014 at 5:33 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > 
> > > Try something like this:  
> > > {  
> > > "fields": ,  
> > > "query": {  
> > > "filtered": {  
> > > "filter": {  
> > > "terms": {  
> > > "field\_name": ["09", "11"]  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "size": 1  
> > > }
> > > 
> > > The empty array if fields tells it you don't want any fields from the  
> > > result object. If you leave it out you'll get them all. Use the "\_id"  
> > > that it returns.  
> > > Using a filtered query with a filter like that stops Elasticsearch from  
> > > trying to figure out the score which might save it some math operations and  
> > > some priority queue maintenance.  
> > > You could also switch the field to a number which might make it faster  
> > > to compare. I dunno about that.
> > > 
> > > Finally, instead of a single query you could use a multi search[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html)  
> > > .
> > > 
> > > Without a change to Elasticsearch you aren't going to get too much  
> > > faster than that. It is still "reasonably" fast though I'll agree you  
> > > wouldn't want to do in a loop. Typically I think of Elasticsearch as  
> > > something I hit once, maybe twice for each request.
> > > 
> > > See, still pretty fast:  
> > > manybubbles@elastic1008:~$ curl  
> > > localhost:9200/enwiki/page/\_search?pretty -d '{  
> > > "fields": ,  
> > > "query": {  
> > > "filtered": {  
> > > "filter": {  
> > > "terms": {  
> > > "namespace": [0, 1]  
> > > }  
> > > }  
> > > }  
> > > },  
> > > "size": 1  
> > > }'  
> > > {  
> > > "took" : 78,  
> > > "timed\_out" : false,  
> > > "\_shards" : {  
> > > "total" : 40,  
> > > "successful" : 40,  
> > > "failed" : 0  
> > > },  
> > > "hits" : {  
> > > "total" : 9216713,  
> > > "max\_score" : 1.0,  
> > > "hits" : [ {  
> > > "\_index" : "enwiki\_general\_first",  
> > > "\_type" : "page",  
> > > "\_id" : "20019",  
> > > "\_score" : 1.0  
> > > } ]  
> > > }  
> > > }
> > > 
> > > Oh! You might want to think about routing[http://www.elasticsearch.org/blog/customizing-your-document-routing/](http://www.elasticsearch.org/blog/customizing-your-document-routing/).  
> > > Adding routing cuts the request above from 78 millis in Elasticsearch to  
> > > 17. It isn't really useful for my use case but you might be able to figure  
> > > it out for yours.
> > > 
> > > Nik
> > > 
> > > On Thu, Jan 30, 2014 at 3:17 PM, Frederick Dark [fred@redewireless.net](mailto:fred@redewireless.net)wrote:
> > > 
> > > > Sorry, I'm really novice with ES.  
> > > > I don't know how to do that to make it faster.  
> > > > Could you give me a clue?
> > > > 
> > > > I have 5 million documents, every document have 20 numbers, from 01 to  
> > > > 60, like this:  
> > > > record 1 = 01 03 06 08 13 19 22 25 28 29 30 31 40 42 44 46 53 55 57 59  
> > > > record 2 = 02 05 06 08 11 12 23 25 29 31 34 36 37 43 44 52 54 55 57 58  
> > > > record 3 = 04 05 12 14 19 20 24 29 31 35 38 39 40 44 47 53 54 56 57 58  
> > > > ...
> > > > 
> > > > They are all unique. Unique combinations.  
> > > > I usually look for documents with certain numbers.  
> > > > It takes roughly 0.06 now. How can I make it faster?
> > > > 
> > > > If my taking too much of your time, forget it.  
> > > > Thank you very much, anyway 🙂
> > > > 
> > > > On Thu, Jan 30, 2014 at 5:08 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > > 
> > > > > Not unless you want to send a pull request 🙂 I've wanted one for a  
> > > > > bit but not badly enough to implement one myself. You can probably save a  
> > > > > bit of time by disabling scoring by using a filters and a filtered query  
> > > > > rather than a query string. That disables scoring and in most cases lets  
> > > > > Elasticsearch cache the individual portions of the query. If you wrote  
> > > > > your query\_string below with term filters I imagine it'd be faster.
> > > > > 
> > > > > Nik
> > > > > 
> > > > > On Thu, Jan 30, 2014 at 3:00 PM, Frederick Dark \<[fred@redewireless.net](mailto:fred@redewireless.net)
> > > > > 
> > > > > > wrote:
> > > > > 
> > > > > > Thank you.
> > > > > > 
> > > > > > So there is no way to speed up a search like that one, taking just  
> > > > > > the first match?  
> > > > > > Is there any other way to do that?
> > > > > > 
> > > > > > May be saying "relax, you job is done" after the first match?  
> > > > > > 🙂
> > > > > > 
> > > > > > I don't know if I was clear. I'm trying to make it work faster by  
> > > > > > reducing it's work.
> > > > > > 
> > > > > > On Thu, Jan 30, 2014 at 4:45 PM, Nikolas Everett [nik9000@gmail.com](mailto:nik9000@gmail.com)wrote:
> > > > > > 
> > > > > > > "size": 1  
> > > > > > > will only return the top match. Elasticsearch will still count all  
> > > > > > > the other matches and order them though. It doesn't have a search type  
> > > > > > > that says "get me the first N of these you see I don't care about order".  
> > > > > > > This is generally OK because Lucene is really fast at ordering the  
> > > > > > > documents.
> > > > > > > 
> > > > > > > Nik
> > > > > > > 
> > > > > > > On Thu, Jan 30, 2014 at 2:41 PM, Derf [fred@redewireless.net](mailto:fred@redewireless.net) wrote:
> > > > > > > 
> > > > > > > > Hi,
> > > > > > > > 
> > > > > > > > I want to know how can I make a search stop when it find just one  
> > > > > > > > document.  
> > > > > > > > I have 5million documents and I'm looking for documents that have  
> > > > > > > > some numbers. I don't need sorting or scoring.
> > > > > > > > 
> > > > > > > > I think I can filter the result, but I guess that this way, all  
> > > > > > > > documents will be considered on the search. I need to speed up the search,  
> > > > > > > > taking only the first match and ignoring the other ones. Is there any way  
> > > > > > > > to stop a search on the first match?
> > > > > > > > 
> > > > > > > > Now I'm looking this way:  
> > > > > > > > curl -XPOST "[http://log01:9200/ncards/card/\_search](http://log01:9200/ncards/card/_search)"  
> > > > > > > > -d'{"query":{"query\_string":{"query": "09 OR 11"}}}'
> > > > > > > > 
> > > > > > > > --  
> > > > > > > > You received this message because you are subscribed to the Google  
> > > > > > > > Groups "elasticsearch" group.  
> > > > > > > > To unsubscribe from this group and stop receiving emails from it,  
> > > > > > > > send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).
> > > > > > > > 
> > > > > > > > To view this discussion on the web visit  
> > > > > > > > [https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/4f755ad9-78eb-4547-b4ca-df3fc9234d37%40googlegroups.com)  
> > > > > > > > .  
> > > > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > > 
> > > > > > > --  
> > > > > > > You received this message because you are subscribed to a topic in  
> > > > > > > the Google Groups "elasticsearch" group.  
> > > > > > > To unsubscribe from this topic, visit  
> > > > > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > > > > .  
> > > > > > > To unsubscribe from this group and all its topics, send an email to  
> > > > > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > > > To view this discussion on the web visit  
> > > > > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd3E703D%3DktLJnNrdjvhdcgvaSSMMHuULiWjAVRu0fpZWw%40mail.gmail.com)  
> > > > > > > .
> > > > > > > 
> > > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > > 
> > > > > > --  
> > > > > > 01000110011100100110010101100100
> > > > > > 
> > > > > > Frederick Dark  
> > > > > > Rede Wireless - Fábrica de Software
> > > > > > 
> > > > > > Cel.: +55 81 9945-3328
> > > > > > 
> > > > > > --  
> > > > > > You received this message because you are subscribed to the Google  
> > > > > > Groups "elasticsearch" group.  
> > > > > > To unsubscribe from this group and stop receiving emails from it,  
> > > > > > send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > > To view this discussion on the web visit  
> > > > > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO\_xb5Yvrg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncGMgH8EfjG%2B6Sn2dBq%3DAcf9Uc0atS5vGLbWtO_xb5Yvrg%40mail.gmail.com)  
> > > > > > .
> > > > > > 
> > > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > > 
> > > > > --  
> > > > > You received this message because you are subscribed to a topic in the  
> > > > > Google Groups "elasticsearch" group.  
> > > > > To unsubscribe from this topic, visit  
> > > > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe)  
> > > > > .  
> > > > > To unsubscribe from this group and all its topics, send an email to  
> > > > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > > To view this discussion on the web visit  
> > > > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd2XCSMQr1ypGSCSu77rn0DPU7%3Ddnodg1417PsKdnsoqrA%40mail.gmail.com)  
> > > > > .
> > > > > 
> > > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > > 
> > > > --  
> > > > 01000110011100100110010101100100
> > > > 
> > > > Frederick Dark  
> > > > Rede Wireless - Fábrica de Software
> > > > 
> > > > Cel.: +55 81 9945-3328
> > > > 
> > > > --  
> > > > You received this message because you are subscribed to the Google  
> > > > Groups "elasticsearch" group.  
> > > > To unsubscribe from this group and stop receiving emails from it, send  
> > > > an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > > To view this discussion on the web visit  
> > > > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncEB2u6U9A7e19PH0sOZe\_vOxmo-Ns\_Y%3DM%2Bah53S59xt-w%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncEB2u6U9A7e19PH0sOZe_vOxmo-Ns_Y%3DM%2Bah53S59xt-w%40mail.gmail.com)  
> > > > .
> > > > 
> > > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > > 
> > > --  
> > > You received this message because you are subscribed to a topic in the  
> > > Google Groups "elasticsearch" group.  
> > > To unsubscribe from this topic, visit  
> > > [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> > > To unsubscribe from this group and all its topics, send an email to  
> > > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > > To view this discussion on the web visit  
> > > [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe\_oSCV9TiySuYGLA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0Ubvw4Jk5LtahdQcbgSNvS8qOVMpe_oSCV9TiySuYGLA%40mail.gmail.com)  
> > > .
> > > 
> > > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> > 
> > --  
> > 01000110011100100110010101100100
> > 
> > Frederick Dark  
> > Rede Wireless - Fábrica de Software
> > 
> > Cel.: +55 81 9945-3328
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0\_3s%3DvQ%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncFZy1ESfxUBqZiJVPdAdgXBsUNCjbXB-R4ZhWn0_3s%3DvQ%40mail.gmail.com)  
> > .
> > 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe](https://groups.google.com/d/topic/elasticsearch/C-yzL1zmbp8/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0BV\_XtvHCVt27xpseHezXwJRAZvnKeGj8fv9F8LgJm4g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPmjWd0BV_XtvHCVt27xpseHezXwJRAZvnKeGj8fv9F8LgJm4g%40mail.gmail.com)  
> .
> 
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
01000110011100100110010101100100

Frederick Dark  
Rede Wireless - Fábrica de Software

Cel.: +55 81 9945-3328

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAF\_mncFRwqmCvCre7n22f%2BsNQSKurcwz8t13ToMUtT2Tq8F8ow%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAF_mncFRwqmCvCre7n22f%2BsNQSKurcwz8t13ToMUtT2Tq8F8ow%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 1:53am UTC](https://discuss.elastic.co/t/how-to-stop-searching-on-first-match/15507/10 "2017-07-06T01:53:52Z")

</div>


