查找字段/附件为空的文档,使用Nest + C#进行ElasticSearch

3z6pesqy  于 2023-02-11  发布在  ElasticSearch
关注(0)|答案(2)|浏览(154)

我正在尝试搜索字段为空的文档。以下尝试无效:

var searchResult1 = client.Search<Doc>(s => s
    .From(0)
    .Size(10)
    .Query(q => q
        .Match(m => m
        .Field(f => f.Attachment.Content)
        .Query("")
        )
    )
);

var searchResult2 = client.Search<Doc>(x => x
    .Query(query => query
        .Term(term => term
        .Field(new Field("Attachment.Content"))
        .Value("")
        )
    )
);

请注意,附件的类型为Nest.Attachment。

hgtggwj0

hgtggwj01#

你已经试过Verbatim()了吗

var searchResult2 = client.Search<Doc>(x => x
.Query(query => query
    .Term(t => t.Verbatim()
    .Field(f => f.x)
    .Value("")
    )
)
);
mlnl4t2r

mlnl4t2r2#

只是添加到@fubo的asnwer。确保将字段索引为KeywordTHEN对术语进行查询搜索,否则将不会返回任何文档。(正如问题作者@cs0815对链接答案的回复)

private void EnsureIndexExitstance()
        {
            var indexExists = _elasticClient.Indices.Exists(indexName);

            if (!indexExists.Exists)
            {
                _elasticClient.Indices.Create(
                    indexName, 
                    c => c.Map<Document>(mm =>
                        mm.Properties(p =>
                            p.Keyword(k =>
                                k.Name("fieldName")))));
            }
        }

相关问题