Skip to content

Commit ca40c61

Browse files
demonstrate fluent queries to exclude fields with empty values (opensearch-project#4579)
a `TermQuery` with an empty string `Value` is considered by the .NET client to be "conditionless" and is removed from the search request body by default. for those times that you want to use a `TermQuery` with an empty string `Value` (e.g. show me all documents with a non-empty last_name property) you need to use the `.Verbatim()` method to tell the client library to include the `TermQuery` as written even though it is considered to be "conditionless". in other words, while a "conditionless" query may not make sense in the positive it can make sense in the negative ``` GET /my-index/_search { "query": { "bool": { "must": [{ "exists": { "field": "last_name"} }], "must_not": [{ "term": {"last_name.keyword": { "value": "" }} }] } }, } ``` Signed-off-by: David Alpert (Next League) <[email protected]>
1 parent 42e8a1b commit ca40c61

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

_clients/OSC-dot-net.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,19 @@ internal class Program
299299
PrintResponse(searchResponse);
300300
}
301301

302+
private static void SearchForAllStudentsWithANonEmptyLastName()
303+
{
304+
var searchResponse = osClient.Search<Student>(s => s
305+
.Index("students")
306+
.Query(q => q
307+
.Bool(b => b
308+
.Must(m => m.Exists(fld => fld.LastName))
309+
.MustNot(m => m.Term(t => t.Verbatim().Field(fld => fld.LastName).Value(string.Empty)))
310+
)));
311+
312+
PrintResponse(searchResponse);
313+
}
314+
302315
private static void SearchLowLevel()
303316
{
304317
// Search for the student using the low-level client

0 commit comments

Comments
 (0)