# Scoring range queries by range size

**URL:** <https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592>\
**Category:** Elasticsearch\
**Created:** [November 20, 2018, 5:10pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592 "2018-11-20T17:10:43Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [November 20, 2018, 5:10pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/1 "2018-11-20T17:10:43Z")

</div>

I have a field with type ip\_range. When I query for an IP address, I'd like to have the results returned in the order of most precise range to least. Is there a simple way to accomplish this? Perhaps I overlooked something obvious in the docs.

Thanks!

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 23, 2018, 10:21am UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/2 "2018-11-23T10:21:35Z")

</div>

Can you give a concrete example of how your query looks like, some sample documents and the order you want them to score?

---

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [November 26, 2018, 1:49am UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/3 "2018-11-26T01:49:49Z")

</div>

Sure! The query is like this:

```
GET /prefix/_search
{
  "query" : {
    "term": {
      "key_prefix": {
        "value": "127.0.0.1"
      }
    }
  }
}

```

And the results are (approximately) like this:

```
     {
        "_index": "prefix",
        "_type": "doc",
        "_id": "rcFIFGcBaGTwZcx4YKxh",
        "_score": 1,
        "_source": {
          [... a bunch of string fields ...],
          "key_prefix": "127.0.0.0/8",
      }

```

Notice that the CIDR 127.0.0.0/8 contains 127.0.0.1. But 127.0.0.0/20 is more precise (it is a range of size 2^12 vs 2^24), and 127.0.0.1/32 more precisely still (it has size 1). I would like them returned in order of range size.

Does that make sense? Please let me know if you need more information.

---

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [November 27, 2018, 5:13pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/4 "2018-11-27T17:13:06Z")

</div>

Any thoughts?

---

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [November 29, 2018, 5:55pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/5 "2018-11-29T17:55:02Z")

</div>

Ping 🙂

---

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [December 3, 2018, 4:02pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/6 "2018-12-03T16:02:22Z")

</div>

@cbuescher

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [December 3, 2018, 9:27pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/7 "2018-12-03T21:27:33Z")

</div>

Hi,

unfortunately both the "term" and also the "range" query on "ip\_range" fields seem to do only constant scoring (also known as filtering), so in order to sort on the precision of the range I'd probably store the cidr suffix alongside the range in a separate field and sort by that. Would that work for you use case?

---

<div class="post-metadata">

**Author:** ![monk](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@monk](https://discuss.elastic.co/u/monk)\
**Post date:** [December 3, 2018, 10:13pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/8 "2018-12-03T22:13:24Z")

</div>

Hey that's a neat and simple solution! I think it ought to work. Thank you for looking into this for me!

---

<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:** [December 31, 2018, 10:24pm UTC](https://discuss.elastic.co/t/scoring-range-queries-by-range-size/157592/9 "2018-12-31T22:24:13Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
