Tag cloud and Category list with gatsby-plugin-categories / gatsby-plugin-tags - graphql

I wanna create a tag cloud (or category list) which should link to the corresponding tagged articles and category. But in the queries I built only the name OR slug are concatenated because they are placed either in fields OR in frontmatter but not in one object
I use this two plugins which are widely used: https://github.com/rmcfadzean/gatsby-pantry
This is my current query:
{
tags: allMarkdownRemark(filter: {frontmatter: {title: {ne: ""}}}) {
group(field: frontmatter___tags) {
fieldValue
totalCount
edges {
node {
fields {
tags
}
frontmatter {
tags
}
}
}
}
}
}
{
"data": {
"allMarkdownRemark": {
"group": [
{
"fieldValue": "Another tag",
"totalCount": 1,
"edges": [
{
"node": {
"fields": {
"tags": [
"another-tag",
"my-example",
"post"
]
},
"frontmatter": {
"tags": [
"Another tag",
"My example",
"Post"
]
}
}
}
]
},
{
"fieldValue": "Example",
"totalCount": 1,
"edges": [
{
"node": {
"fields": {
"tags": [
"example",
"post"
]
},
"frontmatter": {
"tags": [
"Example",
"Post"
]
}
}
}
]
},
{
"fieldValue": "My example",
"totalCount": 1,
"edges": [
{
"node": {
"fields": {
"tags": [
"another-tag",
"my-example",
"post"
]
},
"frontmatter": {
"tags": [
"Another tag",
"My example",
"Post"
]
}
}
}
]
},
{
"fieldValue": "Post",
"totalCount": 2,
"edges": [
{
"node": {
"fields": {
"tags": [
"another-tag",
"my-example",
"post"
]
},
"frontmatter": {
"tags": [
"Another tag",
"My example",
"Post"
]
}
}
},
{
"node": {
"fields": {
"tags": [
"example",
"post"
]
},
"frontmatter": {
"tags": [
"Example",
"Post"
]
}
}
}
]
}
]
}
},
}
How can I get objects something like this:
{ "tags":
[
{ "slug": "another-tag", "frontmatter": "Another Tag"},
{ "slug": "example", "frontmatter": "Example"}
]
}

My current approach is in the view itself. I iterate through the fields.tags and search for them in the array. I save the index and use it for frontmatter.tags (they are happily in the same order)
It is exactly that code:
<ul className="tagcloud">
{tags.group.map((tag, idx) => {
var index = tag.edges[0].node.frontmatter.tags.indexOf(
tag.fieldValue
)
return (
<li key={idx}>
<Link
to={`/tags/${tag.edges[0].node.fields.tags[index]}`}
className="transition link"
>
{tag.fieldValue}
</Link>
</li>
)
})}
</ul>

Related

Logicapp Expression to read Dynamic Json path - read child element where parent path may change but hierarchy remaining same

