Parse.com manipulate Response Object - parse-platform

I am trying to work Ember with Parse.com using
ember-model-parse-adapter by samharnack.
I add added a function to make multiple work search(like search engine) for which I have defined a function on cloud using Parse.Cloud.define and run from client.
The problem is the Array that my cloud response returns is not compatible with Ember Model because of two attributes they are __type and className. how can I modify the response to get response similar to that i get when I run a find query from client. i.e without __type and className
Example responses
for App.List.find() = {
"results":[
{
"text":"zzz",
"words":[
"zzz"
],
"createdAt":"2013-06-25T16:19:04.120Z",
"updatedAt":"2013-06-25T16:19:04.120Z",
"objectId":"L1X55krC8x"
}
]
}
for App.List.cloudFunction("sliptSearch",{"text" : this.get("searchText")})
{
"results":[
{
"text":"zzz",
"words":[
"zzz"
],
"createdAt":"2013-06-25T16:19:04.120Z",
"updatedAt":"2013-06-25T16:19:04.120Z",
"objectId":"L1X55krC8x",
"__type" : Object, //undesired
"className" : "Lists" //undesired
}
]
}

Thanks Vlad something like this worked for me for array
resultobj = [];
searchListQuery.find({
success: function(results) {
for( var i=0, l=results.length; i<l; i++ ) {
temp = results.pop();
resultobj.push({
text: temp.get("text"),
createdAt: temp.createdAt,
updatedAt: temp.updatedAt,
objectId: temp.id,
words: "",
hashtags: ""
});
}

In your cloud code before you make any response, create and object and extract from it the attributes/members you need and then response it. like so:
//lets say result is some Parse.User or any other Parse.Object
function(result)
{
var responseObj = {};
responseObj.name = responseObj.get("name");
responseObj.age = responseObj.get("age");
responseObj.id = responseObj.id;
response.success(responseObj);
}
on the response side you will get {"result": {"name": "jhon", "age": "26", "id": "zxc123s21"}}
Hope this would help you

Related

GraphQL Apollo pagination and type policies

I am really struggling with this concept. I hope someone can help me understand it better.
The documentations uses a simple example and it's not 100% clear to me how it works.
I have tried using keyArgs, but they didn't work, so I adopted to use the args parameter in the read and merge functions. First, let me explain my scenario.
I have a couple of search endpoints that use the same parameters:
{
search:
{
searchTerm: "*",
includePartialMatch: true,
page: 1,
itemsToShow: 2,
filters: {},
facets: [],
orderBy: {}
}
}
So I have setup my type policies like this:
const cache = new InMemoryCache({
typePolicies: {
Query: {
fields: {
searchCategories: typePolicy,
searchBrands: typePolicy,
searchPages: typePolicy,
searchProducts: typePolicy,
},
},
},
});
And I was using a generic typePolicy for them all.
At first, I tried this:
const typePolicy = {
keyArgs: [
"search",
[
"identifier",
"searchTerm",
"includePartialMatches",
"filters",
"orderBy",
"facets",
],
],
// Concatenate the incoming list items with
// the existing list items.
merge(existing: any, incoming: any) {
console.log("existing", existing);
console.log("incoming", incoming);
if (!existing?.items) console.log("--------------");
if (!existing?.items) return { ...incoming }; // First request
const items = existing.items.concat(incoming.items);
const item = { ...existing, ...incoming };
item.items = items;
console.log("merged", item);
console.log("--------------");
return item;
},
};
But this does not do what I want.
What I would like, is for apollo to work as it does normally, but when the "page" changes for any field, it appends it instead of caching a new request.
Does anyone know what I am doing wrong or can provide me with a better example that what is on the documentation?

Is there a way to get a structure of a Strapi CMS Content Type?

A content-type "Product" having the following fields:
string title
int qty
string description
double price
Is there an API endpoint to retrieve the structure or schema of the "Product" content-type as opposed to getting the values?
For example: On endpoint localhost:1337/products, and response can be like:
[
{
field: "title",
type: "string",
other: "col-xs-12, col-5"
},
{
field: "qty",
type: "int"
},
{
field: "description",
type: "string"
},
{
field: "price",
type: "double"
}
]
where the structure of the schema or the table is sent instead of the actual values?
If not in Strapi CMS, is this possible on other headless CMS such as Hasura and Sanity?
You need to use Models, from the link:
Link is dead -> New link
Models are a representation of the database's structure. They are split into two separate files. A JavaScript file that contains the model options (e.g: lifecycle hooks), and a JSON file that represents the data structure stored in the database.
This is exactly what you are after.
The way I GET this info is by adding a custom endpoint - check my answers here for how to do this - https://stackoverflow.com/a/63283807/5064324 & https://stackoverflow.com/a/62634233/5064324.
For handlers you can do something like:
async getProductModel(ctx) {
return strapi.models['product'].allAttributes;
}
I needed the solution for all Content Types so I made a plugin with /modelStructure/* endpoints where you can supply the model name and then pass to a handler:
//more generic wrapper
async getModel(ctx) {
const { model } = ctx.params;
let data = strapi.models[model].allAttributes;
return data;
},
async getProductModel(ctx) {
ctx.params['model'] = "product"
return this.getModel(ctx)
},
//define all endpoints you need, like maybe a Page content type
async getPageModel(ctx) {
ctx.params['model'] = "page"
return this.getModel(ctx)
},
//finally I ended up writing a `allModels` handler
async getAllModels(ctx) {
Object.keys(strapi.models).forEach(key => {
//iterate through all models
//possibly filter some models
//iterate through all fields
Object.keys(strapi.models[key].allAttributes).forEach(fieldKey => {
//build the response - iterate through models and all their fields
}
}
//return your desired custom response
}
Comments & questions welcome
This answer pointed me in the right direction, but strapi.models was undefined for me on strapi 4.4.3.
What worked for me was a controller like so:
async getFields(ctx) {
const model = strapi.db.config.models.find( model => model.collectionName === 'clients' );
return model.attributes;
},
Where clients is replaced by the plural name of your content-type.

How to properly format data with AppSync and DynamoDB when Lambda is in between

Receiving data with AppSync directly from DynamoDB seems working for my case, but when I try to put a lambda function in between, I receive errors that says "Can't resolve value (/issueNewMasterCard/masterCards) : type mismatch error, expected type LIST"
Looking to the AppSync cloudwatch response mapping output, I get this:
"context": {
"arguments": {
"userId": "18e946df-d3de-49a8-98b3-8b6d74dfd652"
},
"result": {
"Item": {
"masterCards": {
"L": [
{
"M": {
"cardId": {
"S": "95d67f80-b486-11e8-ba85-c3623f6847af"
},
"cardImage": {
"S": "https://s3.eu-central-1.amazonaws.com/logo.png"
},
"cardWallet": {
"S": "0xFDB17d12057b6Fe8c8c434653456435634565"
},...............
here is how I configured my response mapping template:
$utils.toJson($context.result.Item)
I'm doing this mutation:
mutation IssueNewMasterCard {
issueNewMasterCard(userId:"18e946df-d3de-49a8-98b3-8b6d74dfd652"){
masterCards {
cardId
}
}
}
and this is my schema :
type User {
userId: ID!
masterCards: [MasterCard]
}
type MasterCard {
cardId: String
}
type Mutation {
issueNewMasterCard(userId: ID!): User
}
The Lambda function:
exports.handler = (event, context, callback) => {
const userId = event.arguments.userId;
const userParam = {
Key: {
"userId":{S:userId}
},
TableName:"FidelityCardsUsers"
}
dynamoDB.getItem(userParam, function(err, data) {
if (err) {
console.log('error from DynamDB: ',err)
callback(err);
} else {
console.log('mastercards: ',JSON.stringify(data));
callback(null,data)
}
})
I think the problem is that the getItem you use when you use the DynamoDB datasource is not the same as the the DynamoDB.getItem function in the aws-sdk.
Specifically it seems like the datasource version returns an already marshalled response (that is, instead of something: { L: [ list of things ] } it just returns something: [ list of things]).
This is important, because it means that $utils.toJson($context.result.Item) in your current setup is returning { masterCards: { L: [ ... which is why you are seeing the type error- masterCards in this case is an object with a key L, rather than an array/list.
To solve this in the resolver, you can use the $util.dynamodb.toDynamoDBJson(Object) macro (https://docs.aws.amazon.com/appsync/latest/devguide/resolver-util-reference.html#dynamodb-helpers-in-util-dynamodb). i.e. your resolver should be:
$util.dynamodb.toDynamoDBJson($context.result.Item)
Alternatively you might want to look at the AWS.DynamoDB.DocumentClient class (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html). This includes versions of getItem, etc. that automatically marshal and unmarshall the proprietary DynamoDB typing back into native JSON. (Frankly I find this much nicer to work with and use it all the time).
In that case you can keep your old resolver, because you'll be returning an object where masterCards is just a JSON array.

In FormFlow, How do i get the form values after form is done?

I have a complete Form that has different fields. Name, phone and so on.
Before complition, I would like to send the fields to a method then then sends an email. More specificly, I want to put the values on a dictionary and then pass it to the method.
Where are those values stored in the Form so I can get them?
This is my code
form.OnCompletion(processOrder);
var parameters = new Dictionary<string, string>
{
{ "tileid", "open" },
{ "src", "Facebook" },
{ "chid", "9" },
{ "apply-first-name", "xxx" },
{ "apply-last-name", "xxx" },
{ "apply-email", "xxx" }
};
sendAsync(parameters);
return form.Build();
I found the answer. All values are inside the ”state” objekt passed to the oncompletion method.

is there a way to deserialize Elasticsearch Nest search query?

upon building my Elasticsearch query using Nest, i want to be able to see the JSON version of what's being sent to Elasticsearch. is this possible? some sort of deserializer i suppose.
here's the info for my follow up question:
{
"_infer": {
"defaultIndex": "myindex"
},
"acknowledged": false,
"isValid": false,
"connectionStatus": {
"success": false,
"requestMethod": "POST",
"requestUrl": "http://localhost:9200/myindex",
"settings": {},
"request": "ew0KICAic2V0dGluZ3MiOiB7DQogICAgImluZGV4Ijogew0KICAgICAgImFuYWx5c2lzIjogew0KICAgICAgICAiYW5hbHl6ZXIiOiB7DQogICAgICAgICAgImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCIsDQogICAgICAgICAgICAgICJsb3dlcmNhc2UiDQogICAgICAgICAgICBdLA0KICAgICAgICAgICAgInR5cGUiOiAiY3VzdG9tIg0KICAgICAgICAgIH0sDQogICAgICAgICAgImZ1bGxUZXJtIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJrZXl3b3JkIiwNCiAgICAgICAgICAgICJmaWx0ZXIiOiBbDQogICAgICAgICAgICAgICJzdGFuZGFyZCINCiAgICAgICAgICAgIF0sDQogICAgICAgICAgICAidHlwZSI6ICJjdXN0b20iDQogICAgICAgICAgfSwNCiAgICAgICAgICAiY2FzZUluc2Vuc2l0aXZlIjogew0KICAgICAgICAgICAgInRva2VuaXplciI6ICJsb3dlcmNhc2UiLA0KICAgICAgICAgICAgImZpbHRlciI6IFsNCiAgICAgICAgICAgICAgInN0YW5kYXJkIg0KICAgICAgICAgICAgXSwNCiAgICAgICAgICAgICJ0eXBlIjogImN1c3RvbSINCiAgICAgICAgICB9DQogICAgICAgIH0NCiAgICAgIH0NCiAgICB9DQogIH0sDQogICJtYXBwaW5ncyI6IHsNCiAgICAic2VhcmNoZGF0YSI6IHsNCiAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAidXBjIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgInByb2R1Y3RpZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ0aXRsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdG9yZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJtZWRpYWZvcm1hdCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAibGFiZWwiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAidmVuZG9yIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImF2YWlsYWJsZWRhdGUiOiB7DQogICAgICAgICAgInR5cGUiOiAiZGF0ZSINCiAgICAgICAgfSwNCiAgICAgICAgInJlbGVhc2VkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJsYXN0cmV0dXJuZGF0ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkYXRlIg0KICAgICAgICB9LA0KICAgICAgICAibm9ucmV0dXJuYWJsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJtcGFhIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImVzcmIiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAiZXNyYiI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiY3NucmZsYWciOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiY292ZXIiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAic2l6ZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJjb2xvciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzdHlsZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJxb2giOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImFkdWx0Ijogew0KICAgICAgICAgICJ0eXBlIjogImludGVnZXIiDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc3JhbmsiOiB7DQogICAgICAgICAgInR5cGUiOiAiaW50ZWdlciINCiAgICAgICAgfSwNCiAgICAgICAgImxpc3RwcmljZSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJkb3VibGUiDQogICAgICAgIH0sDQogICAgICAgICJzZWxsaW5ncHJpY2UiOiB7DQogICAgICAgICAgInR5cGUiOiAiZG91YmxlIg0KICAgICAgICB9LA0KICAgICAgICAiY29zdHByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogImRvdWJsZSINCiAgICAgICAgfSwNCiAgICAgICAgInNwZWNpYWxvcmRlciI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJmbGFncyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJzYWxlc2NhdGVnb3J5Ijogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyINCiAgICAgICAgfSwNCiAgICAgICAgImFydGlzdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJkaXJlY3RvcnMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJzdGFycmluZyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJvYmplY3QiLA0KICAgICAgICAgICJwcm9wZXJ0aWVzIjogew0KICAgICAgICAgICAgIm5hbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybSIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgImlkIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgImNyZWRpdHMiOiB7DQogICAgICAgICAgInR5cGUiOiAib2JqZWN0IiwNCiAgICAgICAgICAicHJvcGVydGllcyI6IHsNCiAgICAgICAgICAgICJuYW1lIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm0iLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJpZCI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJhZGRpdGlvbmFsdGl0bGVzIjogew0KICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgImFuYWx5emVyIjogImNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgfSwNCiAgICAgICAgImF0dHJpYnV0ZXMiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAic3BlY2lhbHR5Ijogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInNwZWNpYWx0eSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAiZ2VucmVzIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgImdlbnJlcyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9LA0KICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAidGhlbWUiOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJhbmFseXplciI6ICJmdWxsVGVybUNhc2VJbnNlbnNpdGl2ZSINCiAgICAgICAgICAgIH0sDQogICAgICAgICAgICAicmF3Ijogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiaW5kZXgiOiAibm90X2FuYWx5emVkIg0KICAgICAgICAgICAgfQ0KICAgICAgICAgIH0NCiAgICAgICAgfSwNCiAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICJ0eXBlIjogIm11bHRpX2ZpZWxkIiwNCiAgICAgICAgICAiZmllbGRzIjogew0KICAgICAgICAgICAgInByaWNlIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJ0cmFja3MiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAiYW5hbHl6ZXIiOiAiY2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICB9LA0KICAgICAgICAiZGVsZXRlZCI6IHsNCiAgICAgICAgICAidHlwZSI6ICJzdHJpbmciDQogICAgICAgIH0sDQogICAgICAgICJ1cGRhdGVkYXRlIjogew0KICAgICAgICAgICJ0eXBlIjogImRhdGUiDQogICAgICAgIH0sDQogICAgICAgICJwcm9jZXNzZWQiOiB7DQogICAgICAgICAgInR5cGUiOiAic3RyaW5nIg0KICAgICAgICB9LA0KICAgICAgICAiZGVjYWRlcyI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJkZWNhZGVzIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwZXJpb2QiOiB7DQogICAgICAgICAgInR5cGUiOiAibXVsdGlfZmllbGQiLA0KICAgICAgICAgICJmaWVsZHMiOiB7DQogICAgICAgICAgICAicGVyaW9kIjogew0KICAgICAgICAgICAgICAidHlwZSI6ICJzdHJpbmciLA0KICAgICAgICAgICAgICAiYW5hbHl6ZXIiOiAiZnVsbFRlcm1DYXNlSW5zZW5zaXRpdmUiDQogICAgICAgICAgICB9LA0KICAgICAgICAgICAgInJhdyI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImluZGV4IjogIm5vdF9hbmFseXplZCINCiAgICAgICAgICAgIH0NCiAgICAgICAgICB9DQogICAgICAgIH0sDQogICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAidHlwZSI6ICJtdWx0aV9maWVsZCIsDQogICAgICAgICAgImZpZWxkcyI6IHsNCiAgICAgICAgICAgICJwbGF0Zm9ybSI6IHsNCiAgICAgICAgICAgICAgInR5cGUiOiAic3RyaW5nIiwNCiAgICAgICAgICAgICAgImFuYWx5emVyIjogImZ1bGxUZXJtQ2FzZUluc2Vuc2l0aXZlIg0KICAgICAgICAgICAgfSwNCiAgICAgICAgICAgICJyYXciOiB7DQogICAgICAgICAgICAgICJ0eXBlIjogInN0cmluZyIsDQogICAgICAgICAgICAgICJpbmRleCI6ICJub3RfYW5hbHl6ZWQiDQogICAgICAgICAgICB9DQogICAgICAgICAgfQ0KICAgICAgICB9DQogICAgICB9DQogICAgfQ0KICB9DQp9",
"numberOfRetries": 0,
"httpStatusCode": 400,
"serializer": {},
"successOrKnownError": true
},
"infer": {
"defaultIndex": "myindex"
}
}
Yup. You can use the serializer that is exposed by ElasticClient like this:
var query = Nest.Query<SomeObject>...
var json = Client.Serializer.SerializeToString(query);
You can also use Newtonsoft directly, or any JSON library for that matter to serialize your query object. Using the serializer in ElasticClient though (which essentially wraps Newtonsoft), will give you the exact JSON that NEST will generate.
Checkout how the unit tests are done in NEST for more details, specifically this.
I used SearchDescriptor like this for a complex query:
SearchDescriptor<T> sd = new SearchDescriptor<T>()
.From(0).Size(100)
.Query(q => q
.Bool(t => t
.Must(u => u
.Bool(v => v
.Should(
...
)
)
)
)
);
And got the deserialized JSON like this:
{
"from": 0,
"size": 100,
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
...
]
}
}
]
}
}
}
It was annoying, NEST library should have something that spits out the JSON from request. However this worked for me:
using (MemoryStream mStream = new MemoryStream()) {
client.Serializer.Serialize(sd, mStream);
Console.WriteLine(Encoding.ASCII.GetString(mStream.ToArray()));
}
NEST library version: 2.0.0.0.
Newer version may have an easier method to get this (Hopefully).
If the above answer doesn't work, or you want a simpler one, I posted another way here on what is probably a duplicate, (but differently worded) question:
https://stackoverflow.com/a/31247636/261405
Add this to your WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
...
// ignore circular reference globally
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
...
}
Then you should be able to do something like this:
// Return raw
var esObj = await _client.SearchAsync<object>(searchRequest);
// more specific deserialization using Newtonsoft.Json example
foreach (var hit in esObj.Hits)
{
var myObj = JsonConvert.DeserializeObject<MyObject>(hit.xxx);
...
}
For Nest 5.3.0 :
var memoryStream = new System.IO.MemoryStream();
client.Serializer.Serialize(query, memoryStream);
var jsonQuery = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());

Resources