How to loop through array in watson? - ibm-watson

I am trying to show the Options on Watson Assistant which are dynamic from an array. How can I loop through the array from the context and display it in the output/response in the form of Options?
{
"output": {
"generic": [
{
"title": "This is a test",
"options": [
{
"label": "<? $my_options_array[0].label ?>",
"value": {
"input": {
"text": "<? $my_options_array[0].label ?>"
}
}
},
{
"label": "<? $my_options_array[1] ?>",
"value": {
"input": {
"text": "<? $my_options_array[1] ?>"
}
}
}
],
"response_type": "option"
}
]
},
"context": {
"my_options_array": "<? $availableWorkFlows.split(',') ?>"
}
}

Related

Add and update child arrays value in Mongo db

I want to add value in array of the following json structure in mongo db collection "Scdtls".
I need to add value only where "Type": "Fees" and "IsCurrentVersion": true.
{
"_id": ObjectId("60e71243b484cf3ec22a6845"),
"Item": "School",
"Text": "School Details",
"Types": [
{
"Type": "Library",
"Values": [
{
"IsCurrentVersion": false,
"KeyWords": [
{
"_id": ObjectId("611a4f113800d6af3fc4814f"),
"Value": "Physics"
}
]
}
]
},
{
"Type": "Fees",
"Values": [
{
"IsCurrentVersion": false,
"KeyWords": [
{
"_id": ObjectId("611a4f113800d6af3fc4814f"),
"Value": "Admission"
},
{
"_id": ObjectId("611a4f113800d6af3fc4814k"),
"Value": "Canteen"
}
]
},
{
"IsCurrentVersion": true,
"KeyWords": [
{
"_id": ObjectId("611a4f113800d6af3fc4814g"),
"Value": "Tution"
}
]
}
]
}
]
}
This is how I am trying
db.Scdtls.update({
"Item": "School",
"Types.Type": "Fees",
"Types.Values.IsCurrentVersion": true
},
{
"$push": {
"Types.1.Values.$[].KeyWords": {
"_id": ObjectId(),
"Value": "Sports"
}
}
})
But it is also adding "Value": "Sports" in "Type" : "Fees" where "IsCurrentVersion": false.
Not able to understand the reason.
If I understand correctly, you can do the update you want using "arrayFilters", like this:
db.Scdtls.update({
"Item": "School",
"Types.Type": "Fees",
"Types.Values.IsCurrentVersion": true
},
{
"$push": {
"Types.$[x].Values.$[y].KeyWords": {
"_id": ObjectId("deadbeafcafebabec0deface"),
"Value": "Sports"
}
}
},
{
"arrayFilters": [
{ "x.Type": "Fees" },
{ "y.IsCurrentVersion": true }
]
})
Try it on mongoplayground.net.

Jolt for input array to name value pair of array

