Access field document in an elasticsearch query


(Oussama Markad) #1

For example suppose we have this mapping :

{
    "location":{
        "dynamic":"false",
        "properties":{
            "locality":{"type":"text"},
            "country":{"type":"text"},
            "postalCode":{"type":"text"},
            "coordinates":{"type":"geo_point"},
            "radius":{"type":"double"}
        }
    }
}

And this is my query :

GET index_name/location/_search
{
    "query": {
        "bool": {
            "filter": {
                "geo_distance": {
                    "coordinates": [
                        2.352222, 
                        48.999
                    ],
                    "distance": $radius <--- *(here I want to access the
                                               value of radius in document)*
                 }
            }
        }
    }
}

Is there a mean to access a field document value in an Elasticsearch query ?


(Makoto Nozawa) #2

Hi,
How about using Geo-Shape Datatype datatype (cirle) and GeoShape Query (point)?


(Oussama Markad) #3

Hi,

Eventhough I will still need to access the value in the field 'radius'.

GET index_name/location/_search
{
    "query": {
        "geo_shape": {
            "location": { 
                "shape": { 
                    "type":   "circle", 
                    "radius": $radius, <-- (here I want to get the radius stored in each document while 
                                                           searching)
                    "coordinates": [ 
                        2.352222, 
                        48.999
                    ]
                }
            }
        }
    }
}

(Oussama Markad) #4

the Script Query does the trick :

GET index_name/location/_search
{
    "query": {
         "bool": {
             "must": [
                 {
                     "script": {
                         "script": {
                            "inline": "doc['coordinates'].planeDistance(48.856614 , 2.37959999) <= doc['radius'].value",
                            "lang": "painless"
                         }
                     }
                  }
             ]
         }
     }
 }

(system) #5

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