Search in Elastic pass a list and return all matchs

Asked

Viewed 33 times

1

I need to perform a search in Elastic search, but I’m having difficulties

BoolQueryBuilder query = boolQuery()
            .must(matchQuery(CLIENT_ID, clientId));

fieldValues
      .forEach(fieldValue -> query.must(boolQuery().should(matchQuery(FIELD_NAME, fieldValue))));

fieldValues is a list of strings. And I would like to have the match result of all the values I have in the list with the FIELD_NAME field. But the way I implemented it, it results 0 always, because from what I understand, he tries to match with the first value AND all the rest. I’d like an OR operation, not AND.

  • I never used this java api for Elasticsearch, but apparently the problem is the use of MUST. I believe you do not need the Must written inside the foreach, try calling the query directly.should(matchQuery(FIELD_NAME, fieldValue)).

  • Hello man. Man, thanks for the answer, but as a result everything comes. I imagine it is by the query from above. It seems that everything that belongs to the customer’s id is taken. When it comes time to give the match, he does not disregard those who do not give match. If it helps, I can show the resulting query.

1 answer

0

To search in a list you can use filters and must use "Terms" by passing the field and the list of values.

GET /empresas/_search
{
  "query": {
    "bool": {
      "filter": [
        {"terms": {"CLIENT_ID": ["3085","2062","2002","2003"]}}
      ]
    }
  }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.