Joabe da Luz
Joabe da Luz

Reputation: 1020

Error in my elasticquery - Range returns bad request

I have the following function that does an elastic search:

$scope.getLocationHistory = function(device, lastTime){

    console.log("Device location searching");

    query = {
        "query": {
            "bool": {          
                "must": [
                    { "term": {"deviceId":device} },
                    { "match": {"eventType":"Connected"} } 
                ],
                "must_not":[
                    {"query_string": {
                            "query": "Pong",
                            "fields": ["data.message"]
                        }
                    },
                ] 
            }                  
        },
        "range" : {
            "timestamp" : {
                "gt" : "now-1h"
            }
        },
        "filter" : {
            "exists" : { "field" : "data.location" }
        },
        "sort": [{ "timestamp": { "order": "desc" }}]
    }

    $http.post(databaseLocation+"/canary/_search", query).success(function(data, status, headers, config){
        $scope.getMapHistory(data.hits.hits); //Auxiliary function
    }).error(function(data, status, headers, config){
        console.log(data);
    }); 

}

I'm new on the elastic query and I'm getting this error with the range field:

POST http://... 400 (Bad Request) Object {error: "SearchPhaseExecutionException[Failed to execute ph…arse Failure [No parser for element [range]]]; }]", status: 400}error: "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;....

Probably I wrote the range field wrong, but I found how to use it on this doc: www.elasticsearch.org/...

I would like to know what I'm doing wrong on the query.

Upvotes: 1

Views: 547

Answers (1)

Joabe da Luz
Joabe da Luz

Reputation: 1020

EDITED I just had to add another filter to the range and it's working, but the performance is better if you put the range as a must param

    query = {
        "query": {
            "bool": {          
                "must": [
                    { "range" : { "timestamp" : { "gt" : timeRange }}},
                    { "term": {"deviceId":device} },
                    { "match": {"eventType":"Connected"} } 
                ],
                "must_not":[{
                        "query_string": {
                            "query": "Pong",
                            "fields": ["data.message"]
                        }
                    },
                ] 
            },

        },
        "filter": { 
            "exists" : { "field":"data.location" }
        },
        "sort": [{ "timestamp": { "order": "desc" }}]
    }

Upvotes: 1

Related Questions