GeoDistance

42 views
Skip to first unread message

Minn

unread,
Nov 8, 2016, 7:22:06 AM11/8/16
to Elastica - Elasticsearch PHP Client
Hello,
I am looking to find the difference in km of every geolocation nearby.
I have this code but I am not able to merge a FilterBool and Script in order to get the distance.


 $index = $this->elasticaClient->getIndex("places");
 $es_bool = new Elastica\Filter\Bool(); 
 
 $range = new \Elastica\Filter\Range('sqm', array('gte' => 10, 'lte' => 100));
 $filterGeo = new Elastica\Filter\GeoDistance('location', array('lat' => 42.33, 'lon' => -3.26), "2km");
 $es_bool->addMust(array($range, $filterGeo));
 
// I am not able to set the script
 $script = new Elastica\Script("doc[\u0027location\u0027] ? doc[\u0027location\u0027].distanceInKm(lat,lon) : 0"); 
 $elasticaResultSet = $index->search($es_bool, 50);

any ideas? thanks

ruflin

unread,
Nov 15, 2016, 10:23:53 AM11/15/16
to Elastica - Elasticsearch PHP Client
Sorry for the late answer. Can you share the pure script code you would like to use?
Message has been deleted

Minn

unread,
Nov 15, 2016, 1:09:25 PM11/15/16
to Elastica - Elasticsearch PHP Client
I am not sure if this is the best way to get the distance but .... I had to repeat the geoDistance condition because I am not able to write a script inside a query (error: It can not recognize script_fields ) and If I remove geo_distance it ignores the two matches and range.
Anyways ... So, I was looking to represent in elastica php something similar to have a match , range, geo_distance and the most important thing: Get/return the distance value (doc[\u0027location\u0027].
distanceInKm(lat,lon))  ---> my headache
Thanks
curl -XGET 'http://127.0.0.1:9200/information/_search'  -d '
{
    "fields" : [ "_source" ],
    "query":
        {"bool":
            {"must": [
                { "match": { "tipos_c": "Rest"}} ,
                { "match": { "font":  "yui"}}  ,
                { "range" : {  "sqm" : { "from" : "10", "to" :"50"   }   } }
                ]
            ,"filter" : {"geo_distance" :
                    {  "distance" : "2km",
                        "location" : {  "lat" : 41.560689200000000,
                                "lon" : 1.999436000000059
                                 }
                        }        
                    }
             }
           },

           "script_fields" : {
                  "location" : {
                     "params" : {
                        "lat" : 41.560689200000000,
                        "lon" : 1.999436000000059
                     },
                 "script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
                  }
           } 
         }'


In my first post I could represent the range and the geoDistance but It is not possible for me to return the distance between the two points compared.

Minn

unread,
Nov 15, 2016, 4:47:21 PM11/15/16
to Elastica - Elasticsearch PHP Client
I think I found the solution. I would like to sort ASC . Any ideas?

        $index = $this->elasticaClient->getIndex(INDEX);
        $es_bool = new Elastica\Query\BoolQuery();
        $es  = new Elastica\Query();

        $matchtc = new \Elastica\Query\Match();
        $matchtc->setFieldQuery(TYPE_C, "Res");

        $filter = new Elastica\Filter\GeoDistance('location', array('lat' => 37.118221, 'lon' => -3.585934), '1km');

        $range  = new Elastica\Filter\Range();
        $range->addField("superficie", array('from' => 10, 'to' => 60));

        $es_bool->addMust(array($matchtc , $range, $filter) );

       $score = new Elastica\Query\FunctionScore( );
       $score->setQuery($es_bool);
       $score->setBoostMode('replace');
       $script = new Elastica\Script('doc[\u0027location\u0027].distanceInKm(lat,lon)',array('lat' => 37.118221,'lon' => -3.585934   ),"groovy"); 
       $score->addScriptScoreFunction($script);
       $es->setQuery($score);   

       $elasticaResultSet = $index->search($es);

ruflin

unread,
Nov 21, 2016, 8:40:44 AM11/21/16
to Elastica - Elasticsearch PHP Client

Minn

unread,
Dec 12, 2016, 6:16:20 PM12/12/16
to Elastica - Elasticsearch PHP Client
Yes!! thanks
Reply all
Reply to author
Forward
0 new messages