data store in the DB:
{
"_id": "61c1c0efc204bb170e280d2f",
"title": "Searching Relevant Cases",
"question": "Your Industry",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c142c204bb170e280d45",
"title": "Searching Relevant Cases",
"question": "Employee size",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "none",
"view": "list_view",
"is_active": true,
"is_delete": false,
"sub_question": [],
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
},
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
},
{
"_id": "61c1c2d7c204bb170e280d7e",
"title": "Dependent 1",
"question": "On previous Chosen option 1",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c2d7c204bb170e280d7f",
"answer": "Mid (100 - 1000)"
},
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c2d7c204bb170e280d80",
"answer": "Tech"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
}
Query parameters are:
obj: {
"question_id": "61c1c0efc204bb170e280d2f",
"answer": "Bank"
},
obj1: {
"question_id": "61c1c142c204bb170e280d45",
"answer": "SMB (<100)"
}
The result I'm expecting is:
{
"_id": "61c1c24ac204bb170e280d5e",
"title": "Dependent",
"question": "On previous Chosen option",
"vendor_id": "61b8324040fb21d80f3e6702",
"answer_type": "is_radio",
"optional_answer": false,
"sub_question_type": "has_parent",
"sub_question": [
{
"question_id": "61c1c0efc204bb170e280d2f",
"_id": "61c1c24ac204bb170e280d5f",
"answer": "Bank"
},
{
"question_id": "61c1c142c204bb170e280d45",
"_id": "61c1c24ac204bb170e280d60",
"answer": "SMB (<100)"
}
],
"view": "list_view",
"is_active": true,
"is_delete": false,
"draggable_list": [],
"__v": 0
}
The answer what I get from the user (user:14732669) on StackOverflow is:
https://mongoplayground.net/p/pAV76ctwhfC
this query is working but not returning the resulted object instead it returns two objects.
I have to match the question_id as well as the answer of (obj and obj1) for both the objects which are in the sub_question array
$match
db.collection.aggregate([
{
"$match": {
"$and": [
{
"sub_question.question_id": "61c1c0efc204bb170e280d2f",
"sub_question.answer": "Bank"
},
{
"sub_question.question_id": "61c1c142c204bb170e280d45",
"sub_question.answer": "SMB (<100)"
}
]
}
}
])
mongoplayground
Related
I'm using Azure Search with a scoring profile. I need text fields along with quantity sold to be a part of the scoring profile. I can configure the following profile, but the quantity sold doesn't seem to be factored in to the search score when I query the index. I'm thinking because quantity sold isn't a string, its an int. Therefore, I can't make the field searchable? I'm using the new featuresMode parameter in the query, the quantity sold field doesn't even appear in the scoring breakdown
"scoringProfiles": [
{
"name": "Product Name",
"functions": [
{
"fieldName": "QuantitySold",
"freshness": null,
"interpolation": "linear",
"magnitude": {
"boostingRangeStart": 0,
"boostingRangeEnd": 100000,
"constantBoostBeyondRange": true
},
"distance": null,
"tag": null,
"type": "magnitude",
"boost": 6
}
],
"functionAggregation": "sum",
"text": {
"weights": {
"ProductName": 4,
"ProductSet": 3,
"ProductDesc": 2
}
}
}
],
What type of boost you should use depends on the datatype. An int like QuantitySold should use type magnitude for boosting. A date would use type freshness and so on.
I recreated a minimal example with the simplest possible index with only two properties: Id and Title.
CREATE INDEX
{
"#odata.context": "https://{{SEARCH_SVC}}.{{DNS_SUFFIX}}/$metadata#indexes/$entity",
"#odata.etag": "\"0x8D8761DCBBCCD00\"",
"name": "{{INDEX}}",
"defaultScoringProfile": null,
"fields": [
{
"name": "Id",
"type": "Edm.String",
"facetable": false,
"filterable": true,
"key": true,
"retrievable": true,
"searchable": true,
"sortable": true,
"analyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
},
{
"name": "Title",
"type": "Edm.String",
"facetable": false,
"filterable": true,
"key": false,
"retrievable": true,
"searchable": true,
"sortable": true,
"analyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
},
{
"name": "QuantitySold",
"type": "Edm.Int32",
"facetable": true,
"filterable": true,
"key": false,
"retrievable": true,
"searchable": false,
"sortable": true,
"analyzer": null,
"searchAnalyzer": null,
"synonymMaps": [],
"fields": []
}
], "scoringProfiles": [
{
"name": "relevance",
"text": {
"weights": {
"Title": 1.5
}
}
},
{
"name": "sales",
"functions": [
{
"type": "magnitude",
"fieldName": "QuantitySold",
"boost": 100,
"interpolation": "linear",
"magnitude": {
"boostingRangeStart": 1,
"boostingRangeEnd": 100000,
"constantBoostBeyondRange": false
}
}
]
} ],"corsOptions": null, "suggesters": [], "analyzers": [], "tokenizers": ], "tokenFilters": [], "charFilters": [], "encryptionKey": null}
UPLOAD MINIMAL
I then submit two products. One called Apple iPhone with a low sales quantity. And Apple Juice with a high sales quantity.
{
"value": [
{
"#search.action": "mergeOrUpload",
"Id": "2",
"Title": "Apple Juice",
"QuantitySold": 10000
},
{
"#search.action": "mergeOrUpload",
"Id": "1",
"Title": "Apple iPhone",
"QuantitySold": 35
}
]
}
QUERY
Without using any scoring profile, I query for apple. As expected, the two items are equally relevant responses to my query. They both match one of two tokens. Both get score 0.25811607
https://{{SEARCH_SVC}}.{{DNS_SUFFIX}}/indexes/{{INDEX}}/docs?search=apple&$count=true&searchMode=all&queryType=full&api-version={{API-VERSION}}&featuresMode=enabled
{
"#odata.count": 2,
"value": [
{
"#search.score": 0.25811607,
"#search.features": {
"Title": {
"uniqueTokenMatches": 1.0,
"similarityScore": 0.25811607,
"termFrequency": 1.0
}
},
"Id": "2",
"Title": "Apple Juice",
"QuantitySold": 10000
},
{
"#search.score": 0.25811607,
"#search.features": {
"Title": {
"uniqueTokenMatches": 1.0,
"similarityScore": 0.25811607,
"termFrequency": 1.0
}
},
"Id": "1",
"Title": "Apple iPhone",
"QuantitySold": 35
}
]
}
QUERY WITH BOOST ON QUANTITY SOLD
I then repeat the query for apple, but this time I boost items with a high QuantitySold by selecting my scoring profile called sales. This boosts our Apple Juice item to the top with a score of 2.8123235. The Apple iPhone item has also received a boost, but much smaller with only a score of 0.26680434.
https://{{SEARCH_SVC}}.{{DNS_SUFFIX}}/indexes/{{INDEX}}/docs?search=apple&$count=true&searchMode=all&queryType=full&api-version={{API-VERSION}}&featuresMode=enabled&scoringProfile=sales
{
"#odata.count": 2,
"value": [
{
"#search.score": 2.813235,
"#search.features": {
"Title": {
"uniqueTokenMatches": 1.0,
"similarityScore": 0.25811607,
"termFrequency": 1.0
}
},
"Id": "2",
"Title": "Apple Juice",
"QuantitySold": 10000
},
{
"#search.score": 0.26680434,
"#search.features": {
"Title": {
"uniqueTokenMatches": 1.0,
"similarityScore": 0.25811607,
"termFrequency": 1.0
}
},
"Id": "1",
"Title": "Apple iPhone",
"QuantitySold": 35
}
]
}
Good morning, can anyone advise me? I'm pretty clueless. I've already spent a few hours on it and I don't know how to solve this.
The data is fictitious and the original JSON is far more complex.
JSON
{
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
How can I get from main -> content -> items -> images -> url?
More precisely each images and from that url.
Thank you very much in advance for your help.
It looks like your main array has an array as 1st element. If that is correct, you take the first element of main, and then loop over that.
let jsonObj = {
"main": [
[
{
"type": "dasdasdasd",
"id": 5,
"content": {
"title": "adadadsad",
"items": [
{
"date": "2012-02-02T11:23:00Z",
"id": 12,
"name": "test",
"isEnabled": false,
"isHighlited": false,
"images": {
"lists": {
"small": [
{
"id": 18,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"original": [
{
"id": 19,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"large": [
{
"id": 22,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
],
"medium": [
{
"id": 23,
"position": 0,
"titleImage": true,
"url": "",
"thumbnailReady": true
}
]
}
},
"enum": "LINIE",
"url": "https://test.com"
}
]
}
}
]
],
"second": [
]
}
let smallImg = "", originalImg = "", largeImg = "", mediumImg = "";
jsonObj.main[0].forEach(mainItem => {
mainItem.content.items.forEach(item => {
smallImg = item.images.lists.small[0].url;
originalImg = item.images.lists.original[0].url;
largeImg = item.images.lists.large[0].url;
mediumImg = item.images.lists.medium[0].url;
});
});
console.log(smallImg);
console.log(originalImg);
console.log(largeImg);
console.log(mediumImg);
This is my first attempt at parsing nested JSON with Ruby. I need to go through the JSON to pull out specific values for "_id", "name", and "type" for instance. I then need to create a reference table so that I can refer to each "_id" and associated information. I also need to combine information from multiple JSON responses. I've been able to get basic information and have tried a few things I've found online. I just need a little assistance with a starting point. If anyone has any ideas of where to start with this I'd really appreciate it.
Devices JSON response hash. Each device starts with _id.
{
"api": "1.0",
"error": null,
"id": "60b5d4c3077862123cfa4443",
"result": {
"devices": [
{
"_id": "123456787786211fd31f3dd",
"batteryPowered": true,
"category": "door_lock",
"deviceTypeId": "144_1_1",
"firmware": [
{
"id": "us.144.1_1.0",
"version": "2.6"
}
],
"gatewayId": "1234567807786214fbc6bd4e",
"info": {
"firmware.stack": "3.28",
"hardware": "0",
"manufacturer": "Kwikset",
"model": "912",
"protocol": "zwave",
"zwave.node": "2",
"zwave.smartstart": "no"
},
"name": "Garage Door",
"parentDeviceId": "",
"persistent": false,
"reachable": false,
"ready": true,
"roomId": "1234567807786211fd31f3eb",
"security": "middle",
"status": "idle",
"subcategory": "",
"type": "doorlock"
},
{
"_id": "1234567897786211fd31f3ed",
"batteryPowered": true,
"category": "door_lock",
"deviceTypeId": "59_1_1129",
"firmware": [
{
"id": "us.59.18064.0",
"version": "3.3"
},
{
"id": "us.59.18065.1",
"version": "11.0"
}
],
"gatewayId": "1234567897786214fbc6bd4e",
"info": {
"firmware.stack": "6.3",
"hardware": "3",
"manufacturer": "Schlage",
"model": "BE469ZP",
"protocol": "zwave",
"zwave.node": "3",
"zwave.smartstart": "no"
},
"name": "Front Door",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f3ec",
"security": "high",
"status": "idle",
"subcategory": "",
"type": "doorlock"
},
{
"_id": "1234567897786211fd31f40a",
"batteryPowered": false,
"category": "switch",
"deviceTypeId": "57_20562_12344",
"firmware": [
{
"id": "us.57.29240.0",
"version": "5.25"
}
],
"gatewayId": "1234567807786214fbc6bd4e",
"info": {
"firmware.stack": "4.54",
"hardware": "255",
"manufacturer": "Honeywell",
"model": "ZW4103/39337",
"protocol": "zwave",
"zwave.node": "4",
"zwave.smartstart": "no"
},
"name": "Lamp Switch",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f416",
"security": "no",
"status": "idle",
"subcategory": "interior_plugin",
"type": "switch.outlet"
},
{
"_id": "1234567b07786211fd31f40e",
"batteryPowered": false,
"category": "dimmable_light",
"deviceTypeId": "57_20548_12339",
"firmware": [
{
"id": "us.57.29747.0",
"version": "5.21"
}
],
"gatewayId": "1234567d07786214fbc6bd4e",
"info": {
"firmware.stack": "4.34",
"hardware": "255",
"manufacturer": "Honeywell",
"model": "39339/ZW3107",
"protocol": "zwave",
"zwave.node": "5",
"zwave.smartstart": "no"
},
"name": "Lamp Dimmer",
"parentDeviceId": "",
"persistent": false,
"reachable": true,
"ready": true,
"roomId": "1234567807786211fd31f416",
"security": "no",
"status": "idle",
"subcategory": "dimmable_plugged",
"type": "dimmer.outlet"
}
]
}
}
There is then also a JSON response that lists the functions for each device in the same format above. However instead of "devices"=> it is "items"=> and the beach function is the _id key again.
I'd like to combine function _id tags and descriptions with the device JSON, so I can create a way to send my script "unlock door lock 1" and it subs the number with the _id of the device and the function _id.
You can start with a very rough navigator function like this:
def find_device(data, name, index)
# Filter through the device list...
data['result']['devices'].select do |device|
# ...for matching names.
device.name == name
end[index] # Take indexed entry
end
Where now you can do find_device(data, 'door_lock', 0) to dig up that entry.
Converting "door lock 1" to [ 'door_lock', 0 ] should be pretty trivial:
def to_location(str)
# Split off the name component(s) and index number
*name, index = str.split(/\s+/)
# Reassemble with underscores and -1 to account for 0-index
[ name.join('_'), index.to_i - 1 ]
end
I would like to ensure that json objects within a json array are ordered correctly by a specific property with a json schema.
Is that possible? And if so, how can I create such a json schema?
Schema:
{
"type": "object",
"properties": {
"cities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"shortName": {
"type": "string"
},
"name": {
"type": "string"
},
"showInMap": {
"type": "boolean"
},
"active": {
"type": "boolean"
}
},
"??ORDERBY??": "shortName",
"??ORDER??": "ASC",
"required": [
"shortName"
]
}
}
}
}
I would like to filter out json files which are not correctly ordered.
Example: (invalid)
{
"cities": [
{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
},
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}
]
}
And accept json files which are ordered correctly.
Example: (valid)
{
"cities": [
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
},{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
}
]
}
EDIT: This answer makes no use of json schema.
Here is a minimal solution working on your sample data. It is not safe for production, as it is missing all sorts of checks for undefined, but I think you can enhance it at will.
function isSorted(array, sortKey) {
return array.reduce((ordered, item, index) => {
return index > array.length - 2 ? ordered : ordered && item[sortKey] < array[index + 1][sortKey];
}, true);
}
const incorrectCase = [{
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
},
{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}
]
const correctCase = [{
"shortName": "LD",
"name": "London",
"showInMap": true,
"active": false
},
{
"shortName": "MO",
"name": "Moscow",
"showInMap": false,
"active": false
}, {
"shortName": "NY",
"name": "New York",
"showInMap": true,
"active": true
}
];
console.log('incorrect case: ', isSorted(incorrectCase, "shortName"));
console.log('correct case: ', isSorted(correctCase, "shortName"));
I have 2 objects
{
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
},
and
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Object one defines what a field should be and what it is called
The second object is a job description.
What i need is to match a field to each key (this even sounds confusing i my head, so here is an example)
{
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true,
"value": "THIS IS MY REF"
}
},
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
},
Here you go:
var def = {
"_id": "58b7f36b3354c24630f6f3b0",
"name": "refcode",
"caption": "Reference",
"type": "string",
"search": false,
"required": false,
"table": true,
"expansion": true
};
var jobDesc = {
"_id": "58b7f36b3354c24630f6f3c8",
"vacancyid": "0",
"refcode": "THIS IS MY REF",
"position": "Test",
"jobtype": "Temp",
"department": "Industrial",
"branch": "Office",
"startdate": "02/12/2013",
"contactname": "Person Name",
"contactemail": "person#domain",
"Q_V_TYP": "Daily",
"score": 0
};
var jobDescKeysArr = Object.keys(jobDesc);
if (jobDescKeysArr.indexOf(def.name) !== -1) {
// A match.
def.value = jobDesc[def.name];
jobDesc[def.name] = Object.assign({}, def);
console.log(jobDesc)
}