我有以下功能进行弹性搜索:
$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);
});
}
我是弹性查询的新手,我在范围字段中收到此错误:
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;....
可能我写了范围字段错误,但我发现如何在此文档中使用它:www.elasticsearch.org/...
我想知道我在查询中做错了什么。
答案 0 :(得分:1)
EDITED 我只需要在范围中添加另一个过滤器并且它正在工作,但如果将范围作为必须参数,则性能会更好
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" }}]
}