elasticsearchnestelasticsearch-plugin

Nest QueryContainer usage


I am able to populate QueryContainer with DateRangeQuery array as shown below

        QueryContainer marriageDateQuerys = null;

        if (!string.IsNullOrEmpty((item.marriage_date)))
            {
                DateRangeQuery query = new DateRangeQuery();
                query.Field = "marriages.marriage_date";
                query.Name = item.marriage_date;
                query.GreaterThanOrEqualTo = item.marriage_date;
                query.LessThanOrEqualTo = item.marriage_date;

                marriageDateQuerys &= query;
            }

But when I use QueryContainer to use MatchQuery/TermQuery to populate data it is not happening.

           QueryContainer marriageSpouseFirstNameQuerys = null;  


            if (!string.IsNullOrEmpty((item.spouse_first_name)))
            {
                MatchQuery query = new MatchQuery();
                query.Field = "marriages.spouse_first_name";
                query.Name = item.spouse_first_name;
                marriageSpouseFirstNameQuerys &= query;
            }

Query object is created in last if condition but marriageSpouseFirstNameQuerys is not populated with the same. I even tried marriageSpouseFirstNameQuerys += query; but without any success


Solution

  • Didn't try it but you can try something like this

                  Query = new QueryContainer(new BoolQuery
                        {
                            Must = new List<QueryContainer>
                            {
                                new MatchQuery
                                {
                                    //props
                                },
                                new TermQuery
                                {
                                    Field = field
                                    Value = value
                                },
                            }
                        })