I'm trying to query an Elasticsearch index from C# via Elasticsearch.net (not NEST). Specifically, I need to get all documents with a status of "success" that have been created since a specific date. In an attempt to do this, I have:
var query = new {
query = new {
match = new {
field="status",
query="success"
}
},
range = new {
?
}
};
I'm not sure what to use for the range part. In fact, I'm not even sure if my syntax for the query is correct. I don't really understand how the C# syntax maps to the Query DSL in Elasticsearch. Any help is appreciated.
Thank you!
Range Queries in Elasticsearch Combining the greater than ( gt ) and less than ( lt ) range parameters is an effective way to search for documents that contain a certain field value within a range where you know the upper and lower bounds. In this example, we can find all cars that were made in 2016, 2017, and 2018: 1.
By default, you cannot use from and size to page through more than 10,000 hits. This limit is a safeguard set by the index. max_result_window index setting. If you need to page through more than 10,000 hits, use the search_after parameter instead.
An Field Range Index accelerates queries for comparisons within a specified type. Each Field Range Index keeps track of the values appearing in a field. Field Range Indexes also allow you to use the cts:field-values family of lexicon APIs and to use the cts:field-range-query constructor in searches.
Something like this should do:
var query = new {
bool = new {
must = new {
match = new {
field = "status",
query = "success"
}
},
filter = new {
range = new {
createDate = new {
gt = "2018-12-01T00:00:00.000Z"
}
}
}
}
};
I don't really understand how the C# syntax maps to the Query DSL in Elasticsearch.
By looking at provided example I assume you want to use PosData.Serializable(query). In this case the query object (no matter what type it is) will be JSON serialized and posted to elasticsearch cluster without any modifications. When you create object using new {} C# syntax it is serialized by default to JSON with the same keys as properties of this object. That is, the object
new {
query = new {
bool = new {
must = new {
term = new {
status = "success"
}
},
filter = new {
range = new {
date = new { gte = "2018-12-22T00:00:00.000Z" }
}
}
}
}
}
will be serialized and passed to elasticsearch as
"query": {
"bool": {
"must": {
"term": {
"status": "success"
}
},
"filter": {
"range": {
"date": { "gte": "2018-12-22T00:00:00.000Z" }
}
}
}
}
So, by using low level Elasticsearch client you create objects which has almost 1:1 mapping to Query DSL syntax. You can copy examples from elastic.co, replace ":" with " = new", remove quotes from property names and, basically, thats it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With