# Closest number query

**URL:** https://discuss.elastic.co/t/closest-number-query/11275
**Category:** Elasticsearch
**Created:** [March 23, 2013, 11:28pm UTC](https://discuss.elastic.co/t/closest-number-query/11275 "2013-03-23T23:28:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)
#### Post date: [March 23, 2013, 11:28pm UTC](https://discuss.elastic.co/t/closest-number-query/11275/1 "2013-03-23T23:28:35Z")

</div>

Hello,  
I'm working on interesting problem, and I think it could be solved using ES  
features.  
I have millions of documents, which have an multi-field of 2 numbers.

Here is an example:  
\_source{  
some\_data:{ related to the document.....},  
dna:{  
entry:{  
color:2397879,  
area:0.4  
},  
entry:{  
color:938893,  
area:0.3  
},  
entry:{  
color:3498438,  
area:0.2  
},  
entry:{  
color:894879,  
area:0.1  
}  
}  
}

Visually, it is a rectangle with the colors each covering specified area  
("1" is the total space, so 2397879 would cover almost a half, etc..)  
Now, I need to make a query which has an number as input. I need to find  
all documents "color" field of which is closest to that number, and "area"  
field is a boost, so if a color covers more space, the document should be  
higher in the search result.

First, I couldn't find the right way to search closest numbers. For  
example, if one document has field value 4 and another document has value 6  
they are equally close to 5, and should be retrieved with the same score. I  
could make some kind of threshold and just run a range filter (from  
x-threshold, to x+threshold) on the "color" field, but this wouldn't be the  
right order.  
Second step is boosting using other field's value. Any help here is also  
appreciated.

I'm kind of stack here, and need help of an ES guru.  
If you have any thoughts, suggestions, please share,  
Any help is greatly appreciated.  
Thank you,  
Eugene

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [March 25, 2013, 10:53pm UTC](https://discuss.elastic.co/t/closest-number-query/11275/2 "2013-03-25T22:53:28Z")

</div>

I think a combination of gradually expanding range filter that have  
suggested with script sort might work. Alone script sort would be very  
slow, but if you will combine it with range filter that would  
significantly reduce the number of results, it might actually  
work: [color\_match.sh · GitHub](https://gist.github.com/imotov/5241590)

On Saturday, March 23, 2013 7:28:35 PM UTC-4, Eugene Strokin wrote:

> Hello,  
> I'm working on interesting problem, and I think it could be solved using  
> ES features.  
> I have millions of documents, which have an multi-field of 2 numbers.
> 
> Here is an example:  
> \_source{  
> some\_data:{ related to the document.....},  
> dna:{  
> entry:{  
> color:2397879,  
> area:0.4  
> },  
> entry:{  
> color:938893,  
> area:0.3  
> },  
> entry:{  
> color:3498438,  
> area:0.2  
> },  
> entry:{  
> color:894879,  
> area:0.1  
> }  
> }  
> }
> 
> Visually, it is a rectangle with the colors each covering specified area  
> ("1" is the total space, so 2397879 would cover almost a half, etc..)  
> Now, I need to make a query which has an number as input. I need to find  
> all documents "color" field of which is closest to that number, and "area"  
> field is a boost, so if a color covers more space, the document should be  
> higher in the search result.
> 
> First, I couldn't find the right way to search closest numbers. For  
> example, if one document has field value 4 and another document has value 6  
> they are equally close to 5, and should be retrieved with the same score. I  
> could make some kind of threshold and just run a range filter (from  
> x-threshold, to x+threshold) on the "color" field, but this wouldn't be the  
> right order.  
> Second step is boosting using other field's value. Any help here is also  
> appreciated.
> 
> I'm kind of stack here, and need help of an ES guru.  
> If you have any thoughts, suggestions, please share,  
> Any help is greatly appreciated.  
> Thank you,  
> Eugene

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Eugene\_Strokin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/eugene_strokin/32/1238_2.png) [@Eugene\_Strokin](https://discuss.elastic.co/u/Eugene_Strokin)
#### Post date: [March 26, 2013, 1:44am UTC](https://discuss.elastic.co/t/closest-number-query/11275/3 "2013-03-26T01:44:36Z")

</div>

Your example helped a lot, thank you.  
I've done it in a similar way, but using Custom Score Query, and writing  
the script based on your example.  
Thanks again,  
Eugene

On Monday, March 25, 2013 6:53:28 PM UTC-4, Igor Motov wrote:

> I think a combination of gradually expanding range filter that have  
> suggested with script sort might work. Alone script sort would be very  
> slow, but if you will combine it with range filter that would  
> significantly reduce the number of results, it might actually work:  
> [color\_match.sh · GitHub](https://gist.github.com/imotov/5241590)
> 
> On Saturday, March 23, 2013 7:28:35 PM UTC-4, Eugene Strokin wrote:
> 
> > Hello,  
> > I'm working on interesting problem, and I think it could be solved using  
> > ES features.  
> > I have millions of documents, which have an multi-field of 2 numbers.
> > 
> > Here is an example:  
> > \_source{  
> > some\_data:{ related to the document.....},  
> > dna:{  
> > entry:{  
> > color:2397879,  
> > area:0.4  
> > },  
> > entry:{  
> > color:938893,  
> > area:0.3  
> > },  
> > entry:{  
> > color:3498438,  
> > area:0.2  
> > },  
> > entry:{  
> > color:894879,  
> > area:0.1  
> > }  
> > }  
> > }
> > 
> > Visually, it is a rectangle with the colors each covering specified area  
> > ("1" is the total space, so 2397879 would cover almost a half, etc..)  
> > Now, I need to make a query which has an number as input. I need to find  
> > all documents "color" field of which is closest to that number, and "area"  
> > field is a boost, so if a color covers more space, the document should be  
> > higher in the search result.
> > 
> > First, I couldn't find the right way to search closest numbers. For  
> > example, if one document has field value 4 and another document has value 6  
> > they are equally close to 5, and should be retrieved with the same score. I  
> > could make some kind of threshold and just run a range filter (from  
> > x-threshold, to x+threshold) on the "color" field, but this wouldn't be the  
> > right order.  
> > Second step is boosting using other field's value. Any help here is also  
> > appreciated.
> > 
> > I'm kind of stack here, and need help of an ES guru.  
> > If you have any thoughts, suggestions, please share,  
> > Any help is greatly appreciated.  
> > Thank you,  
> > Eugene

--  
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).  
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, 2:44am UTC](https://discuss.elastic.co/t/closest-number-query/11275/4 "2017-07-06T02:44:28Z")

</div>


