- .NET Clients: other versions:
- Introduction
- Breaking Changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Missing Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Distance Range Query Usage
- Geo Hash Cell Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Bucket Aggregations
- Children Aggregation Usage
- Date Histogram Aggregation Usage
- Handling responses
- Date Range Aggregation Usage
- Handling Responses
- Filter Aggregation Usage
- Handling Responses
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Metric Aggregations
WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Verbatim and Strict Query Usage
editVerbatim and Strict Query Usage
editNEST has the concept of conditionless queries; if the input to a query is determined to be empty, for example,
null
or ""
for a string input, then the query will not be serialized and sent to Elasticsearch. If a conditionless
query is part of a compound query then the query will not be part of the json query dsl sent to Elasticsearch.
Conditionless behavior can be controlled on individual queries by using Strict and Verbatim queries
- Strict
- Individual queries can be marked as strict meaning that if they are conditionless, an exception is thrown. This is useful for when a query must have an input value.
- Verbatim
- Individual queries can be marked as verbatim meaning that the query should be sent to Elasticsearch as is, even if it is conditionless.
Verbatim query usage
editIsVerbatim
should be set on individual queries to take effect
Fluent DSL example
editq .Bool(b => b .Must(qt => qt .Term(t => t .Verbatim() .Field(p => p.Description) .Value("") ), qt => qt .Term(t => t .Field(p => p.Name) .Value("foo") ) ) )
Object Initializer syntax example
editnew TermQuery { IsVerbatim = true, Field = "description", Value = "" } && new TermQuery { Field = "name", Value = "foo" }
Example json output.
{ "bool": { "must": [ { "term": { "description": { "value": "" } } }, { "term": { "name": { "value": "foo" } } } ] } }
Non-Cascading Strict Outer Queries
editSetting IsStrict
on the outer query container does not cascade
Fluent DSL example
editq .Strict() .Bool(b => b .Must(qt => qt .Term(t => t .Field(p => p.Description) .Value("") ), qt => qt .Term(t => t .Field(p => p.Name) .Value("foo") ) ) )
Object Initializer syntax example
editIQueryContainer query = new QueryContainer(new BoolQuery { Must = new List<QueryContainer> { new TermQuery { Field = "description", Value = "" }, new TermQuery { Field = "name", Value = "foo" } } }); query.IsStrict = true; return (QueryContainer)query;
Example json output.
{ "bool": { "must": [ { "term": { "name": { "value": "foo" } } } ] } }
Non-Cascading Verbatim Outer Queries
editSetting IsVerbatim
on the outer query container does not cascade
Fluent DSL example
editq .Verbatim() .Bool(b => b .Must(qt => qt .Term(t => t .Field(p => p.Description) .Value("") ), qt => qt .Term(t => t .Field(p => p.Name) .Value("foo") ) ) )
Object Initializer syntax example
editIQueryContainer query = new QueryContainer(new BoolQuery { Must = new List<QueryContainer> { new TermQuery { Field = "description", Value = "" }, new TermQuery { Field = "name", Value = "foo" } } }); query.IsVerbatim = true; return (QueryContainer)query;
Example json output.
{ "bool": { "must": [ { "term": { "name": { "value": "foo" } } } ] } }
A compound query can also be marked as verbatim, demonstrated here with a bool
query.
Fluent DSL example
editq .Bool(b => b .Verbatim() )
Object Initializer syntax example
editnew BoolQuery { IsVerbatim = true, }
Example json output.
{ "bool": {} }
A single verbatim query will be serialized as-is
Fluent DSL example
editq .Term(t => t .Verbatim() .Field(p => p.Description) .Value("") )
Object Initializer syntax example
editnew TermQuery { IsVerbatim = true, Field = "description", Value = "" }
Example json output.
{ "term": { "description": { "value": "" } } }
Leaf queries within a compound query marked as verbatim will also be serialized as-is
Fluent DSL example
editq .Bool(b => b .Filter(f => !f .Term(t => t .Verbatim() .Field(p => p.Name) .Value("") ) && f .Exists(e => e .Field(p => p.NumberOfCommits) ) ) )
Object Initializer syntax example
editnew BoolQuery { Filter = new QueryContainer[] { !new TermQuery { IsVerbatim = true, Field = "name", Value = "" } && new ExistsQuery { Field = "numberOfCommits" } } }
Example json output.
{ "bool": { "filter": [ { "bool": { "must": [ { "exists": { "field": "numberOfCommits" } } ], "must_not": [ { "term": { "name": { "value": "" } } } ] } } ] } }
Strict Query Usage
editA query can be marked as strict meaning that if it is determined to be conditionless, it will throw an
exception. The following example demonstrates this by trying to send an empty string as the value for
a term
query marked as strict
On this page
- Verbatim query usage
- Fluent DSL example
- Object Initializer syntax example
- Non-Cascading Strict Outer Queries
- Fluent DSL example
- Object Initializer syntax example
- Non-Cascading Verbatim Outer Queries
- Fluent DSL example
- Object Initializer syntax example
- Fluent DSL example
- Object Initializer syntax example
- Fluent DSL example
- Object Initializer syntax example
- Fluent DSL example
- Object Initializer syntax example
- Strict Query Usage