elastic search query based on the value of the attributes - elasticsearch

I have data set as below. Here, We have typeOfProduct, endDate and productDisplayDate attributes. Need to writ query based on the type of product as"ADD", "ADJ", "WTY" and "NEW".
I need to write the query as
IF (typeOfProduct === 'WTY')
today > endDate
IF (typeOfProduct === 'ADJ' || typeOfProduct === 'ADD')
today > productDisplayDate
IF (typeOfProduct === 'NEW')
NO CONDITION SHOULD DISPLAY ALL
{
"id": "100",
"name": "XYZ product",
"typeOfProduct": "WTY",
"endDate": "30/08/2020",
"productDisplayDate": null
},
{
"id": "101",
"name": "XYZ1 product",
"typeOfProduct": "NEW",
"endDate": "30/08/2020",
"productDisplayDate": null
},
{
"id": "101",
"name": "XYZ1 product",
"typeOfProduct": "ADJ",
"endDate": "30/08/2020",
"productDisplayDate": "30/08/2020",
}

The query you're after would look like this:
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"terms": {
"typeOfProduct": [
"NEW"
]
}
},
{
"bool": {
"filter": [
{
"terms": {
"typeOfProduct": [
"WTY"
]
}
},
{
"range": {
"endDate": {
"lt": "now"
}
}
}
]
}
},
{
"bool": {
"filter": [
{
"terms": {
"typeOfProduct": [
"ADJ",
"ADD"
]
}
},
{
"range": {
"productDisplayDate": {
"lt": "now"
}
}
}
]
}
}
]
}
}
}

Related

Elastic search sublist query with and logic

I have post below json data:
[
{
"id": 1,
"shopname": "seven up",
"shopkeeper": "John",
"salesbooks": [
{
"bookid": 11,
"bookname": "Tom-story",
"soldout": false
},
{
"bookid": 12,
"bookname": "Iron-Man",
"soldout": true
}
]
}
]
and I make a simple elastic query as below:
{
"from": 0,
"query": {
"bool": {
"must": [
{
"wildcard": {
"salesbooks.bookname": {
"value": "*Iron-Man*"
}
}
},
{
"term": {
"salesbooks.soldout": {
"value": false
}
}
}
]
}
}
}
It should be empty as I want filter salesbooks.bookname contain ("iron-man") and soldout is false, it didn't work
can I know what's wrong inside
Thank you
The "must" clause requires that all clauses be true, in your case there is no "salesbooks.bookname": "Iron-Man" and soldout false, only "salesbooks.bookname": "Iron-Man" and soldout true.
Another important point is that the wildcard is a Term Level Query.
My example I tested (I figured salesbooks is a nested object).
PUT /house
{
"mappings": {
"properties": {
"salesbooks": {
"type": "nested",
"properties": {
"bookname": {
"type": "keyword"
},
"soldout": {
"type": "boolean"
}
}
}
}
}
}
PUT /house/_doc/1
{
"shopname": "seven up",
"shopkeeper": "John",
"salesbooks": [
{
"bookid": 11,
"bookname": "Tom-story",
"soldout": false
},
{
"bookid": 12,
"bookname": "Iron-Man",
"soldout": true
}
]
}
GET house/_search
{
"from": 0,
"query": {
"nested": {
"path": "salesbooks",
"query": {
"bool": {
"must": [
{
"wildcard": {
"salesbooks.bookname": "*Iron-Man*"
}
},
{
"term": {
"salesbooks.soldout": {
"value": true
}
}
}
]
}
}
}
}
}

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

ElasticSearch - Filtering data returned from nested query

