SD.
SD.

Reputation: 9549

Elasticsearch javascript search field in array

This comes directly from the elasticsearch documentation.

client.search({
  index: 'myindex',
  body: {
    query: {
      match: {
        title: 'test'
      }
    }
  }
}, function (error, response) {
  // ...
});

What I am trying to achieve is the same but search for multiple titles.

Sort of the equivallent of if title in ['title1', 'title2', 'title3']

However title: ['title1', 'title2', 'title3'] gives an error as a query string.

There was another suggestion to use filter but it seems to not have any effect. Any suggestion would be greatly appreciated.

Upvotes: 0

Views: 1174

Answers (1)

Val
Val

Reputation: 217254

The correct way is to append all the title values into your query string as it will be analyzed and thus the title field will be matched against each token.

client.search({
  index: 'myindex',
  body: {
    query: {
      match: {
        title: 'title1 title2 title3'
      }
    }
  }
}, function (error, response) {
  // ...
});

UPDATE

If you search for not-analyzed values, you should prefer a terms query:

client.search({
  index: 'myindex',
  body: {
    query: {
      terms: {
        title: ['title1', 'title2', 'title3']
      }
    }
  }
}, function (error, response) {
  // ...
});

Upvotes: 1

Related Questions