Query both parent/child documents


(Eugene Prokopiev) #1

Hi,

I have some documents with parent-child relationship:

curl -XPUT 'http://localhost:9200/data?pretty=1' -d '
{
   "mappings" : {
      "destination" : {},
      "venue" : {
         "_parent" : {
            "type" : "destination"
         }
      }
   }
}
'

curl -XPOST 'http://localhost:9200/wg_data/_bulk?pretty=1' -d '
{"index":{"_id":"472","_type":"destination"}}
{"city_id":"49","country_code":"DE","id":"472","city_name":"Munich"}
{"index":{"_parent":"472","_id":"2804","_type":"venue"}}
{"subcategories":["Falafel Restaurant","Middle Eastern Restaurant"],"name":"Sababa","categories":["Restaurant"],"id":"2804"}
{"index":{"_parent":"472","_id":"2805","_type":"venue"}}
{"subcategories":["Cafe","Cocktail Bar","Italian Restaurant"],"name":"Bar Centrale","categories":["Bar","Restaurant"],"id":"2805"}
{"index":{"_parent":"472","_id":"2806","_type":"venue"}}
{"subcategories":["Restaurant","Bar"],"name":"LUX Restaurant & Bar","categories":["Bar"],"id":"2806"}
{"city_id":"40","country_code":"FR","id":"465","city_name":"Paris"}
{"index":{"_parent":"465","_id":"1868","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Benedict","categories":["Restaurant"],"id":"1868"}
{"index":{"_parent":"465","_id":"1870","_type":"venue"}}
{"subcategories":["Restaurant","Gourmet Shop","Bar"],"name":"Comptoir Gourmet","categories":["Bar","Shop"],"id":"1870"}
{"index":{"_parent":"465","_id":"1871","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Pamela Popo","categories":["Restaurant"],"id":"1871"}
{"index":{"_parent":"465","_id":"1872","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Chez Julien","categories":["Restaurant"],"id":"1872"}
{"index":{"_parent":"465","_id":"8426","_type":"venue"}}
{"subcategories":["Jazz Club"],"name":"Sunset","categories":["Activity"],"id":"8426"}

Is it possible to select paren/child pair by parent country_code (FR) and child subcategory ("Jazz Club") by one query? So, I want to see something like this as result:

{"city_id":"40","country_code":"FR","id":"465","city_name":"Paris", venues:
[{"subcategories":["Jazz Club"],"name":"Sunset","categories":["Activity"],"id":"8426"}]
}

I see only one way now - find parent id first by country_code:

GET data/destination/_search 
{
  "query": { "term": {
    "country_code.keyword": {
      "value": "FR"
    }
  }}
}

and child by parent id and subcategory:

GET data/venue/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "parent_id": {
            "type": "venue",
            "id": "465"
          }
        },
        {
          "term": {
            "subcategories.keyword": {
              "value": "Jazz Club"
            }
          }
        }
      ]
    }
  }
}

Maybe more simple way exists?


(system) #2

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.