Multi-field search in the elasticsearch-js library
<p>I am using elasticsearch (managed instance from <a href="https://www.searchly.com" rel="nofollow noreferrer">searchly</a>) with the <a href="https://github.com/elastic/elasticsearch-js/" rel="nofollow noreferrer">elasticsearch-js</a> npm client library. I want to search multiple fields in my index from a term. There seems to be loads of <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/multi-query-strings.html" rel="nofollow noreferrer">documentation</a> on this, e.g.</p>
<pre><code>GET /_search
{
  &quot;query&quot;: {
    &quot;bool&quot;: {
      &quot;should&quot;: [
        { &quot;match&quot;: { &quot;title&quot;:  &quot;War and Peace&quot; }},
        { &quot;match&quot;: { &quot;author&quot;: &quot;Leo Tolstoy&quot;   }}
      ]
    }
  }
}
</code></pre>
<p>where I could just set the same value for author and title. However, this is a get request and is structured differently to the nodejs library, where I am doing this:</p>
<pre><code>this._client.search({
  index: 'sample',
  body: {
    query: {
      match: {
        name: 'toFind'
      }
    }
  }
}).then(function (resp) {
  var hits = resp.hits.hits;
}, function (err) {
  console.trace(err.message);
});
</code></pre>
<p>I can't have multiple <code>match:</code> fields otherwise tsc complains about strict mode, and if I try something like:</p>
<pre><code>    query: {
      match: {
        name: 'toFind',
        description: 'toFind'
      }
    }
</code></pre>
<p>then I get an error:</p>
<blockquote>
<p>&quot;type&quot;: &quot;query_parsing_exception&quot;,</p>
<p>&quot;reason&quot;: &quot;[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?&quot;,</p>
</blockquote>