如何使用 Nest C# 在 elasticsearch 查询 (EQL) 中使用 not exist 或 null?

How can I use not exist or null in elasticsearch query (EQL) by using Nest C#?

大家都知道,Nest Elasticsearch 搞清楚查询找东西不容易,也很无聊。我也偶然发现了这个问题。结果我无法在查询中使用 'not empty' 和 null。

   var list = client.Count<LogMessage>(s => s.Index("xxx-*").Query(q =>
            !q.Term(t => t.Field(f => f.Test.Suffix("keyword")).Value(null)) &&
            q.Term(t => t.Field(f => f.Environment.Suffix("keyword")).Value("yyy")) &&
            q.DateRange(t => t.Field(f => f.LogDate).GreaterThan(DateTime.Now.AddMinutes(-15)))));

Null 或 string.Empty 在这里不起作用。如何使用 NOT null 或 Not Empty?

IMO 这不是一个 NEST 可用性问题,因为在 Elasticsearch 本身中执行此操作非常重要。我通过使用 .Exists 在该字段 and/or 上否定通配符查询 (.Wilcard) 来查找没有该字段的文档而获得成功,因为空值未存储在文档中并且为空很难在非关键字文本字段中搜索值,因为分析器不会查找空白值。

请看这个答案和下面那个更短的答案。 https://github.com/elastic/elasticsearch/issues/7515#issuecomment-158668403

我想你要找的是:

 .Query(q => q.Bool(b => b
                       .Must(m => m
                            .Exists(e => e
                                .Field(f => f.Test)
                           )
                       )
                  ))

这会检查空值。