Elastic search sublist query with and logic - elasticsearch

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

Related

elasticsearch need to add a must to a bool should query

I have the following query that works as expected:
GET <index_name>/_search
{
"sort": [
{
"irFileCreateTime": {
"order": "desc"
}
}
],
"query": {
"bool": {
"should": [
{
"match": {
"fileId": 46704
}
},
{
"match": {
"fileId": 46706
}
},
{
"match": {
"fileId": 46719
}
}
]
}
}
}
The problem is that I need to further filter the data, but the field I need to filter on is a text field. I have tried many different ways of putting a must match into my query but everything is either malformed or filters out all hits when I know it should only filter out half. How can I add a must match "irStatus":"COMPLETE" to this query? Thanks in advance.
What you're after is a term query on, preferably, the keyword of irStatus. That is to say:
GET index/_search
{
"sort": [
{
"irFileCreateTime": {
"order": "desc"
}
}
],
"query": {
"bool": {
"must": [
{
"term": {
"irStatus.keyword": {
"value": "COMPLETE"
}
}
}
],
"should": [
{
"match": {
"fileId": 46704
}
},
{
"match": {
"fileId": 46706
}
},
{
"match": {
"fileId": 46719
}
}
]
}
}
}
Assuming your mapping looks something like this:
{
"mappings": {
"properties": {
"irFileCreateTime": {
"type": "date"
},
"fileId": {
"type": "integer"
},
"irStatus": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
The reason it's apparently failing on your end is that "COMPLETE" has been lowercased due to standard analyzer.
Alternatively, you could do:
{
"must":[
{
"query_string":{
"query":"irStatus:COMPLETE AND (fileId:(46704 OR 46706 OR 46719))"
}
}
]
}

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

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).

Elasticsearch nested query and order by "hits"

Using Elasticsearch, I'm trying to search nested data and return the documents with the most "hits."
Relevant Example Data
POST myrecipes/recipe
{
"title": "Potato Salad",
"ingredients": [
{"name":"potato"},
{"name":"mayonaise"},
{"name":"mustard"},
{"name":"bacon"},
{"name":"onion"},
{"name":"parsley"}
]
}
POST myrecipes/recipe
{
"title": "Seared Scallops",
"ingredients": [
{"name":"scallops"},
{"name":"butter"},
{"name":"bacon"}
]
}
POST myrecipes/recipe
{
"title": "Tuna melt",
"ingredients": [
{"name":"tuna"},
{"name":"onion"},
{"name":"butter"},
{"name":"bacon"},
{"name":"mayonaise"},
{"name":"lettuce"},
{"name":"tomato"},
{"name":"bread"}
]
}
My Index
PUT myrecipes
{
"mappings": {
"recipe": {
"properties": {
"ingredients": {
"type": "nested",
"include_in_parent": true,
"properties": {
"name": {
"type": "string",
"fields": {
"untouched": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
}
}
}
My Search:
POST myrecipes/recipe/_search
{
"query": {
"nested": {
"path": "ingredients",
"query": {
"bool": {
"should": [
{
"match": {
"ingredients.name": {
"query": "bacon"
}
}
},
{
"match": {
"ingredients.name": {
"query": "butter"
}
}
}
]
}
}
}
}
}
The query returns
"Tuna melt", "Potato Salad", "Seared Scallops"
I'm hoping for:
"Seared Scallops", "Tuna melt", "Potato Salad"
Since there are two "hits" on "Seared scallops" and "Tuna Melt" (bacon and butter), and only one "hit" on "Potato Salad" (bacon).

ElasticSearch and NEST - How do I construct a simple OR query?

I'm developing a building repository query.
Here is the query that I am trying to write.
(Exact match on zipCode) AND ((Case-insensitive exact
match on address1) OR (Case-insensitive exact match on siteName))
In my repository, I have a document that looks like the following:
address1: 4 Myrtle Street
siteName: Myrtle Street
zipCode: 90210
And I keep getting matches on:
address1: 45 Myrtle Street
siteName: Myrtle
zipCode: 90210
Here are some attempts that have not worked:
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"term": {
"address1": {
"value": "45 myrtle street"
}
}
},
{
"term": {
"siteName": {
"value": "myrtle"
}
}
}
]
}
},
{
"term": {
"zipCode": {
"value": "90210"
}
}
}
]
}
}
}
{
"query": {
"filtered": {
"query": {
"term": {
"zipCode": {
"value": "90210"
}
}
},
"filter": {
"or": {
"filters": [
{
"term": {
"address1": "45 myrtle street"
}
},
{
"term": {
"siteName": "myrtle"
}
}
]
}
}
}
}
}
{
"filter": {
"bool": {
"must": [
{
"or": {
"filters": [
{
"term": {
"address1": "45 myrtle street"
}
},
{
"term": {
"siteName": "myrtle"
}
}
]
}
},
{
"term": {
"zipCode": "90210"
}
}
]
}
}
}
{
"query": {
"bool": {
"must": [
{
"span_or": {
"clauses": [
{
"span_term": {
"siteName": {
"value": "myrtle"
}
}
}
]
}
},
{
"term": {
"zipCode": {
"value": "90210"
}
}
}
]
}
}
}
{
"query": {
"filtered": {
"query": {
"term": {
"zipCode": {
"value": "90210"
}
}
},
"filter": {
"or": {
"filters": [
{
"term": {
"address1": "45 myrtle street"
}
},
{
"term": {
"siteName": "myrtle"
}
}
]
}
}
}
}
}
Does anyone know what I am doing wrong?
I'm writing this with NEST, so I would prefer NEST syntax, but ElasticSearch syntax would certainly suffice as well.
EDIT: Per #Greg Marzouka's comment, here are the mappings:
{
[indexname]: {
"mappings": {
"[indexname]elasticsearchresponse": {
"properties": {
"address": {
"type": "string"
},
"address1": {
"type": "string"
},
"address2": {
"type": "string"
},
"address3": {
"type": "string"
},
"city": {
"type": "string"
},
"country": {
"type": "string"
},
"id": {
"type": "string"
},
"originalSourceId": {
"type": "string"
},
"placeId": {
"type": "string"
},
"siteName": {
"type": "string"
},
"siteType": {
"type": "string"
},
"state": {
"type": "string"
},
"systemId": {
"type": "long"
},
"zipCode": {
"type": "string"
}
}
}
}
}
}
Based on your mapping, you won't be able to search for exact matches on siteName because it's being analyzed with the standard analyzer, which is more tuned for full text search. This is the default analyzer that is applied by Elasticsearch when one isn't explicitly defined on a field.
The standard analyzer is breaking up the value of siteName into multiple tokens. For example, Myrtle Street is tokenized and stored as two separate terms in your index, myrtle and street, which is why your query is matching that document. For a case-insensitive exact match, instead you want Myrtle Street stored as a single, lower-cased token in your index: myrtle street.
You could set siteName to not_analyzed, which won't subject the field to the analysis chain at all- meaning the values will not be modified. However, this will produce a single Myrtle Street token, which will work for exact matches, but will be case-sensitive.
What you need to do is create a custom analyzer using the keyword tokenizer and lowercase token filter, then apply it to your field.
Here's how you can accomplish this with NEST's fluent API:
// Create the custom analyzer using the keyword tokenizer and lowercase token filter
var myAnalyzer = new CustomAnalyzer
{
Tokenizer = "keyword",
Filter = new [] { "lowercase" }
};
var response = this.Client.CreateIndex("your-index-name", c => c
// Add the customer analyzer to your index settings
.Analysis(an => an
.Analyzers(az => az
.Add("my_analyzer", myAnalyzer)
)
)
// Create the mapping for your type and apply "my_analyzer" to the siteName field
.AddMapping<YourType>(m => m
.MapFromAttributes()
.Properties(ps => ps
.String(s => s.Name(t => t.SiteName).Analyzer("my_analyzer"))
)
)
);

Resources