I need to get a query from Elasticsearch for last registered records in specified area in range of specified serial numbers. for this reason I mapped my index in this shape:
{ "settings": {
"index": {
"number_of_shards": 5,
"number_of_replicas": 2
}
},
"mapping": {
"AssetStatus": {
"properties": {
"serialnumber": {
"type": "text",
"fielddata": true
},
"vehiclestate": {
"type": "text",
"fielddata": true
},
"vehiclegeopoint": {
"type": "geo-point",
"fielddata": true
},
"vehiclespeed": {
"type": "number",
"fielddata": true
},
"vehiclefuelpercent": {
"type": "text",
"fielddata": true
},
"devicebatterypercent": {
"type": "text",
"fielddata": true
},
"networklatency": {
"type": "text",
"fielddata": true
},
"satellitescount": {
"type": "number",
"fielddata": true
},
"createdate": {
"type": "date",
"fielddata": true
}
}
}
}
}
and this query works correctly
{
"query": {
"bool": {
"must": [
{
"term": {
"serialnumber.keyword": "2228187d-b1a5-4e18-82bb-4d12438e0ec0"
}
},
{
"range": {
"vehiclegeopoint.lat": {
"gt": "31.287958",
"lt": "31.295485"
}
}
},
{
"range": {
"vehiclegeopoint.lon": {
"gt": "48.639844",
"lt": "48.652032"
}
}
}
],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 0,
"sort": [],
"aggs": {
"SerialNumberGroups": {
"terms": {
"field": "serialnumber.keyword"
},
"aggs": {
"tops": {
"top_hits": {
"sort": [
{
"createdate.keyword": {
"order": "desc"
}
}
],
"size": 1
}
}
}
}
}
}
whereas my nest query has this error
Invalid NEST response built from a unsuccessful low level call on POST: /fms2/AssetStatus/_search?typed_keys=true
Audit trail of this API call:
- [1] BadResponse: Node: http://localhost:9200/ Took: 00:00:00.1917118
OriginalException: Elasticsearch.Net.ElasticsearchClientException: Request failed to execute. Call: Status code 400 from: POST
/fms2/AssetStatus/_search?typed_keys=true. ServerError: Type: parsing_exception Reason: "Unknown key for a VALUE_STRING in [field]."
Request:force it to be set on the response.>
Response:ConnectionSettings to force it to be set on the response.>
my nest query is this
var searchResponse =
Client.Search<AssetStatus>(x => x
.Index(settings.DefaultIndex)
.Type("AssetStatus")
.Query(fq => fq.GeoBoundingBox(c => c.Field(f => f.VehicleGeoPoint).BoundingBox(new GeoLocation(TopLeft.Lat, TopLeft.Lon), new GeoLocation(BottomRight.Lat, BottomRight.Lon))))
.Query(fq =>
fq.Bool(b => b.
Filter(
f => f.Match(m => m.Field(g => g.SerialNumber.Suffix("keyword").Equals(sn)))
)
))
.Aggregations(a => a
.Terms("group_by_SerialNumber", st => st
.Field(o => o.SerialNumber.Suffix("keyword"))
.Size(0)
.Aggregations(b=> b.TopHits("top_hits", lastRegistered => lastRegistered
.Field(bf=> bf.CreateDate.Suffix("keyword"))
.Size(1)))
))
);
This problem is for sorting in aggregation
Problem was in my POCO, as you can see in my nest query I use uppercase letters for naming my properties. I should use Nest library for using data annotation for elastic's POCO.
[ElasticsearchType(Name = "AssetStatus")]
public class AssetStatus
{
[Text]
[PropertyName("serialnumber")]
public string SerialNumber { get; set; }
[Text]
[PropertyName("vehiclestate")]
public string VehicleState { get; set; }
[GeoPoint]
[PropertyName("vehiclegeopoint")]
public GeoPoint VehicleGeoPoint { get; set; }
[Number]
[PropertyName("vehiclespeed")]
public int VehicleSpeed { get; set; }
[Text]
[PropertyName("vehiclefuelpercent")]
public string VehicleFuelPercent { get; set; }
[Text]
[PropertyName("devicebatterypercent")]
public string DeviceBatteryPercent { get; set; }
[Text]
[PropertyName("networklatency")]
public string NetworkLatency { get; set; }
[Number]
[PropertyName("satellitescount")]
public byte SatellitesCount { get; set; }
[Date]
[PropertyName("createdate")]
public string CreateDate { get; set; }
}