2013-04-19 10 views
16

Diciamo che abbiamo un sacco di documenti in un indice ElasticSearch. Ogni documenti ha più sedi in un array, come questo:ElasticSearch filtro di distanza geografico con più posizioni nell'array - possibile?

{ 
    "name": "foobar", 
    "locations": [ 
    { 
     "lat": 40.708519, 
     "lon": -74.003212 
    }, 
    { 
     "lat": 39.752609, 
     "lon": -104.998100 
    }, 
    { 
     "lat": 51.506321, 
     "lon": -0.127140 
    } 
    ] 
} 

Secondo il ElasticSearch reference guide

il filtro geo_distance può lavorare con più sedi/centri per documento. Una volta che una singola posizione/punto corrisponde al filtro, il documento verrà incluso nel filtro.

Quindi, è possibile creare un filtro di distanza geo che controlla tutte le posizioni nell'array?

Questo non sembra funzionare, purtroppo:

{ 
    "filter": { 
    "geo_distance": { 
     "distance": "100 km", 
     "locations": "40, -105" 
    } 
    } 
} 

tiri "QueryParsingException[[myIndex] failed to find geo_point field [locations]" poiché locations non è un singolo geo_point, ma una serie di geo_point s.

risposta

23

Hai specificato un geo_point mapping per il tuo documento?

curl -XDELETE 'http://localhost:9200/twitter/' 
curl -XPUT 'http://localhost:9200/twitter/' 

curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d ' 
{ 
    "tweet" : { 
     "properties" : { 
      "locations" : {"type" : "geo_point"} 
     } 
    } 
}' 

curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d ' 
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elastic Search, so far so good?", 
    "locations" : [{ 
     "lat" : 50.00, 
     "lon" : 10.00 
    }, 
    { 
     "lat" : 40.00, 
     "lon" : 9.00 
    }] 
}' 

curl -XPUT 'http://localhost:9200/twitter/tweet/2' -d ' 
{ 
    "user": "kimchy", 
    "postDate": "2009-11-15T13:12:00", 
    "message": "Trying out Elastic Search, so far so good?", 
    "locations" : [{ 
     "lat" : 30.00, 
     "lon" : 8.00 
    }, 
    { 
     "lat" : 20.00, 
     "lon" : 7.00 
    }] 
}' 

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{ 
    "query": { 
     "filtered" : { 
      "query" : { 
       "match_all" : {} 
      }, 
      "filter" : { 
       "geo_distance" : { 
        "distance" : "20km", 
        "tweet.locations" : { 
         "lat" : 40.00, 
         "lon" : 9.00 
        } 
       } 
      } 
     } 
    } 
}' 
+0

Sì, c'è la mappatura geo_point per le posizioni. – Max

+0

Quindi dovrebbe funzionare, vedere il mio esempio. Quale versione ES usi? L'ho provato su 0.20.6. – Thorsten

+1

In realtà, * funziona * ora dopo aver eseguito il nuking dell'indice originale. Specificando una mappatura geo_point e quindi inserendo semplicemente una serie di posizioni, il mio mapping precedente era troppo complesso. Molte grazie! – Max

3

Per elasticsearch versione 5.1, per lo stesso indice superiore, interrogazione andrà in questo modo,

curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d ' 
{ 
    "query": { 
     "bool": { 
      "must": { 
       "match_all": {} 
      }, 
      "filter": { 
       "geo_distance": { 
        "distance": "200km", 
        "locations": { 
         "lat": 40.00, 
         "lon": 9.00 
        } 
       } 
      } 
     } 
    } 
}' 
Problemi correlati