Omarkad
Omarkad

Reputation: 289

Access field document in query elasticsearch

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 ?

Is it feasible using Script Query ? doc here

Upvotes: 1

Views: 84

Answers (1)

Omarkad
Omarkad

Reputation: 289

Yes actually 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"
                        }
                    }
                 }
            ]
        }
    }
}

thanks

Upvotes: 1

Related Questions