elasticsearch-js库中的多字段搜索

Geo*_*rds 0 javascript search node.js elasticsearch

我将elasticsearch(来自searchly 的托管实例)与elasticsearch -js npm 客户端库一起使用。我想从一个术语中搜索索引中的多个字段。似乎有很多关于此的文档,例如

GET /_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "title":  "War and Peace" }},
        { "match": { "author": "Leo Tolstoy"   }}
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以为作者和标题设置相同的值。但是,这是一个获取请求,其结构与 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);
});
Run Code Online (Sandbox Code Playgroud)

我不能有多个match:字段,否则 tsc 会抱怨严格模式,如果我尝试以下操作:

    query: {
      match: {
        name: 'toFind',
        description: 'toFind'
      }
    }
Run Code Online (Sandbox Code Playgroud)

然后我收到一个错误:

"type": "query_parsing_exception",

"reason": "[匹配] 查询以简化形式解析,带有直接字段名称,但包含的选项不仅仅是字段名称,可能使用其 \u0027options\u0027 形式,带有 \u0027query\u0027 元素?”,

Chi*_*h25 6

由于您想在多个字段上匹配相同的字符串,因此您需要多匹配查询。尝试这样的事情

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);
});
Run Code Online (Sandbox Code Playgroud)