Stoyan Dekov Stoyan Dekov - 3 months ago 9
AngularJS Question

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.

Val Val
Answer

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) {
  // ...
});
Comments