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)
}
}
}
}
]
}
}
}
}
}
}
}