12

I have a .Net application trying to fetch data from an elasticsearch document store, having records in the following structure:

{
  "_index": "TestIndex",
  "_type": "amqp",
  "_id": "123",
  "_source": {
    "@timestamp": "2014-10-27T01:31:54.780Z",    
    "type": "amqp",        
    "LogGenerationTime": "2014-10-26T21:31:54.780",    
    "ThreadID": "6",
    "ProcessID": "8136",
    "SessionID": "xyz",
    "UserID": "12345678",
  },  
}

I want to fetch all the records with LogGenerationTime in the last 20 mins. Here's the query that I have written so far but it does not seem to return any data:

    var format = "yyyy-MM-dd'T'HH:mm:ss.fff";
    var lowerBound = DateTime.Now.AddMinutes(-20);
    ISearchResponse<Amqp> resultSet = _elasticSearchClient.Search<Amqp>(q => q.Query
                    (p => p.Range
                        (v => v.OnField
                            (x => x.LogGenerationTime).GreaterOrEquals(lowerBound, format))));

Can someone please help write the correct query to fetch expected results? Thanks!

Chrysalis
  • 4,130
  • 2
  • 21
  • 23
  • your query looks pretty much correct. I suggest using fiddler to see the json that it sends to ES to try to determine the issue. – jhilden Oct 27 '14 at 18:46

2 Answers2

8

Looking at the source code, there are two overloads of the OnField method. When I use the the that takes Linq expression parameter, the query does not return any data. But I was able to make it work with the other overload, which takes string value, where I am passing the field name of the elasticsearch document as a string. Here's the query that returns expected results:

var resultSet = _elasticSearchClient.Search<Amqp>(q => q.Query
                (p => p.Range(v => v.OnField("LogGenerationTime").GreaterOrEquals(lowerBound))).Size(10000));

Looks like a bug in the framework but am not completely sure.

Chrysalis
  • 4,130
  • 2
  • 21
  • 23
  • NEST by default camelCases property expressions, see [this answer here](http://stackoverflow.com/questions/26434104/elasticsearch-net-client-cant-do-basic-search/26450038#26450038) how you can override this behaviour. Verbatim strings or [attribute marked properties](https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Nest/Domain/Mapping/Attributes/ElasticPropertyAttribute.cs#L12) always take precedence. – Martijn Laarman Nov 04 '14 at 10:05
1

The following code works for me:

Range(r => r.GreaterOrEquals(lowerBound).Format("MM/dd/yyyy").OnField(LogTime))
Pang
  • 9,564
  • 146
  • 81
  • 122
Olev
  • 69
  • 5