elasticsearch Nest QueryContainer用法

zpjtge22  于 2023-03-29  发布在  ElasticSearch
关注(0)|答案(2)|浏览(136)

我能够使用DateRangeQuery数组填充QueryContainer,如下所示

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;
            }

但是,当我使用QueryContainer来使用MatchQuery/TermQuery填充数据时,它并没有发生。

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;
            }

查询对象是在last if条件中创建的,但marriageSpouseFirstNameQuerys没有使用相同的条件填充。

30byixjq

30byixjq1#

没试过但你可以试试这样的

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

4dc9hkyq2#

下面的代码为我工作后,与eyildiz答案的变化

if (!string.IsNullOrEmpty((item.spouse_last_name)))
                {
                    marriageSpouseLastNameQuery = new QueryContainer(
                        new MatchQuery
                        {
                            Field = "marriages.spouse_last_name",
                            Query = item.spouse_last_name
                        });
                    lstmarriageSpouseLastNameQuerys.Add(marriageSpouseLastNameQuery);
                }

相关问题