Input is :
{
"Size": "2",
"done": "true",
"records": [
{
"Id": "a7g6s0000004GZuAAM",
"NN": "00096411.0",
"Name": "ISOLIN TRADE & INVEST"
},
{
"Id": "a7g6s0000004GZzAAM",
"Number": "00096412.0",
"Name": "ISOLIN"
}
]
}
Spec used:
[
{
"operation": "remove",
"spec": {
"records": {
"*": {
"attributes": " "
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": { //iterate on each object of records
"*": { //iterate on each element of object
"$": "Items[#1].Fields[].Name",
"#": "Items[#1].Fields[].Value"
}
}
}
}
}
]
Current Output:
{
"Size": "2",
"done": "true",
"Items": [
{
"Fields": [
{
"Name": "Id"
},
{
"Value": "a7g6s0000004GZuAAM"
},
{
"Name": "NN"
},
{
"Value": "00096411.0"
},
{
"Name": "Name"
},
{
"Value": "ISOLIN TRADE & INVEST"
},
{
"Name": "Id"
},
{
"Value": "a7g6s0000004GZzAAM"
},
{
"Name": "Number"
},
{
"Value": "00096412.0"
},
{
"Name": "Name"
},
{
"Value": "ISOLIN"
}
]
}
]
}
Expected output:
{
"Size": "2",
"done": "true",
"Items": [
{
"Fields": [
{
"Name": "Id",
"Value": "a7g6s0000004GZuAAM"
},
{
"Name": "NN",
"Value": "00096411.0"
},
{
"Name": "Name",
"Value": "ISOLIN TRADE & INVEST"
},
{
"Name": "Id",
"Value": "a7g6s0000004GZzAAM"
},
{
"Name": "Number",
"Value": "00096412.0"
},
{
"Name": "Name",
"Value": "ISOLIN"
}
]
}
]
}
You can seperate those attributes into individual objects through use of Items.&2.[#2]. pattern as prefix such as
[
{
"operation": "shift",
"spec": {
"*": "&",
"records": {
"*": {
"*": {
"$": "Items.&2.[#2].Name",
"#": "Items.&2.[#2].Value"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"Items": {
"*": {
"*": "Fields[]"
}
}
}
}
]

JOLT remove list levels in nested JSON

I have JSON files where the elements are embeded in lists, with unnecesary sub-levels. I need to simplify this structure with JOLT but I can't figure out how. I looked at similar Q&As but I didn't find examples that would be similar enough.
Here is the input:
{
"datasets": [{
"datasetid": "id_of_1st_dataset",
"metas": {
"domain": "mydomain",
"records_count": 120234,
"keywords": ["foo", "bar"]
},
"fields": [
{
"name": "1st_field_of_1st_dataset",
"type": "text"
},
{
"name": "2nd_field_of_1st_dataset",
"type": "int"
}
]
},
{
"datasetid": "id_of_2nd_dataset",
"metas": {
"domain": "mydomain",
"records_count": 5402,
"keywords": ["banana", "bar"]
},
"fields": [
{
"name": "1st_field_of_2nd_dataset",
"type": "text"
},
{
"name": "2nd_field_of_2nd_dataset",
"type": "int"
}
]
}
]
}
And the desired output:
{
"datasetid": "id_of_1st_dataset",
"domain": "mydomain",
"records_count": 120234,
"keywords": ["foo", "bar"]
"fields": [
{
"name": "1st_field_of_1st_dataset",
"type": "text"
},
{
"name": "2nd_field_of_1st_dataset",
"type": "int"
}
]
},
{
"datasetid": "id_of_2nd_dataset",
"domain": "mydomain",
"records_count": 5402,
"keywords": ["banana", "bar"]
"fields": [
{
"name": "1st_field_of_2nd_dataset",
"type": "text"
},
{
"name": "2nd_field_of_2nd_dataset",
"type": "int"
}
]
}
What would be the right jolt SPEC to transform this input into this output? Thanks in advance for your attention and best regards.
Can be done with the below spec,
[
{
"operation": "shift",
"spec": {
"datasets": {
"*": {
"datasetid": "[&1].datasetid",
"fields": "[&1].fields",
"metas": {
"*": {
"#": "[&3].&"
}
}
}
}
}
}
]

JOLT filter array elements based on existence of a key field

Is it possible to filter array elements based on presence of a field.
My input JSON is as below:
{
"payload": {
"logical": {
"schemas": [
{
"name": "myschema",
"tables": [
{
"name": "myname",
"alias": "temp_alias",
"keys": [
{
"name": "value1",
"key": "key1",
"match": "match_val"
},
{
"name": "value1",
"key": "key2",
"match": "match_val"
},
{
"name": "value1",
"key": "key3"
},
{
"name": "value1",
"key": "key4"
}
]
}
]
}
]
}
}
}
Expected output is:
{
"payload": {
"logical": {
"schemas": [
{
"tables": [
{
"name": "myname",
"alias": "temp_alias",
"keys": {
"name": "value1",
"match": "match_val",
"key": [
"key1",
"key2"
]
}
}
]
}
]
}
}
}
If there is "match" field in "keys" element, then we will take "key" value from that element and put in the output array "key".
I am using this spec file, but not getting required output. Can anyone please suggest how to do it?
[
{
"operation": "shift",
"spec": {
"*": "&",
"payload": {
"*": "&",
"logical": {
"schemas": {
"*": {
"tables": {
"*": {
"name": "payload.logical.schemas[&3].tables[&1].name",
"alias": "payload.logical.schemas[&3].tables[&1].alias",
"keys": {
"*": {
"match": {
"match1|match2": {
"#2": {
"name": "payload.logical.schemas[&4].tables[&4].keys.name",
"match": "payload.logical.schemas[&4].tables[&4].keys.match",
"key": "payload.logical.schemas[&4].tables[&4].keys.key"
}
}
}
}
}
}
}
}
}
}
}
}
}
]
I got desired output with below spec file:
[
{
"operation": "shift",
"spec": {
"*": "&",
"payload": {
"*": "&",
"logical": {
"schemas": {
"*": {
"tables": {
"*": {
"name": "payload.logical.schemas[&3].tables[&1].name",
"alias": "payload.logical.schemas[&3].tables[&1].alias",
"keys": {
"*": {
"match": {
"*": {
"#(2,name)": "payload.logical.schemas[&7].tables[&5].keys[&3].name",
"#(2,match)": "payload.logical.schemas[&7].tables[&5].keys[&3].match",
"#(2,key)": "payload.logical.schemas[&7].tables[&5].keys[&3].key"
}
}
}
}
}
}
}
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "&",
"payload": {
"*": "&",
"logical": {
"schemas": {
"*": {
"tables": {
"*": {
"name": "payload.logical.schemas[&3].tables[&1].name",
"alias": "payload.logical.schemas[&3].tables[&1].alias",
"keys": {
"*": {
"name": "payload.logical.schemas[&5].tables[&3].keys.name",
"match": "payload.logical.schemas[&5].tables[&3].keys.match",
"key": "payload.logical.schemas[&5].tables[&3].keys.key"
}
}
}
}
}
}
}
}
}
},
{
"operation": "cardinality",
"spec": {
"payload": {
"logical": {
"schemas": {
"*": {
"tables": {
"*": {
"keys": {
"name": "ONE",
"match": "ONE"
}
}
}
}
}
}
}
}
}
]

How to construct a JSON based query on nested object having array items with ES search?

[
{
"name": "Document 1",
"tags": {
"typeATags": ["a1"],
"typeBTags": ["b1"],
"typeCTags": ["c1"],
"typeDTags": ["d1"]
}
},
{
"name": "Document 2",
"tags": {
"typeATags": ["a2"],
"typeBTags": ["b1", "b2"],
"typeCTags": ["c2"],
"typeDTags": ["d1", "d2"]
}
},
{
"name": "Document 3",
"tags": {
"typeATags": ["a1", "a2", "a3"],
"typeBTags": ["b1", "b2", "b3"],
"typeCTags": ["c3"],
"typeDTags": ["d1", "d2", "d3"]
}
}
]
How to build a query on ES 6.0,
That will return all the records that has 'a1' and 'b1' tags ? // should return 1,3
That will return all the records that has 'a1' and 'a2' tags combined? // should return 3
That will return all the records that has 'a1' or 'a2' tags ? //should return 1,2,3
That will return all the records that has 'a1' AND ( 'c1' OR 'c3') tags ? //should return 1,2
Thanks #mickl for the Answer
Edit 1:
Here is my actual Schema,
{
"cmslocal": {
"mappings": {
"video": {
"properties": {
"assetProps": {
"properties": {
"assetType": {
"type": "string"
},
"configPath": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
},
"analyzer": "standard"
},
"contentSha1": {
"type": "string"
},
"originalPath": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
},
"analyzer": "standard"
},
"path": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
},
"analyzer": "standard"
},
"thumbnailPath": {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
},
"analyzer": "standard"
}
}
},
"channel": {
"type": "string"
},
"configProps": {
"properties": {
"events": {
"type": "nested",
"include_in_root": true,
"properties": {
"Desc": {
"type": "string"
},
"Tags": {
"type": "string"
},
"UUID": {
"type": "string"
}
}
},
"roiUUID": {
"type": "string"
}
}
},
"contentSha1": {
"type": "string"
},
"eventDesc": {
"type": "string"
},
"ext": {
"type": "string"
},
"format": {
"type": "string"
},
"fovProps": {
"properties": {
"description": {
"type": "string"
},
"width": {
"type": "float"
}
}
},
"locationProps": {
"type": "nested",
"properties": {
"address": {
"type": "string"
},
"city": {
"type": "string"
},
"country": {
"type": "string"
},
"county": {
"type": "string"
},
"location": {
"type": "geo_point"
},
"postcode": {
"type": "string"
},
"state": {
"type": "string"
}
}
},
"nodeid": {
"type": "string"
},
"poleHeight": {
"type": "float"
},
"query": {
"properties": {
"bool": {
"properties": {
"filter": {
"properties": {
"term": {
"properties": {
"nodeid": {
"type": "string"
}
}
}
}
}
}
}
}
},
"retentionPolicy": {
"type": "string"
},
"siteScopeID": {
"type": "string"
},
"tagProps": {
"type": "nested",
"properties": {
"conditions": {
"type": "string"
},
"environment": {
"type": "string"
},
"events": {
"type": "string"
},
"lighting": {
"type": "string"
},
"objects": {
"type": "string"
},
"other": {
"type": "string"
},
"scenes": {
"type": "string"
},
"useCases": {
"type": "string"
},
"weather": {
"type": "string"
}
}
},
"test": {
"type": "string"
},
"title": {
"type": "string"
},
"uploadTime": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"videoProps": {
"properties": {
"bitrate": {
"type": "float"
},
"datetime": {
"type": "date",
"format": "date_hour_minute_second_millis"
},
"daySegments": {
"type": "string"
},
"duration": {
"type": "long"
},
"framerate": {
"type": "float"
},
"height": {
"type": "integer"
},
"overlaysOn": {
"type": "boolean"
},
"width": {
"type": "integer"
}
}
}
}
}
}
}
}
Please help to build the query so I can search for
Only nodeId, Only Channel, Date Range
Any of the Tags
I'm able to search for nodeId, Channel Id using
{
"query": {
"bool": {
"filter": [
{ "match": { "nodeid": "N02cff15a" } },
{ "match": { "channel": "1" } }
]
}
}
}
and I can able to Search for tagProps using
{
"nested": {
"path": "tagProps",
"query": {
"bool": {
"must": [
{ "match": { "tagProps.objects": "car" } },
{ "match": { "tagProps.objects": "truck" } }
]
}
}
}
}
Help me combine two queries so I can search for NodeId with Combination of Tags.
Since tags is a nested field you should define nested mapping before indexing your documents.
{
"mappings": {
"your_type": {
"properties": {
"tags": {
"type": "nested"
}
}
}
}
}
Now you can index your data and then you can utilize nested query syntax, for your first use case it's like:
{
"query": {
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{ "match": { "tags.typeATags": "a1" }},
{ "match": { "tags.typeBTags": "b1" }}
]
}
}
}
}
}
Next queries can be composed of must and should, like for the last one:
{
"query": {
"nested": {
"path": "tags",
"query": {
"bool": {
"must": [
{ "match": { "tags.typeATags": "a1" }}
],
"should": [
{"match": {"tags.typeCTags": "c1"}},
{"match": {"tags.typeCTags": "c3"}}
]
}
}
}
}
}

Resources