0

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.

0

1 Answer 1

1

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) {
  // ...
});
Sign up to request clarification or add additional context in comments.

4 Comments

I am searching on non-analysed fields (IDs) and this method gives back 0 hits.
Updated my answer
Thanks! Is there a way to combine terms with match (analysed and non-analysed search)? Something like title: ['title1', 'title2', 'title3'] AND author: 'myAuthor'?
Yes, though, this would better fit in a different question so as not to mix different concepts together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.