Skip to content

Commit

Permalink
demonstrate fluent queries to exclude fields with empty values (opens…
Browse files Browse the repository at this point in the history
…earch-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]>
  • Loading branch information
david-alpert-nl authored and harshavamsi committed Oct 31, 2023
1 parent da42d16 commit 3fc0be3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions _clients/OSC-dot-net.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ internal class Program
PrintResponse(searchResponse);
}

private static void SearchForAllStudentsWithANonEmptyLastName()
{
var searchResponse = osClient.Search<Student>(s => s
.Index("students")
.Query(q => q
.Bool(b => b
.Must(m => m.Exists(fld => fld.LastName))
.MustNot(m => m.Term(t => t.Verbatim().Field(fld => fld.LastName).Value(string.Empty)))
)));

PrintResponse(searchResponse);
}

private static void SearchLowLevel()
{
// Search for the student using the low-level client
Expand Down

0 comments on commit 3fc0be3

Please sign in to comment.