Sorting _geo_distance does not work for filter term

Hello,
I have a problem.

Sorting by _geo_distance works for:

[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]".

[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.

What happens instead? How does your data look like?

Returned data is ok, but the results are not sorted by _geo_distance.

Without filter term sorted is ok, but when i added

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

Sorted by "_geo_distance" not working.

Results:

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
                                )

                        )

                )

        )

)

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?

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

 ["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"
      }
    }

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