2016-10-03 87 views
0

我正在使用elasticsearch(受管實例,來自searchly)和elasticsearch-js npm客戶端庫。我想從一個術語中搜索索引中的多個字段。似乎在此有大量的documentation,例如,elasticsearch-js庫中的多字段搜索

GET /_search 
{ 
    "query": { 
    "bool": { 
     "should": [ 
     { "match": { "title": "War and Peace" }}, 
     { "match": { "author": "Leo Tolstoy" }} 
     ] 
    } 
    } 
} 

其中我可以爲作者和標題設置相同的值。然而,這是一個GET請求和結構不同的庫的NodeJS,在那裏我這樣做:

this._client.search({ 
    index: 'sample', 
    body: { 
    query: { 
     match: { 
     name: 'toFind' 
     } 
    } 
    } 
}).then(function (resp) { 
    var hits = resp.hits.hits; 
}, function (err) { 
    console.trace(err.message); 
}); 

我不能有多個match:場否則TSC抱怨嚴格的模式,如果我嘗試像:

query: { 
     match: { 
     name: 'toFind', 
     description: 'toFind' 
     } 
    } 

然後我得到一個錯誤:

"type": "query_parsing_exception",

"reason": "[match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its \u0027options\u0027 form, with \u0027query\u0027 element?",

回答

2

既然你想匹配多個字段相同的字符串,你需要multi match查詢。嘗試像這樣

this._client.search({ 
    index: 'sample', 
    body: { 
    query: { 
     multi_match: { 
     query: 'toFind', 
     fields: ['name','description'] 
     } 
    } 
    } 
}).then(function (resp) { 
    var hits = resp.hits.hits; 
}, function (err) { 
    console.trace(err.message); 
});