最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multi-field search in the elasticsearch-js library - Stack Overflow

programmeradmin2浏览0评论

I am using elasticsearch (managed instance from searchly) with the elasticsearch-js npm client library. I want to search multiple fields in my index from a term. There seems to be loads of documentation on this, e.g.

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

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:

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

I can't have multiple match: fields otherwise tsc plains about strict mode, and if I try something like:

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

then I get an error:

"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?",

I am using elasticsearch (managed instance from searchly) with the elasticsearch-js npm client library. I want to search multiple fields in my index from a term. There seems to be loads of documentation on this, e.g.

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

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:

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

I can't have multiple match: fields otherwise tsc plains about strict mode, and if I try something like:

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

then I get an error:

"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?",

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 3, 2016 at 9:49 George EdwardsGeorge Edwards 9,22921 gold badges85 silver badges171 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Since you want to match same string on multiple fields you need multi match query. Try something like this

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);
});
发布评论

评论列表(0)

  1. 暂无评论