cs0815
cs0815

Reputation: 17388

find documents with empty field / Attachment.Content in elastic search using Nest + C#

I am trying to search for documents with an empty field. These attempts do not work:

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("")
        )
    )
);

Please note that Attachment is of type Nest.Attachment.

Upvotes: 0

Views: 254

Answers (2)

GGesheva
GGesheva

Reputation: 186

Just to add up to @fubo's asnwer. Make sure you index the field as Keyword THEN do a query search on term otherwise you will end up with no documents returned. (just as @cs0815, the author of the question, replied to the linked answer)

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

Upvotes: 0

fubo
fubo

Reputation: 45947

did you already try Verbatim()

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

Upvotes: 2

Related Questions