# Sorting \_geo\_distance does not work for filter term

**URL:** <https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124>\
**Category:** Elasticsearch\
**Created:** [December 4, 2017, 11:07am UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124 "2017-12-04T11:07:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mate01988](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mate01988](https://discuss.elastic.co/u/mate01988)\
**Post date:** [December 4, 2017, 11:07am UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/1 "2017-12-04T11:07:37Z")

</div>

Hello,  
I have a problem.

Sorting by \_geo\_distance works for:

```auto
[query] => Array(
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [multi_match] => Array
                                        (
                                            [query] => test
                                            [fields] => Array
                                                (
                                                    [0] => title^2
                                                    [1] => tag_suggest^2
                                                    [2] => description
                                                )
                                            [fuzziness] => 2
                                        )
                                )
                            [filter] => Array
                                (
                                    [geo_distance] => Array
                                        (
                                            [distance] => 500km
                                            [location] => Array
                                                (
                                                    [lat] => 52.406374
                                                    [lon] => 16.925168100000064
                                                )
                                        )
                                )
                        )
                )
            [suggest] => Array(
                    [my-suggestion] => Array(
                            [text] => test
                            [term] => Array
                                (
                                    [field] => title
                                )
                        )
                )
            [sort] => Array
                (
                    [_geo_distance] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 52.406374
                                    [lon] => 16.925168100000064
                                )
                            [order] => asc
                            [unit] => km
                            [distance_type] => plane
                        )
                )
        )

```

But sorting by \_geo\_distance not working, when i add to filter "[must][term]".

```auto
[query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [multi_match] => Array
                                        (
                                            [query] => test
                                            [fields] => Array
                                                (
                                                    [0] => title^2
                                                    [1] => tag_suggest^2
                                                    [2] => description
                                                )
                                            [fuzziness] => 2
                                        )
                                )
                            [filter] => Array
                                (
                                    [geo_distance] => Array
                                        (
                                            [distance] => 500km
                                            [location] => Array
                                                (
                                                    [lat] => 52.406374
                                                    [lon] => 16.925168100000064
                                                )
                                        )
                                    [must] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [is_active] => 1
                                                )
                                        )
                                )
                        )
                )
            [suggest] => Array
                (
                    [my-suggestion] => Array
                        (
                            [text] => test
                            [term] => Array
                                (
                                    [field] => title
                                )
                        )
                )
            [sort] => Array
                (
                    [_geo_distance] => Array
                        (
                            [location] => Array
                                (
                                    [lat] => 52.406374
                                    [lon] => 16.925168100000064
                                )
                            [order] => asc
                            [unit] => km
                            [distance_type] => plane
                        )
                )
        )

```

Please help.

---

<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 4, 2017, 3:32pm UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/2 "2017-12-04T15:32:56Z")

</div>

What happens instead? How does your data look like?

---

<div class="post-metadata">

