The requirement is to group by Employee Id and Name and then aggregate the sum(salary). I have managed to get it to work. However, the final aggregated result has to be sorted in descending order of the sum. I tried couple of ways but the ordering doesn't seem to work.
var resp =
elasticClient.Search<Employee>(
k => k.Size(0).Query(q => q.QueryString(s => s.Query(query)))
.Aggregations(a => a.Terms("EmpId", v => v.Field(f => f.EmpId)
.Aggregations(aa => aa.Terms("EmpName",vv => vv.Field(ff => ff.EmpName).OrderDescending("Salary")
.Aggregations(aaa => aaa.Sum("Salary", sa => sa.Field(fff => fff.Salary))))))));
Employee Class:
public sealed class Employee
{
public long Id { get; set; }
public int DateId { get; set; }
public int? DivisionNumber { get; set; }
public string Manager { get; set; }
public decimal Salary { get; set; }
public int UpdateId { get; set; }
public DateTime UpdateTimestamp { get; set; }
public int EmpId { get; set; }
public string EmpName { get; set; }
}
Mapping:
POST /sample_employee
{
"mappings": {
"post":{
"properties": {
"empId": {
"type" : "long"
},
"empName": {
"type": "string",
"index": "not_analyzed"
},
"salary": {
"type": "float"
},
"dateId": {
"type": "long"
},
"divisionNumber":{
"type": "long"
}
}
}
}
}
Query for aggregation:
GET /sample_employee/_search
{
"size": 0,
"query": {
"query_string": {
"query": "dateId:(1780) AND divisionNumber:(1)"
}
},
"aggs": {
"EmpId": {
"terms": {
"field": "empId"
},
"aggs": {
"EmpName": {
"terms": {
"field": "empName",
"size": 30,
"order":{
"Amount.value": "desc"}
},
"aggs": {
"Amount": {
"sum": {
"field": "salary"
}
}
}
}
}
}
}
}
Response:
"aggregations": {
"EmpId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 10,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jerry Mathews",
"doc_count": 1,
"Amount": {
"value": 10000
}
}
]
}
},
{
"key": 11,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Tom Raju",
"doc_count": 1,
"Amount": {
"value": 15000
}
}
]
}
},
{
"key": 12,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Nel Gad",
"doc_count": 1,
"Amount": {
"value": 20000
}
}
]
}
},
{
"key": 13,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Jim Thomas",
"doc_count": 1,
"Amount": {
"value": 25000
}
}
]
}
},
{
"key": 14,
"doc_count": 1,
"EmpName": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Amat Ahu",
"doc_count": 1,
"Amount": {
"value": 30000
}
}
]
}
}
]
}
}
}
Related
I can not deserialize json to Dictionary<string, List<Purchas>> in C#.
Here is my .json:
{
"Ukraine": {
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 190,
"Currency": "UAH"
}
}
},
"USA": {
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 19,
"Currency": "USD"
}
}
}
}
Here is my Purchase class:
public class Price
{
public int Amount { get; set; }
public string Currency { get; set; }
}
public class Purchase
{
public int Credits { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
Here is how I am trying to deserialize it:
var countryToPurchases = JsonConvert.DeserializeObject<Dictionary<string, List<Purchase>>>(dataJSON);
Here is the error I am getting:
JsonReaderException: Invalid property identifier character: {. Path 'Ukraine', line 3, position 4.
Newtonsoft.Json.JsonTextReader.ParseProperty()
What am I missing here?
You have two problems here:
The Json is invalid. There should be arrays there but there aren't.
A valid json would look like this:
[
{
"Ukraine": [
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 190,
"Currency": "UAH"
}
}
]
},
{
"USA": [
{
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
},
{
"Credits": 1000,
"Name": "Smart hare",
"Price": {
"Amount": 19,
"Currency": "USD"
}
}
]
}
]
The Price property should be of type Price, not int.
public class Purchase
{
public int Credits { get; set; }
public string Name { get; set; }
public Price Price { get; set; }
}
please check this json and see if it's works :
{
"Ukraine": {
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 100,
"Currency": "UAH"
}
},
"USA": {
"Credits": 500,
"Name": "Clever goat",
"Price": {
"Amount": 10,
"Currency": "USD"
}
}
}
I need to revise a method that builds a SearchDescriptor using .Nest so that the score is higher for
product search results for items having a contract price (value of zero).
I captured the serialized version of the query added "field_value_factor" to return the results in the desired order. I have not determined how to achieve this in the .Nest query statement.
Can someone recommend how to revise the .NEST client statements to produce the same search descriptor?
Thank you
Below is the query we want to achieve where you will see field_value_factor at the bottom:
{
"from": 0,
"size": 3000,
"sort": [
{
"_score": {
"order": "desc"
}
},
{
"priceStatus": {
"order": "asc"
}
},
{
"unitPrice": {
"order": "asc"
}
}
],
"aggs": {
"PriceStatus": {
"terms": {
"field": "priceStatus",
"size": 5
}
},
"VendorName": {
"terms": {
"field": "vendorName",
"size": 5
}
},
"CatalogName": {
"terms": {
"field": "catalogName",
"size": 5
}
},
"ManufacturerName": {
"terms": {
"field": "manufacturerName",
"size": 5
}
},
"IsGreen": {
"terms": {
"field": "isGreen",
"size": 5
}
},
"IsValuePack": {
"terms": {
"field": "isValuePack",
"size": 5
}
},
"UnitOfMeasure": {
"terms": {
"field": "unitOfMeasure",
"size": 5
}
},
"Attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"TheAttributeName": {
"terms": {
"field": "attributes.name",
"size": 10
},
"aggs": {
"TheAttributeValue": {
"terms": {
"field": "attributes.value",
"size": 5
}
}
}
}
}
}
},
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"multi_match": {
"type": "phrase",
"query": "pen",
"slop": 3,
"boost": 16.0,
"fields": [
"itemNumber*^4",
"shortDescription*^4",
"subCategory1Name*^1.5",
"subCategory2Name*^2.0",
"categoryName*^0.9",
"longDescription*^0.6",
"catalogName*^0.30",
"manufactureName*^0.20",
"vendorName*^0.15",
"upcCode*^0.10"
]
}
},
{
"multi_match": {
"query": "pen",
"boost": 15.0,
"minimum_should_match": "75%",
"fields": [
"itemNumber*^4",
"shortDescription*^4",
"subCategory1Name*^1.5",
"subCategory2Name*^2.0",
"categoryName*^0.9",
"longDescription*^0.6",
"catalogName*^0.30",
"manufactureName*^0.20",
"vendorName*^0.15",
"upcCode*^0.10"
]
}
},
{
"multi_match": {
"query": "pen",
"fuzziness": 1.0,
"slop": 2,
"minimum_should_match": "75%",
"fields": [
"itemNumber*^4",
"shortDescription*^4",
"subCategory1Name*^1.5",
"subCategory2Name*^2.0",
"categoryName*^0.9",
"longDescription*^0.6",
"catalogName*^0.30",
"manufactureName*^0.20",
"vendorName*^0.15",
"upcCode*^0.10"
]
}
}
]
}
},
"filter": {
"bool": {
"must": [
{
"terms": {
"catalogId": [
"fbb3dd2c-f81c-4ff3-bd5b-9c2cffc51540"
]
}
}
]
}
},
"field_value_factor": {
"field": "priceStatus",
"factor": -1,
"modifier": "none"
}
}
}
}
Below is the current method that builds the SearchDescriptor:
private SearchDescriptor<SearchItem> BuildSearchDescriptor(
string searchTerm,
IList<Guid> catalogIds,
int from,
int size,
string index,
string preference,
int attrSize,
int valueSize,
Dictionary<string, string[]> filterProps,
Dictionary<string, string[]> filterAttrs,
Guid? categoryId)
{
var searchDescriptor = new SearchDescriptor<SearchItem>()
.From(from)
.Size(size)
.Query(q =>
q.Filtered(fd => BuildFilterTerms(fd, filterProps, filterAttrs, catalogIds, categoryId)
.Query(iq => BuildQueryContainer(iq, searchTerm))
)
)
.Index(index)
.Preference(preference)
.Aggregations(agg => BuildAggregationDescriptor(agg, attrSize, valueSize, catalogIds.Count))
.Sort(sort => sort.OnField("_score").Descending())
.SortAscending(p=> p.PriceStatus)
.SortAscending(p => p.UnitPrice);
// Debug the raw string that will post to the ES servers i.e. use this in postman
//var str = System.Text.Encoding.UTF8.GetString(client.Serializer.Serialize(searchDescriptor));
return searchDescriptor;
}
Your JSON query isn't valid; field_value_factor is a function of a function_score query. In NEST 1.x, this would look like
var response = client.Search<Document>(x => x
.Query(q => q
.FunctionScore(fs => fs
.Functions(fu => fu
.FieldValueFactor(fvf => fvf
.Field(f => f.PriceStatus)
.Factor(-1)
.Modifier(FieldValueFactorModifier.None)
)
)
)
)
);
public class Document
{
public string Title { get; set; }
public int PriceStatus { get; set; }
}
which produces the query
{
"query": {
"function_score": {
"functions": [
{
"field_value_factor": {
"field": "PriceStatus",
"factor": -1.0,
"modifier": "none"
}
}
]
}
}
}
I want to deserialize JSON response received by Telegram Bot API by getUpdate() method.
JSON data:
{
"ok": true,
"result": [
{
"update_id": 920493886,
"message": {
"message_id": 123,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457375,
"text": "Aata aala"
}
},
{
"update_id": 920493887,
"message": {
"message_id": 124,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457387,
"text": "Jeva tuzyakadun reply aala tevha"
}
},
{
"update_id": 920493888,
"message": {
"message_id": 125,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457443,
"text": "Deposite"
}
},
{
"update_id": 920493889,
"message": {
"message_id": 127,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457645,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493890,
"message": {
"message_id": 128,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472457670,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493891,
"message": {
"message_id": 130,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457848,
"text": "Deposite"
}
},
{
"update_id": 920493892,
"message": {
"message_id": 132,
"from": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k"
},
"chat": {
"id": 201520743,
"first_name": "Chandrakant",
"last_name": "Kumathekar",
"username": "chandrakant_k",
"type": "private"
},
"date": 1472457883,
"text": "Deposite"
}
},
{
"update_id": 920493893,
"message": {
"message_id": 133,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472468407,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
},
{
"update_id": 920493894,
"message": {
"message_id": 134,
"from": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777"
},
"chat": {
"id": 219633031,
"first_name": "Shreeeeeee",
"username": "Winner7777",
"type": "private"
},
"date": 1472473070,
"text": "\/menu",
"entities": [
{
"type": "bot_command",
"offset": 0,
"length": 5
}
]
}
}
]
}
I have generated classes from json2csharp
public class From
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string username { get; set; }
}
public class Chat
{
public int id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string type { get; set; }
public string username { get; set; }
}
public class Entity
{
public string type { get; set; }
public int offset { get; set; }
public int length { get; set; }
}
public class Message
{
public int message_id { get; set; }
public From from { get; set; }
public Chat chat { get; set; }
public int date { get; set; }
public string text { get; set; }
public List<Entity> entities { get; set; }
}
public class Result
{
public int update_id { get; set; }
public Message message { get; set; }
}
public class RootObject
{
public bool ok { get; set; }
public List<Result> result { get; set; }
}
using newtonsoft to deserialization
var d = Newtonsoft.Json.JsonConvert.DeserializeObject(rcvd_data);
what to do next? how to put this all at work? I am confused please help.
To deserialization use this code:
RootObject ro = JsonConvert.DeserializeObject<RootObject>(rcvd_data);
And in ro you have all data.
EXAMPLE
bool ok = ro.ok;
foreach(Result r in ro.result)
{
int uId = r.update_id;
Message m = r.message;
int msgId = m.message_id;
}
string data = #"{""ok"":true,""result"":[{""update_id"":920493886,...";
RootObject ro = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(data);
foreach (Result result in ro.result)
{
//two example fields
Console.WriteLine("update_id= " + result.update_id);
Console.WriteLine("message text= "+result.message.text);
}
Based on: https://yts.am/api/v2/list_movies.json (Source: https://yts.am/api)
I used: http://json2csharp.com/ to create classes
Copy and paste the result of the get request of postman to json2csharp to automatically create the classes that you need
And once created, i used them on a Service like this:
private string URL = "https://yts.am/api/v2/list_movies.json";
public async Task<List<Movie>> GetMovies()
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(URL);
RootObject lista = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(json);
List<Movie> todoes = lista.data.movies;
return todoes;
}
I'm using Newtonsoft.Json
Try this way
var d = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(rcvd_data);
then d will contain list of RootObject. By iterating using foreach you can get all the properties of RootObject model
I am calling a REST service to get back a JSON structure.
The error I get is
Test method BarPanda.Web.Services.Test.PosServiceTests.GetMenu threw exception:
System.MissingMethodException: No parameterless constructor defined for this object.
The original JSON is as follows (partial)
{
"count": 3,
"limit": 50,
"_links": {
"self": {
"etag": "b49a27c7e7c663af8d6a736e24fac7f5",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/" ,
"profile": "https://panel.omnivore.io/docs/api#category_list"
}
},
"_embedded": {
"categories": [
{
"id": "AdiRjiAp",
"name": "Drinks",
"_links": {
"items": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/items/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_list"
},
"self": {
"etag": "05dad4d734401321a4854cf4f0369102",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/categories/AdiRjiAp/" ,
"profile": "https://panel.omnivore.io/docs/api#category_retrieve"
}
},
"_embedded": {
"items": [
{
"id": "gki84ia9",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Soda",
"open": false,
"pos_id": "gki84ia9",
"price": 150,
"price_levels": [
{
"id": "Byineidy",
"price": 150
},
{
"id": "g4T4dTBj",
"price": 200
},
{
"id": "K6czkc8b",
"price": 250
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list"
},
"self": {
"etag": "c59b380aed5c1f33915b028b739df955",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/gki84ia9/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"
}
}
},
{
"id": "doTaLTyg",
"in_stock": true,
"modifier_groups_count": 0,
"name": "Orange Juice",
"open": false,
"pos_id": "doTaLTyg",
"price": 175,
"price_levels": [
{
"id": "L4iqKid8",
"price": 175
},
{
"id": "K6T8MTzb",
"price": 300
}
],
"_links": {
"modifier_groups": {
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/modifier_groups/" ,
"profile": "https://panel.omnivore.io/docs/api#modifier-group_list"
},
"self": {
"etag": "d3ae9754edb321f18e192ebea446baeb",
"href": "https://api.omnivore.io/0.1/locations/gcBdM7TL/menu/items/doTaLTyg/" ,
"profile": "https://panel.omnivore.io/docs/api#menu-item_retrieve"
}
}
}
]
}
},
I am trying to deserialize it with the following code and object classes
var response = _client.Execute(request);
var converter = new JsonDeserializer();
var menu = converter.Deserialize<PosMenu>(response);
PosMenu
[DataContract]
public class PosMenu
{
[DataMember]
public int VenueId { get; set; }
[DataMember]
public int count { get; set; }
[DataMember]
public PosMenuEmbedded _embedded { get; set; }
}
PosMenuEmbedded
[DataContract]
public class PosMenuEmbedded
{
[DataMember]
public long UniqueId { get; set; }
[DataMember]
public PosMenuCategory[] categories { get; set; }
[DataMember]
public int PosMenuId { get; set; }
}
PosMenuCategory
[DataContract]
public class PosMenuCategory
{
}
Note: I have taken all properties out of this class for now just to see if I could get it working with a blank class, but alas not.
If I comment out the line in PosMenuEmbedded
public PosMenuCategory[] categories { get; set; }
It succeeds. If I put it back in, it fails, even with an empty class.
Can anyone suggest why this might be?
[DataMember]
public List<PosMenuCategory> categories { get; set; }
i am getting measurements from withings and want to show them in graphs but not able to convert it to json. i also try JsonConvert.SerializeObject(myString) using Newtonsoft.dlland simple
System.Web.Script.Serialization.JavaScriptSerializer sr = new System.Web.Script.Serialization.JavaScriptSerializer();
sr.Serialize(myString);
but it is not converting.
My withings measurement string is as follows.
{
"status": 0,
"body": {
"updatetime": 1392764547,
"measuregrps": [
{
"grpid": 17945868,
"attrib": 0,
"date": 139984270,
"category": 1,
"measures": [
{
"value": 72,
"type": 9,
"unit": 0
},
{
"value": 152,
"type": 10,
"unit": 7
},
{
"value": 87,
"type": 17,
"unit": 0
}
]
},
{
"grpid": 176587495,
"attrib": 0,
"date": 13915689,
"category": 1,
"measures": [
{
"value": 94,
"type": 9,
"unit": 0
},
{
"value": 145,
"type": 10,
"unit": 0
},
{
"value": 109,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179262494,
"attrib": 0,
"date": 1391369607,
"category": 1,
"measures": [
{
"value": 77,
"type": 9,
"unit": 0
},
{
"value": 121,
"type": 10,
"unit": 0
},
{
"value": 87,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179258492,
"attrib": 0,
"date": 1391171167,
"category": 1,
"measures": [
{
"value": 61,
"type": 9,
"unit": 0
},
{
"value": 107,
"type": 10,
"unit": 0
},
{
"value": 80,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179089150,
"attrib": 0,
"date": 1391167537,
"category": 1,
"measures": [
{
"value": 69,
"type": 9,
"unit": 0
},
{
"value": 112,
"type": 10,
"unit": 0
},
{
"value": 67,
"type": 11,
"unit": 0
}
]
},
{
"grpid": 179079661,
"attrib": 2,
"date": 1391164672,
"category": 1,
"measures": [
{
"value": 720,
"type": 1,
"unit": -1
}
]
},
{
"grpid": 17998560,
"attrib": 2,
"date": 146989672,
"category": 1,
"measures": [
{
"value": 284,
"type": 4,
"unit": -2
}
]
}
]
}
}
It seems, you want to deserialize your json string, not serialize:
var obj = JsonConvert.DeserializeObject<Withings.RootObject>(json);
public class Withings
{
public class Measure
{
public int value { get; set; }
public int type { get; set; }
public int unit { get; set; }
}
public class Measuregrp
{
public int grpid { get; set; }
public int attrib { get; set; }
public int date { get; set; }
public int category { get; set; }
public List<Measure> measures { get; set; }
}
public class Body
{
public int updatetime { get; set; }
public List<Measuregrp> measuregrps { get; set; }
}
public class RootObject
{
public int status { get; set; }
public Body body { get; set; }
}
}
JsonConvert.SerializeObject(myString) takes an object an returns a string. If you want to turn a string into an object you want to use Deserialize<T>(sting json). Given the arguments name in your sample is myString I would assume you're using the method wrong.
To deserialize you need an equivalent type like;
public class myObject
{
public int status { get; set; }
public Body body { get; set; }
}
public class Body
{
//other parameters ect
}
Your object model needs to exactly match the json in order by Deserialize<T> to behave correctly.