Is searching by _id faster in parent-child relationship?


(shadyabhi) #1

I'm trying to model one-to-many relationships in Elasticsearch with ability to fetch parents and children without involving any searches.

Question 1: I thought that the way I can achieve this is by using _id wherever possible. Can someone confirm if fetching a document by doing GET request to /index/type/1 faster than doing a term query on a field that's not _id?

Below is an example of 1 parent and its 2 children.

Parent (type: parent):-

{
  "_id": "deterministic_id_1",
  "app": "awesome-app-1",
  "server": "awesome-server-1"
}

Children (type: children):-

{
  "config": "awesome-config-1"
  "_parent":  "deterministic_id_1"
}
{
  "config": "awesome-config-2"
  "_parent":  "deterministic_id_1"
}

I have deterministic _id field so that I can fetch parent documents without searching. That already works, simply do a GET request for /index/type/<id>.
I know that Elasticsearch maintains a mapping of all parent->child relationships internally. Now, how do I fetch all children for a parent without searching? Is there a way? Best I could come up with is:-

POST /index/children/_search
{
   "query": {
      "has_parent": {
          "type": "parent",
          "query": {
              "ids": {
                  "type": "config_parent",
                  "_id": "deterministic_id_1"
              }
          }
      }
   }
}

Question 2: Does Is this the fastest way I can fetch all children for the parent with _id as deterministic_id_1? Does this query hit all shards?

I want two things from my data:-

  1. Get access to all parents using my deterministic_id_1. (already done)
  2. Get access to all children using the parent's document id without involving any searching. (pending)

(shadyabhi) #2

Anyone?


(Adrien Grand) #3

It should perform a bit better but I would not expect it to be a game changer.

I think you should be able to do something like this:

POST /index/children/_search?routing=deterministic_id_1
{
   "query": {
     "term": {
       "_parent": "deterministic_id_1"
     }
   }
}

(shadyabhi) #4

Hi @jpountz,

Thanks for the reply. I somehow missed query as one of the options.

I see that we're using a routing key. Can I safely assume that this is the fastest way to search all children for a parent?


(system) #5

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