弹性search关键字不匹配

我在弹性searchDB做search操作。我需要的是,如果我给search关键字“反应”,它应该只给结果匹配关键字“反应”,如果关键字是“反应路由器”,结果应该只包含“反应路由器”。 问题是,如果我给search关键字“反应”,结果匹配“反应路由器”关键字也检索。 有没有比match-phrase更好的select,我尝试了正则expression式和通配符,但没有用。 如何解决这个问题

`const searchTags = (tag) => { const answerPush = []; return new Promise((resolve, reject) => { client.search({ index : constants.QUESTIONNAIRE_INDEX, type : constants.QUESTIONS_TYPE, scroll : constants.SCROLL, body : { query : { match_phrase : { tags : tag } } } }, function getMoreUntilDone(err, res) { if (err) { reject(err); return; } else { res.hits.hits.forEach(function(hit) { answerPush.push(hit._source); }); if (res.hits.total > answerPush.length) { client.scroll({ scrollId: res._scroll_id, scroll: constants.SCROLL }, getMoreUntilDone); } else { const answerArray = []; answerPush.map(val => { answerArray.push(val); }); const result = { questions: answerArray }; resolve(result); } }}); }); }; 

`

要通过完全匹配进行search,您必须使用术语查询。

在你的情况下它可能工作的很好,只要记住,Elasticsearch将分析/索引你的string,例如react-redux将被“拆分”为两个关键字,因为“ – ”

您需要在映射中configuration策略: https : //discuss.elastic.co/t/how-to-search-for-terms-containing-hyphen-on–all-field/81335

使用术语查询而不是匹配词组查询

 const searchTags = (tag) => { const answerPush = []; return new Promise((resolve, reject) => { client.search({ index : constants.QUESTIONNAIRE_INDEX, type : constants.QUESTIONS_TYPE, scroll : constants.SCROLL, body : { query : { term : { tags : tag } } } }, function getMoreUntilDone(err, res) { if (err) { reject(err); return; } else { res.hits.hits.forEach(function(hit) { answerPush.push(hit._source); }); if (res.hits.total > answerPush.length) { client.scroll({ scrollId: res._scroll_id, scroll: constants.SCROLL }, getMoreUntilDone); } else { const answerArray = []; answerPush.map(val => { answerArray.push(val); }); const result = { questions: answerArray }; resolve(result); } }}); }); };