Hope all well.
I am in need of creating logicapp expression for reading child element in json where name of element & hierarchy remains same but parent name can be changing.
for example : JSON-1 :
{
"root": {
"abc1": {
"abc2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
JSON-2 :
{
"root": {
"xyz1": {
"xyz2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
I have tried these but no luck
approach-1: #{body('previous-action')?['']?['']?['element']
approach-2: #{body('previous-action')???['element']
Please let me know if anyone encountered this situation. Many thanks in advance.
I tend to find that converting the JSON to xml (at least in your case) is the simplest solution. Then when you've done that, you can't use XPath to simply make your selection.
Flow
In basic terms ...
I've defined a variable of type object that contains your JSON.
I then convert that JSON object to XML using this expression xml(variables('JSON Object'))
Next, initialize a variable is called Elements of type array (given you have multiple of them). The expression for setting that variable is where the smarts come in. That expression is ... xpath(xml(variables('XML')), '//element/text()') and it's getting the inner text of all element nodes in the XML.
Finally, loop through the results.
If you needed to take it up a level and get the second element then you'd need to change your xpath query to be a lot more generic so you can get the element2 nodes (and 3, 4, 5, etc. if they existed) in each array as well.
Note: I've stuck to your specific question of looking for element.
Result
This definition (which can be loaded directly into your tenant) demonstrates the thinking ...
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_Each_Element": {
"actions": {
"Set_Element": {
"inputs": {
"name": "Element",
"value": "#{item()}"
},
"runAfter": {},
"type": "SetVariable"
}
},
"foreach": "#variables('Elements')",
"runAfter": {
"Initialize_Element": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_Element": {
"inputs": {
"variables": [
{
"name": "Element",
"type": "string"
}
]
},
"runAfter": {
"Initialize_Elements": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_Elements": {
"inputs": {
"variables": [
{
"name": "Elements",
"type": "array",
"value": "#xpath(xml(variables('XML')), '//element/text()')"
}
]
},
"runAfter": {
"Initialize_XML": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Initialize_JSON_Object": {
"inputs": {
"variables": [
{
"name": "JSON Object",
"type": "object",
"value": {
"root": {
"abc1": {
"abc2": [
{
"element": "value1",
"element2": "value"
},
{
"element": "value2",
"element2": "valu2"
}
]
}
}
}
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Initialize_XML": {
"inputs": {
"variables": [
{
"name": "XML",
"type": "string",
"value": "#{xml(variables('JSON Object'))}"
}
]
},
"runAfter": {
"Initialize_JSON_Object": [
"Succeeded"
]
},
"type": "InitializeVariable"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"ParameterTest1": {
"defaultValue": "\"\"",
"type": "String"
}
},
"triggers": {
"manual": {
"inputs": {
"method": "GET",
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {}
}

Extract value of array and add in the same select mongoDB

I am new to the mongoDB aggregation and I have this situation. I have this Json and I need to convert by "select" this object:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"adress": [
{
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"localization": "TATOOINE",
"createDate": 1627990555665
},
]
}
this way:
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "DEATH STAR",
"createDate": 1627990848665
},
{
"type": "PF",
"code": 12345
"Name": Darth Vader,
"currency": "BRL",
"status": "SINGLE",
"localization": "TATOOINE",
"createDate": 1627990555665
}
So, after my query is complete, I will have 02 objects instead of 01. How can I do this?
I would like to do this via select because after converting I will sort by createDate and limit the number of records to return to the API. I'm using Criteria em my project.
The way to do this is $unwind, this will make 1 copy of the document, for each member of the array.
Test code here
db.collection.aggregate([
{
"$unwind": {
"path": "$user.adress"
}
},
{
"$set": {
"user": {
"$mergeObjects": [
"$user",
"$user.adress"
]
}
}
},
{
"$unset": [
"user.adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])
Edit1 (the above is if user is a field, if it was the name of the collection)
$$ROOT is a system variable that has as value all the document
Test code here
Query
db.collection.aggregate([
{
"$unwind": {
"path": "$adress"
}
},
{
"$replaceRoot": {
"newRoot": {
"$mergeObjects": [
"$$ROOT",
"$adress"
]
}
}
},
{
"$unset": [
"adress"
]
},
{
"$sort": {
"createDate": 1
}
},
{
"$limit": 10
}
])

Google GA4 batchRunReports when API doesn't have records throw 500 (Internal Server Error)

https://developers.google.com/analytics/devguides/reporting/data/v1/rest/v1alpha/TopLevel/batchRunReports
Request :
{
"entity": {
"propertyId": "XXXXXXXX"
},
"requests": [
{
"entity": {
"propertyId": "XXXXXXXX"
},
"dimensions": [
{
"name": "date"
},
{
"name": "dateHour"
},
{
"name": "firstUserCampaignName"
}
],
"metrics": [
{
"name": "sessions"
}
],
"dateRanges": [
{
"startDate": "2021-04-06",
"endDate": "2021-04-07"
}
],
"metricAggregations": [
"TOTAL"
],
"dimensionFilter": {
"andGroup": {
"expressions": [
{
"filter": {
"fieldName": "medium",
"stringFilter": {
"matchType": "EXACT",
"value": "Test"
}
}
}
]
}
},
"orderBys": [
{
"desc": true,
"metric": {
"metricName": "sessions"
}
},
{
"desc": false,
"dimension": {
"dimensionName": "dateHour"
}
}
],
"keepEmptyRows": true
}
]
}
Response:
{
"error": {
"code": 500,
"message": "Internal error encountered.",
"status": "INTERNAL"
}
}
But if remove following property from request:
"metricAggregations": [
"TOTAL"
],
I am able to see following response where there is not rows :
{
"reports": [
{
"metricHeaders": [
{
"name": "sessions",
"type": "TYPE_INTEGER"
}
],
"metadata": {},
"dimensionHeaders": [
{
"name": "date"
},
{
"name": "dateHour"
},
{
"name": "firstUserCampaignName"
}
],
"kind": "analyticsData#runReport"
}
],
"kind": "analyticsData#batchRunReports"
}
Any idea how to prevent 500 internal server error in this case ?
This error block google API call for an hour.
Furqan, there seems to be an issue with the Data API where a call using metricAggregations is failing in case the generated report is empty. In the meantime, to workaround this error, you can modify a query so that the resulting report contains more than 0 rows.

Elasticsearch query for nested field existence

I have the following document saved in the Elasticsearch:
{
"transactionGroup": [
{
"name": "Test123",
"groups": [
{
"name": "Grp1",
"groups": [
{
"name": "g1",
"groups": [
{
"name": "g11",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
},
{
"name": "g12",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
}
],
"columns": {
"period": {
"newValue": "3Q"
},
"channelid": {
"newValue": "rQ"
}
}
},
{
"name": "g2",
"groups": [
{
"name": "g21",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
},
{
"name": "g22",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
}
],
"columns": {
"period": {
"newValue": "3Q"
},
"channelid": {
"newValue": "rQ"
}
}
}
],
"columns": {
"entityid": {
"newValue": "3292"
}
}
}
],
"columns": {
"lastmodifieddate": {
"newValue": "2020-05-18 07:29:54"
},
"lastmodifiedby": {
"newValue": "ajay"
}
}
}
]
}
I want following query.
How can I execute an exists query for:
If a group having name="g1", nested name="g11" having column field "cpm.newvalue" ?
I was trying something like:
"bool" : {
"should" : [
{
"exists" : {
"field" : "*transactionGroup.groups.groups.groups.columns.cpm.newValue",
"boost" : 1.0
}
},
But it's not working in all cases.
Here is the mapping:
{"unifiedplanner":{"_all":{"enabled":true,"norms":false},"properties":{"transactionGroup":{"properties":{"columns":{"properties":{"calendarid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channel":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"class":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dealyearid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"demo":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"enddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"endperiodid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"entityid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"equivalised":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"exceptions":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"exclusivity":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"fluidity":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"guaranteed":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"hiatus":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"marketplaceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"maxlineid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ordertypeid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"periodbreakdown":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planclassid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"plannotes":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"platform":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ratecard":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ratingstreamid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startperiodid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"type":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"version":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"address":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"asofdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"brandid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"categoryid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"commission":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"contactid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"corporate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysofweek":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"demo":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"divisionid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"downstreamplanstatusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"enddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"endtime":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"entityid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"gender":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"guaranteedcalculated":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"mode":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"notes":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionabledollartotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionablepercenttotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optiondata":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planhasnooptions":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"primary":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"revision":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"role":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"roles":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"seccategoryid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"secondary":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"self":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"separationtype":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"split":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"starttime":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subdivisionid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subjecttooptiontotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"teamid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"title":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"username":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"version":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"class":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"cpm":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysinadvance":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysofweek":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalsourcesystems":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"imps":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionabledollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionablepercent":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"period":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planclass":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"platform":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"programid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subjecttooption":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"cpm":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"imps":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"transactionSummary":{"properties":{"auditID":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"binLogTransactionId":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"databaseType":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"modifiedTime":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"moduleName":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"moduleTransactionId":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"rootEntityID":{"type":"keyword"},"schema":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"source":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"sourceTstamp":{"type":"long"},"userFullName":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"userName":{"type":"keyword"}}}}},"_default_":{"_all":{"enabled":true,"norms":false},"properties":{"transactionSummary":{"properties":{"rootEntityID":{"type":"keyword"},"userName":{"type":"keyword"}}}}}}
firstly you need to decide which type to assign to your field object or nested.
Example If you have an array with first names and last names
"users:[
{
"firstName":"a",
"lastName":"b"
},
{
"firstName":"x",
"lastName":"y"
}
]
In object type relationship between fields of array doesn't exist.
Suppose if you search for user whose firstName is "a" and last name "y". With object type above document will be returned (even though no such user exists) as objects are not treated as separate documents , you need to use nested type which is less performant than object.
If you decide to make our arrays as nested type, you need to use nested query to search on them
{
"query": {
"bool": {
"filter": {
"nested": {
"path": "groups",
"query": {
"bool": {
"must": [
{
"term": {
"groups.name": {
"value": "grp1"
}
}
},
{
"nested": {
"path": "groups.groups",
"query": {
"exists": {
"field": "groups.groups.field1"--> full name needed(full heirerchy)
}
}
}
}
]
}
}
}
}
}
}
}

Multiple searches with with json_query/jmespath filter in Ansible

I'm trying to parse out specifc subnet names in the following piece of json, while using contains_with or starts_with filters in json_query.
It contains two vnets each of which has multiple subnets:
{
"azure_virtualnetworks": [
{
"name": "test-vnet-172-17-0-0-19",
"properties": {
"subnets": [
{
"name": "test-confluent-subnet-172-17-0-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-test-subnet-172-17-0-32-28",
"properties": {
"addressPrefix": "172.20.88.32/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-test-rg/providers/Microsoft.Network/networkSecurityGroups/test-test-nsg"
},
"provisioningState": "Succeeded"
}
}
]
}
},
{
"name": "test2-vnet-172-17-1-0-19",
"properties": {
"subnets": [
{
"name": "test-confluent-subnet-172-17-1-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-qatesting-subnet-172-17-1-16-28",
"properties": {
"addressPrefix": "172.20.88.16/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
},
"provisioningState": "Succeeded"
}
}
]
}
}
]
}
I need to search for a subnet name after searching by virtual network name.
I can filter as far down as the list of subnets without problems. e.g
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[]
returns:
[
{
"name": "test-confluent-subnet-172-17-1-0-28",
"properties": {
"addressPrefix": "172.20.88.0/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-confluent-rg/providers/Microsoft.Network/networkSecurityGroups/test-confluent-nsg"
},
"provisioningState": "Succeeded"
}
},
{
"name": "test-qatesting-subnet-172-17-1-16-28",
"properties": {
"addressPrefix": "172.20.88.16/28",
"networkSecurityGroup": {
"id": "/subscriptions/********/resourceGroups/test-qatesting-rg/providers/Microsoft.Network/networkSecurityGroups/test-qatesting-nsg"
},
"provisioningState": "Succeeded"
}
}
]
However I'm having problems then searching the subnets. I had thought that some variation on following might work but haven't had any sucess:
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[?contains(name,`test-confluent`) ]
I'm struggling to figure out what the correcting syntax is here.
Select required subnets, stop projection with pipe expression, filter required items from the subnets list:
azure_virtualnetworks[?contains(name,`test2-vnet`)].properties.subnets[] | [?contains(name,`test-confluent`)]

Resources