Querying OR filtering on a nested object


(markokke) #1

I'm trying to query or filter on a nested object that contains keywords. My document is setup like this:

{
"all": {
"type": "object",
"_all": {
"enabled": false
},
"dynamic": false,
"properties": {
"mktprodfamilytransid": {
"type": "integer"
},
"name": {
"type": "string",
"analyzer": "standard",
"boost": 2
},
"descriptionhtml": {
"type": "string",
"analyzer": "standard"
},
"shortdescription": {
"type": "string"
},
"teasertext": {
"type": "string"
},
"productkeywords": {
"type": "nested",
"properties": {
"keywordcategoryid": {
"type": "integer"
},
"keywordcategory": {
"type": "string"
},
"keywordcategoryslug": {
"type": "string"
},
"keywordid": {
"type": "integer"
},
"keyword": {
"type": "string",
"index": "not_analyzed"
},
"keywordslug": {
"type": "string"
}
}
}
}
}
}

I can have 1 to many keywords per product/object. The nest object could look like this:

               "productkeywords":
               [
                   {
                       "keywordcategoryid": 1,
                       "keywordcategory": "Frequency",
                       "keywordcategoryslug": "freq",
                       "keywordid": 6,
                       "keyword": "Other",
                       "keywordslug": "other"
                   },
                   {
                       "keywordcategoryid": 2,
                       "keywordcategory": "Wireless Products",
                       "keywordcategoryslug": "wireless",
                       "keywordid": 8,
                       "keyword": "Wi-Fi",
                       "keywordslug": "wifi"
                   },
                   {
                       "keywordcategoryid": 2,
                       "keywordcategory": "Wireless Products",
                       "keywordcategoryslug": "wireless",
                       "keywordid": 10,
                       "keyword": "ZigBee",
                       "keywordslug": "zigbee"
                   },
                   {
                       "keywordcategoryid": 3,
                       "keywordcategory": "Wired Connectivity",
                       "keywordcategoryslug": "wired",
                       "keywordid": 13,
                       "keyword": "Ethernet",
                       "keywordslug": "ethernet"
                   },
                   {
                       "keywordcategoryid": 3,
                       "keywordcategory": "Wired Connectivity",
                       "keywordcategoryslug": "wired",
                       "keywordid": 15,
                       "keyword": "Serial",
                       "keywordslug": "serial"
                   }
               ]

What I want to do is query or filter on multiple keywords so if a person selects "Serial" AND "Wi-Fi" I want to pull back ONLY documents that have those two keywords.

Here is an example of my current REST Json that works and pulls back ALL documents with keyword of "Wi-FI".

{
"query": {
"nested": {
"_scope": "productscope",
"path": "productkeywords",
"score_mode": "avg",
"query" : {
"filtered" : {
"query": { "match_all" : {} },
"filter" : {
"and" : [
{
"term" : {"productkeywords.keyword": "Wi-Fi" }
}
]
}
}
}
}
}
}

Once I add another element (example below) I get nothing back:

{
"query": {
"nested": {
"_scope": "productscope",
"path": "productkeywords",
"score_mode": "avg",
"query" : {
"filtered" : {
"query": { "match_all" : {} },
"filter" : {
"and" : [
{
"term" : {"productkeywords.keyword": "Wi-Fi" }
},
{
"term" : {"productkeywords.keyword": Serial" }
}
]
}
}
}
}
}
}

Any info would be great...thanks.


(markokke) #2

I realize I made a typo but it will not work even with the type fix in the second filter:

FROM:

"term" : {"productkeywords.keyword": Serial" }

TO:

"term" : {"productkeywords.keyword": "Serial" }


(system) #3