xaberue
xaberue

Reputation: 336

Query one single value with one of a collection in NEST

Inside an ElasticSearch instance, I was trying to search elements by matching one field with multiple values. ie:

public class User
{
  public int Id { get; set; }
  public string Name { get; set; }
  public string Surname { get; set;}
}

If I have multiple users and I want to filter by the ones who's name is John or Mike, until version 6.x, this implementation was working:

    private QueryContainer ApplyNameFilter(QueryContainerDescriptor<User> q, SearchParameters searchParameters)
    {
        return (searchParameters.Names != null && searchParameters.Names .Any()) ?
             q.Terms(z => z.Field(f => f.Name).Terms(searchParameters.Names))
             :
             q;
    }

Notice that inside SearchParameters, Names is a IEnumerable

Once I updated ElasticSearch instance to 7.x, also NEST library to 7.3.1, this filter is not working anymore.

Having a look to the Search NEST documentation I am not fully sure of what I have to refactor there in order to make it work again.

Could anyone help me?

Thanks in advance

Upvotes: 0

Views: 284

Answers (1)

jaspreet chahal
jaspreet chahal

Reputation: 9099

Term field is used to match not_analyzed text(text not broken up in tokens). Check the mapping of field "Name" it will be having a subfield keyword with type:"keyword" or you will need to create one in mapping

you can try below query q.Terms(z => z.Field(f => f.Name.Suffix("keyword")).Terms(searchParameters.Names))

Upvotes: 1

Related Questions