2015-04-12 66 views
0

我想在Javascript中構建下面的搜索調用沒有運氣,下面的工作正常,所以我知道我的索引在ES中正確設置。彈性搜索解析器錯誤

GET /teachersx9/teacher/_search 
    { 
    "query": { 
    "bool": { 
     "must": [ 
      { "match_all": {}}, 
      { 
      "nested": { 
       "path": "langs", 
       "score_mode": "max", 
       "query": { 
        "bool": { 
         "must": [ 
         { "match": { "languagename": "Afrikaans"}} 
        ] 
     }}}} 
    ] 
}}} 

然而,嘗試使用創建我的服務器上查詢時:

app.get('/search', function(req, res) { 

var term = req.query.term;//req.param('term'); 
var urlPath = 'https:.../teachersx9/teacher/_search'; 

//var obj = {}; 
var query = {}; 
query.bool = {}; 

var match_all = {}; 
var nested = {path:"langs", score_mode:"max"}; 
nested.query = {}; 
nested.query.bool = {}; 
nested.query.bool.must = [{match: {languagename:term}}]; 
query.bool.must = [{match_all:match_all}, {nested:nested}]; 

console.log(query); 

request.post({ 
    url: urlPath, 
    json: true, 
    body: query 
}, function(error, response, body){ 
    if (!error && response.statusCode == 200) { 

     console.log(body); 
     res.send(body); 
    } else { 
     console.log(body); 
     res.send(response); 
    } 
    }); 

}); 

錯誤我從ES作爲響應返回的一部分:

"statusCode": 400, 
"body": { 
"error": "SearchPhaseExecutionException[Failed to execute phase [query_fetch], all shards failed; shardFailures {[FMwFQZmkIAfOE7X48Q][teachersx9][0]: RemoteTransportException[[Quentin Quire][inet[/172.31.7.165:9300]][indices:data/read/search[phase/query+fetch]]]; nested: SearchParseException[[teachersx9][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{\"bool\":{\"must\":[{\"match_all\":{}},{\"nested\":{\"path\":\"langs\",\"score_mode\":\"max\",\"query\":{\"bool\":{\"must\":[{\"match\":{\"languagename\":\"Italian\"}}]}}}}]}}]]]; nested: SearchParseException[[teachersx9][0]: from[-1],size[-1]: Parse Failure [No parser for element [bool]]]; }]", 
"status": 400 
}, 
+0

請使用代碼標籤正確地格式化您的代碼和錯誤消息 – deW1

回答

0

bool不Elastic query DSL中有效的頂級構造。您需要將其包裝在query中。

var body = { query: query } 

request.post({ 
    url: urlPath, 
    json: true, 
    body: body 
}) 
+0

謝謝。這有助於:) – mrezzz