**Author:** ![mate01988](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mate01988](https://discuss.elastic.co/u/mate01988)\
**Post date:** [December 4, 2017, 3:56pm UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/3 "2017-12-04T15:56:44Z")

</div>

Returned data is ok, but the results are not sorted by \_geo\_distance.

Without filter term sorted is ok, but when i added

```auto
 [must] => Array
                                        (
                                            [term] => Array
                                                (
                                                    [is_active] => 1
                                                )
                                        )

```

Sorted by "\_geo\_distance" not working.

Results:

```auto
Array
(
    [took] => 33
    [timed_out] => 
    [_shards] => Array
        (
            [total] => 5
            [successful] => 5
            [failed] => 0
        )

    [hits] => Array
        (
            [total] => 5
            [max_score] => 1
            [hits] => Array
                (
                    [0] => Array
                        (
                            [_index] => lupago_dev
                            [_type] => ads
                            [_id] => AWATTiyi9dNjBYSKcgma
                            [_score] => 1
                            [_source] => Array
                                (
                                    [is_active] => 1
                                    [category_id] => 2
                                    [city] => Konin, Polska
                                    [created] => 1512152378
                                    [price] => 4523432
                                    [title] => Test
                                    [url] => test
                                )

                        )

                    [1] => Array
                        (
                            [_index] => lupago_dev
                            [_type] => ads
                            [_id] => AV_zZCrb9dNjBYSKcgmO
                            [_score] => 0.5945348
                            [_source] => Array
                                (
                                    [is_active] => 1
                                    [category_id] => 2
                                    [city] => Warszawa, Polska
                                    [created] => 1511616948
                                    [price] => 2324534
                                    [title] => Test 3
                                    [url] => test_3
                                )

                        )

                    [2] => Array
                        (
                            [_index] => lupago_dev
                            [_type] => ads
                            [_id] => AWATG0mc9dNjBYSKcgmW
                            [_score] => 0.5945348
                            [_source] => Array
                                (
                                    [is_active] => 1
                                    [category_id] => 2
                                    [city] => Warszawa, Polska
                                    [created] => 1512149043
                                    [price] => 2
                                    [title] => Ogłoszenie test 2
                                    [url] => ogloszenie_test_2
                                )

                        )

                    [3] => Array
                        (
                            [_index] => lupago_dev
                            [_type] => ads
                            [_id] => AV_PoGQK9dNjBYSKcgmA
                            [_score] => 0.5945348
                            [_source] => Array
                                (
                                    [is_active] => 1
                                    [category_id] => 2
                                    [city] => Poznań, Polska
                                    [created] => 1511016915
                                    [price] => 1
                                    [title] => Test Weekend
                                    [url] => test_weekend
                                )

                        )

                    [4] => Array
                        (
                            [_index] => lupago_dev
                            [_type] => ads
                            [_id] => AWAdrMNY9dNjBYSKcgmc
                            [_score] => 0.5945348
                            [_source] => Array
                                (
                                    [is_active] => 1
                                    [category_id] => 2
                                    [city] => Warszawa, Polska
                                    [created] => 1512326349
                                    [price] => 3424
                                    [title] => Test przkeństwa
                                    [url] => test_przkenstwa
                                )

                        )

                )

        )

)

```

---

<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 4, 2017, 4:36pm UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/4 "2017-12-04T16:36:14Z")

</div>

At first glance I think you cannot use the "must" inside the "filter" directly. There is no "must" query, this should always be part of a surrounding "bool" query. I think the "term" query should work directly. Can you try if this solves your problem?

---

<div class="post-metadata">

**Author:** ![mate01988](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mate01988](https://discuss.elastic.co/u/mate01988)\
**Post date:** [December 4, 2017, 5:48pm UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/5 "2017-12-04T17:48:24Z")

</div>

I changed the structure and it works! Thank you for the tip.

```auto
 ["query"]=>
    array(1) {
      ["bool"]=>
      array(3) {
        ["must"]=>
        array(1) {
          ["multi_match"]=>
          array(3) {
            ["query"]=>
            string(11) "ogłoszenie"
            ["fields"]=>
            array(3) {
              [0]=>
              string(7) "title^2"
              [1]=>
              string(13) "tag_suggest^2"
              [2]=>
              string(11) "description"
            }
            ["fuzziness"]=>
            int(2)
          }
        }
        ["must_not"]=>
        array(1) {
          ["term"]=>
          array(1) {
            ["is_active"]=>
            bool(false)
          }
        }
        ["filter"]=>
        array(1) {
          ["geo_distance"]=>
          array(2) {
            ["distance"]=>
            string(5) "500km"
            ["location"]=>
            array(2) {
              ["lat"]=>
              string(9) "52.406374"
              ["lon"]=>
              string(18) "16.925168100000064"
            }
          }
        }
      }
    }
    ["suggest"]=>
    array(1) {
      ["my-suggestion"]=>
      array(2) {
        ["text"]=>
        string(11) "ogłoszenie"
        ["term"]=>
        array(1) {
          ["field"]=>
          string(5) "title"
        }
      }
    }
    ["sort"]=>
    array(1) {
      ["_geo_distance"]=>
      array(4) {
        ["location"]=>
        array(2) {
          ["lat"]=>
          string(9) "52.406374"
          ["lon"]=>
          string(18) "16.925168100000064"
        }
        ["order"]=>
        string(3) "asc"
        ["unit"]=>
        string(2) "km"
        ["distance_type"]=>
        string(5) "plane"
      }
    }

```

---

<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:** [January 1, 2018, 5:48pm UTC](https://discuss.elastic.co/t/sorting-geo-distance-does-not-work-for-filter-term/110124/6 "2018-01-01T17:48:30Z")

</div>

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