Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEST Conditional filter query with multiple terms

I would like to do a ElasticSearch query like this:

{
    "query" :
    {
        "bool" :
        {
            "filter" : [
                {
                    "terms" :
                    {
                        "name" : ["name1", "name2"]
                    }
                },
                {
                    "terms" :
                    {
                        "color" : ["orange", "red"]
                    }
                }
            ]
        }
    }
}

I've tried to implement it in NEST like this:

_elasticClient
    .SearchAsync<MyDocument>(s =>
        s.Index("myindex")
            .Query(q => q
                .Bool(bq => bq
                    .Filter(fq =>
                    {
                        QueryContainer query = null;

                        if (nameList.Any()) {
                            query &= fq.Terms(t => t.Field(f => f.Name).Terms(nameList));
                        }

                        if (colorList.Any()) {
                            query &= fq.Terms(t => t.Field(f => f.Color).Terms(colorList));
                        }

                        return query;
                    })
                )
            )
    );

But that gives me a query like this where the filters are wrapped inside a bool must:

{
    "query" :
    {
        "bool" :
        {
            "filter" : [
                {
                    "bool" :
                    {
                        "must" : [
                            {
                                "terms" :
                                {
                                    "name" : ["name1", "name2"]
                                }
                            },
                            {
                                "terms" :
                                {
                                    "color" : ["orange", "red"]
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}

How should I change my NEST code to give me the right query? Do have have to add my terms to something other then a QueryContainer?

like image 945
dhrm Avatar asked Jun 08 '16 09:06

dhrm


2 Answers

You can create a list of filters before you make a query if you want to check conditional filters as shown below:

var nameList = new[] {"a", "b"};
var colorList = new[] {1, 2};

var filters = new List<Func<QueryContainerDescriptor<MyDocument>, QueryContainer>>();
if (nameList.Any())
{
     filters.Add(fq=> fq.Terms(t => t.Field(f => f.Name).Terms(nameList)));
}

if (colorList.Any())
{
    filters.Add(fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList)));
}

ISearchResponse<Property> searchResponse =
     elasticClient.Search<MyDocument>(x => x.Query(q => q
     .Bool(bq => bq.Filter(filters))));

If you don't need to check any condition before making filter query then you can have something like that:

ISearchResponse<MyDocument> searchResponse =
elasticClient.Search<MyDocument>(x => x.Query(q => q
.Bool(bq => bq
.Filter(
        fq => fq.Terms(t => t.Field(f => f.Name).Terms(nameList)),
        fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList))
        ))));
like image 94
Adam Łepkowski Avatar answered Oct 18 '22 23:10

Adam Łepkowski


The Filter method of a bool query takes a params Func<QueryContainerDescriptor<T>, QueryContainer>[] so that you can pass it multiple expressions to represent multiple filters

var nameList = new string[] { "name1", "name2" };
var colorList = new string[] { "orange", "red" };

client.SearchAsync<MyDocument>(s => s
        .Index("myindex")
        .Query(q => q
            .Bool(bq => bq
                .Filter(
                    fq => fq.Terms(t => t.Field(f => f.Name).Terms(nameList)),
                    fq => fq.Terms(t => t.Field(f => f.Color).Terms(colorList))
                )
            )
        )
);

which results in

{
  "query": {
    "bool": {
      "filter": [
        {
          "terms": {
            "name": [
              "name1",
              "name2"
            ]
          }
        },
        {
          "terms": {
            "color": [
              "orange",
              "red"
            ]
          }
        }
      ]
    }
  }
}

NEST also has the concept of conditionless queries, that is, if a query is determined to be conditionless, then it will not be serialized as part of the request.

What does it mean to be conditionless? Well, that depends on the query; for example, in the case of a terms query it is deemed to be conditionless if any of the following are true

  • the field doesn't have a value
  • the term values list is null
  • the terms value is an empty collection
  • the terms values list has values but they are all null or empty strings

To demonstrate

var emptyNames = new string[] {};
string[] nullColors = null;

client.SearchAsync<MyDocument>(s =>
s.Index("myindex")
    .Query(q => q
        .Bool(bq => bq
            .Filter(
                fq => fq.Terms(t => t.Field(f => f.Name).Terms(emptyNames)),
                fq => fq.Terms(t => t.Field(f => f.Color).Terms(nullColors)))
        )
    )
);

results in

{}

Conditionless queries serve to make writing NEST queries easier in that you don't need to check if the collection has values before constructing a query. You can change conditionless semantics on a per query basis using .Strict() and .Verbatim().

like image 36
Russ Cam Avatar answered Oct 18 '22 23:10

Russ Cam