I am have a set of data in the following structure:
[
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
},
"customer": {
"name": "Prod3Cust1",
"totalOrders": 5
}
]
},
{
"productId": "ProductId2",
"customerNumbers": [
"customer": {
"name": "Prod2Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
}
]
}
]
and I need to fetch all the records which have a prefix of "Prod1 as in name field(in the example avoid, only first record should be returned i.e. ProductId1). Also, when the data is returned, I need to just fetch just the customer number whose prefix is Prod1 i.e:
Correct Output:
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
}
]
}
Instead of:
{
"productId": "ProductId1",
"customerNumbers": [
"customer": {
"name": "Prod1Cust1",
"totalOrders": 23
},
"customer": {
"name": "Prod2Cust1",
"totalOrders": 5
},
"customer": {
"name": "Prod3Cust1",
"totalOrders": 5
}
]
}
I'm able to fetch the records whose Name prefix is "Prod1" using nested query coupled with MatchPhrasePrefixQuery (this returns me result with all the customer numbers). How can I further filter the data to get customer numbers whose Name prefix is "Prod1".
Following is my current query:
{
"from": 0,
"size": 10,
"sort": [
{
"name.keyword": {
"missing": "_first",
"order": "asc"
}
}
],
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"match": {
"customerNumbers.name": {
"query": "Prod1",
"type": "phrase_prefix"
}
}
}
]
}
},
"path": "customerNumbers"
}
}
]
}
}
]
}
}
}
P.S: I'm using ElasticSearch 5.x with Nest.
Try using inner_hits:
PUT products
{"mappings":{"_doc":{"properties":{"customerNumbers":{"type":"nested"}}}}}
POST products/_doc
{"productId":"ProductId1","customerNumbers":[{"name":"Prod1Cust1","totalOrders":23},{"name":"Prod2Cust1","totalOrders":5},{"name":"Prod3Cust1","totalOrders":5}]}
POST products/_doc
{"productId":"ProductId2","customerNumbers":[{"name":"Prod2Cust1","totalOrders":23},{"name":"Prod2Cust1","totalOrders":5}]}
GET products/_search
{
"_source": "inner_hits",
"from": 0,
"size": 10,
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"nested": {
"path": "customerNumbers",
"query": {
"bool": {
"must": [
{
"match_phrase_prefix": {
"customerNumbers.name": {
"query": "Prod1"
}
}
}
]
}
},
"inner_hits": {}
}
}
]
}
}
]
}
}
}
yielding the following hits
[
{
"_index":"products",
"_type":"_doc",
"_id":"tyQGo3EBdiyDG0RsTa0N",
"_score":0.9808292,
"_source":{
},
"inner_hits":{
"customerNumbers":{
"hits":{
"total":1,
"max_score":0.9808292,
"hits":[
{
"_index":"products",
"_type":"_doc",
"_id":"tyQGo3EBdiyDG0RsTa0N",
"_nested":{
"field":"customerNumbers",
"offset":0
},
"_score":0.9808292,
"_source":{
"name":"Prod1Cust1", <-----
"totalOrders":23
}
}
]
}
}
}
}
]

should query inside must elasticsearch

I am new to elasticsearch query DSL, I have documents in my ES index as below
{
"doc_id":1,
"city_id": 1,
"name": "test1",
"vvip": false
},
{
"doc_id":2,
"city_id": 1,
"name": "test2",
"vvip": false
},
{
"doc_id":3,
"city_id": 1,
"name": "test3"
}
{
"doc_id":4,
"city_id": 1,
"name": "test4"
}
{
"doc_id":5,
"city_id": 1,
"name": "test5",
"vvip": true
}
{
"doc_id":6,
"city_id": 1,
"name": "test6",
"vvip": true
}
For doc_id 3 and doc_id 4 vvip field does not exist
I want to form a query in such a way that these documents should always be returned if I query for vvip:true or vvip:false
Meaning for query city_id=1 and vvip=false
response will contain doc_id 1,2,3,4
for city_id=1 and vvip=true
response will contain doc_id 3,4,5,6
I tried below query but it did not help
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"term": {
"city_id": "1"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "vvip"
}
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"city_id": "1"
}
},
{
"match": {
"vvip": false
}
}
]
}
}
]
}
}
}
You may simplify your query like this, which should work as you expect :
{
"query": {
"bool": {
"must": [
{
"match": {
"city_id": "1"
}
},
{
"bool": {
"should": [
{
"match": {
"vvip": true
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "vvip"
}
}
}
}
]
}
}
]
}
}
}
The query mean you match city_id=1 AND (vvip=true OR vvip not exist).

Elastic Search Fliter on two fields within same sub document

I am trying to write an elastic search query where I have an array of sub-documents with exactly same structure and I have to check two conditions within the same sub document.
My data looks like this :
{
"_id": "5672ba7c0d71c93d159c408e",
"productId": "1723913526",
"title": "Digitab Tablet",
"instock": true,
"specifications": [{
"category": "In the box",
"name": "Test1",
"value": "No"
}, {
"category": "Warranty",
"name": "Test2",
"value": "Yes"
}, {
"category": "General",
"name": "Test3",
"value": "Abcd"
}],
}
}
What I am trying is : I should get products where specifications.name is "Test1" and specifications.value of the same sub document is "Yes"
Here is the Query that I have written for the this:
{
"query": {
"bool": {
"must": [
{
"wildcard": {
"specifications.name": "Test1*"
}
},
{
"term": {
"instock": "true"
}
},
{
"term": {
"specifications.value": "yes"
}
}
],
"must_not": [],
"should": []
}
}
}
The issue is - It should not return the product with id 1723913526 because "value": "Yes" is true for "name": "Test2" and not for "name": "Test1".
I hope the question is clear, please comment if more information is needed.
Thanks in advance.
You need to modify your mapping to indicate that specifications is a nested type.
Mapping like:
"mappings": {
"product": {
"properties": {
...
"specifications": {
"type": "nested"
}
}
}
}
And then querying like:
"bool": {
"must": [{
"nested": {
"path": "specifications",
"query": {
"bool": {
"must": [
{ "wildcard": { "specifications.first": "Test1*" }},
{ "term": { "specifications.value": "yes" }}
]
}
}
},
{
"term": { "instock": "true" }
}
}]
}

Resources