1

Let's say that I've indexed an object that looks like:

{
  firstName: "Ben"
  lastName: "McCann"
  urls: [{ canonical: "http://www.benmccann.com" }]
  emails: [{ canonical: "[email protected]" }]
}

How do I then create a search for "Ben" or "McCann" or "[email protected]"?

SearchRequest request = new SearchRequest(INDEX)
    .source(new SearchSourceBuilder().query(QueryBuilders.boolQuery()
        .should(QueryBuilders.matchQuery("firstName", "Ben"))
        .should(QueryBuilders.matchQuery("lastName", "McCann"))
        .should(QueryBuilders.nestedQuery("emails", QueryBuilders.matchQuery("emails.canonical", "[email protected]")))));
0

1 Answer 1

4

You need to use a nested query only if you configured the emails field as nested type in your mapping. I don't think it makes sense in your case since you have a single subfield under emails, called canonicals. You can just use again a match query and the dot notation, refering to the field as emails.canonical.

If you use a bool query don't forget to set the minimum should match, which tells you how many should clauses should match at least out of the ones provided in the query. In your case 1 should be fine.

SearchRequest request = Requests.searchRequest(INDEX)
    .source(SearchSourceBuilder.searchSource().query(QueryBuilders.boolQuery()
        .should(QueryBuilders.matchQuery("firstName", "Ben"))
        .should(QueryBuilders.matchQuery("lastName", "McCann"))
        .should(QueryBuilders.matchQuery("emails.canonical", "[email protected]"))
        .minimumShouldMatch(1)));

On the other hand, if you did configure the emails field as nested in your mapping, then you need to use a nested query. I don't see anything wrong in what you are doing, you're just missing the minimum should match parameter.

Sign up to request clarification or add additional context in comments.

Comments

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.