I have a console application that will read the logs from elasticsearch and insert those logs to a database. However I am having an issue with the count api implementation in NEST. Whenever, I type the following in the CLI of kibana I get a value for "count"
to be 16
.
GET /customer-simulation-es-app-logs-development-2021-08/_count
{
"query": {
"bool": {
"should": [
{
"match": {
"fields.Username": "Jessica12"
}
}
],
"filter": [
{
"range": {
"@timestamp": {
"gte": "2021-08-16T00:00:00.000-05:00",
"lt": "2021-08-16T23:59:59.999-05:00"
}
}
}
],
"minimum_should_match": 1
}
}
}
The results returned are:
{
"count" : 16,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
}
}
Then in my NEST implementation for .NET, I get countResponse
to be 0 instead of 16. I do not understand why I am getting that if I am doing the exact same query search in the CLI. Am I not?
private IElasticClient _elasticClient = new ElasticClient();
string indexName = "customer-simulation-es-app-logs-development-2021-08";
var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200"));
connectionSettings.DefaultIndex(indexName);
connectionSettings.EnableDebugMode();
_elasticClient = new ElasticClient(connectionSettings);
// this will tell us how much hits/results there is based on the following criteria
var countResponse = _elasticClient.Count<SourceItems>(c => c
.Query(q => q
.Bool(b => b
.Should(
m => m
.Match(ma => ma
.Field(fa => fa._Source.fields.Username)
.Query("Jessica12")))
.Filter(f => f.DateRange(dr => dr
.Field("@timestamp")
.GreaterThanOrEquals("2021-08-16T00:00:00.000-05:00")
.LessThanOrEquals("2021-08-16T23:59:59.999-05:00")))
.MinimumShouldMatch(1)))).Count;
Console.WriteLine($"\nDocuments in index: {countResponse}");
SourceItems.cs
:
public class SourceItems
{
public String _Index { get; set; }
public String _Id { get; set; }
public Source _Source { get; set; }
}
public class Source
{
[Date(Name = "@timestamp")]
public DateTimeOffset timestamp { get; set; }
public String level { get; set; }
public String message { get; set; }
public EsFields fields { get; set; }
}
public class EsFields
{
public String Username { get; set; }
public String IP { get; set; }
public String SourceContext { get; set; }
public String RequestId { get; set; }
public String RequestPath { get; set; }
public String ConnectionId { get; set; }
public String CorrelationId { get; set; }
}
When I do a search to match the username Jessica12 I get the hits and you can see the username stored in the field.
GET /customer-simulation-es-app-logs-development-2021-08/_search
{
"size": 200,
"query": {
"bool": {
"should": [
{
"match": {
"fields.Username": "Jessica12"
}
}
],
"filter": [
{
"range": {
"@timestamp": {
"gte": "2021-08-16T00:00:00.000-05:00",
"lt": "2021-08-16T23:59:59.999-05:00"
}
}
}
],
"minimum_should_match": 1
}
}
}