简体   繁体   English

在 Nest Api 查询中排除空值

[英]Exclude nulls in Nest Api query

I have added the following snippet in a nest query which returns a ISearchResponse<T>我在返回ISearchResponse<T>的嵌套查询中添加了以下代码片段

.Query(query => query.Match(m =>
    m.Field(f =>
        f => !string.IsNullOrEmpty(f.Purchase.NationalCode.Trim()))
    )))

But it makes no difference to the amount of records returned (and I know there are nulls in there)但这对返回的记录数量没有影响(而且我知道那里有空值)

Should I do be building my query some other way?我应该以其他方式构建查询吗?

Edited to add: I've tried the following based on this answer here but no luck either编辑添加:我已经根据这里的答案尝试了以下但也没有运气

.Query(query => query.Bool(b=>
   b.Must(m=> m.Exists(
      f=>f.Field("nationalCode")))))
.Query(query => query.Bool(b=>
   b.MustNot(m=> m.Term(
      "nationalCode", ""))))

Try the following尝试以下

.Query(query => query
    .Bool(b => b
        .Must(m => m
            .Exists(f => f.Field("nationalCode"))
        )
        .MustNot(m => m
            .Term("nationalCode", "")
        )
    )
)

Both the .Must and .MustNot need to be called on the same bool query descriptor .Must.MustNot需要在同一个 bool 查询描述符上调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM