user687554
user687554

Reputation: 11131

Getting child documents

I have an Elasticsearch index. Each document in that index has a number (i.e 1, 2, 3, etc.) and an array named ChildDocumentIds. There are additional properties too. Still, each item in this array is the _id of a document that is related to this document.

I have a saved search named "Child Documents". I would like to use the number (i.e. 1, 2, 3, etc.) and get the child documents associated with it.

Is there a way to do this in Elastisearch? I can't seem to find a way to do a relational-type query in Elasticsearch for this purpose. I know it will be slow, but I'm o.k. with that.

Upvotes: 0

Views: 48

Answers (1)

Jason Rosendale
Jason Rosendale

Reputation: 821

The terms query allows you to do this. If document #1000 had child documents 3, 12, and 15 then the following two queries would return identical results:

"terms" : { "_id" : [3, 12, 15] }

and:

"terms" : { 
  "_id" : {
    "index" : <parent_index>,
    "type" : <parent_type>,
    "id" : 1000,
    "path" : "ChildDocumentIds"
  }
}

The reason that it requires you to specify the index and type a second time is that the terms query supports cross-index lookups.

Upvotes: 1

Related Questions