0
votes

I'm trying to sort by geo distance an index with nested geo point mapping.

Here is my simplified mapping :

'organizations' => [
                '_doc' => [
                    'properties' => [
                        'locations' => [
                            'type' => 'nested',
                            'properties' => [
                                'point' => [
                                    'type' => 'geo_point',
                                    'ignore_malformed' => true
                                ]
                            ]
                        ]
                    ]
                ]
            ]

Here, each organizations can have several locations point.

Documents :

PUT /organizations , 
[
        {
            name: "A",
            locations: [
                {
                    point: {
                        lat: 1.5,
                        lon: 2
                    }
                },
                {
                    point: {
                        lat: 1,
                        lon: 0
                    }
                }
            ]
        },
        {
            name: "B",
            locations: [
                {
                    point: {
                        lat: 4.2,
                        lon: 3
                    }
                }
            ]
        },
        {
            name: "C",
            locations: [
                {
                    point: {
                        lat: 0.4,
                        lon: 1
                    }
                }
            ]
        }
    ];

Here is my query (PHP array):

[
  "query" =>  [
    "query_string" => [
      "query" => "*"
    ]
  ]
  "sort" =>  [
    0 =>  [
      "_geo_distance" => [
        "locations.point" => "1, 0.1"
        "order" => "asc"
        "unit" => "km"
        "nested_path" => "locations"
      ]
    ]
  ]
]

expected result :

1 => name : A
2 => name : C
3 => bame : B

I'd like to get resources ordered by geo_point (check each location, and then check if a location is close to the given lat / lon )

1
can you add some sample documents and expected search result ?ESCoder
What results do you get vs what you expect?Val
I added document exemple. I got not sorted result at all...Toto NaBendo
@TotoNaBendo on what basis you want the result to be ordered as A,C,B ? If you want in asc order it should be C,A,B ?ESCoder
@Bhavya oh my god my mistake, I updated the sort filterToto NaBendo

1 Answers

1
votes

You can meet your goal using function_score.

For example with the query like:

note: code is in js;

"_geo_distance": {
  "place.location.geo.coordinates": [
    this._geo.lon,
    this._geo.lat
   ],
  "order": "asc",
  "unit": "km",
  "distance_type": "plane"
}

Then in function_score add script_score

function_score["script_score"] = {
"script": {
        "source": `1000/doc['place.location.geo.coordinates'].planeDistance(${this._geo.lat},${this._geo.lon}) `
}

The original function_score is like:

'function_score': {
  "score_mode": "multiply",
  'query': {
    "dis_max": {
       "tie_breaker": 0.7,
       "boost": 1.9,
       "queries": this._query
    },
  },
  'boost': 0.06,
  'boost_mode': 'sum',
 }