mercredi 4 novembre 2015

How to import objects and reference their grandparent/routing correctly using elasticsearch-rails

I'm running a Ruby on Rails app using the elasticsearch-rails gem.

I have this mapping:

"mappings": {
    "account" : {
        "properties": {
            "is_private": {
                "type": "boolean"
            }
        }
    },
    "user" : { 
        "_parent" : { 
            "type" : "account" 
        }  
    },
    "address" : { 
        "_parent" : { 
            "type" : "user" 
        }  
    }
}

In my Address model, the mapping is defined as:

mapping(_parent: {type: 'user', required: 'true'}) do
end

So when I import the Address objects, I run a transform to make sure the routing is taken care of:

t = lambda do |a|
  {
    index: {
      _id: a.id,
      _parent: a.user_id,
      _routing: a.account_id,
      data: a.__elasticsearch__.as_indexed_json
    }
  }
end
Address.import index: 'my_index', batch_size: 100, transform: t

This runs to completion, and it seems like everything is fine, but the routing seeems to be off.

I can perform this:

GET my_index/address/147?parent=160&routing=74

And I get the right address.

But performing GET my_index/address/147?parent=160 returns no hits.

What's more is GET my_index/address/147?parent=74 returns the same hit as the first GET where both the parent and routing values are included. Here, the parent value is being used like the routing value. Is that normal and expected?

Something seems to be off, though. When I run a search on address objects, I get hits, but no inner_hits. So elasticsearch can't seem to return the address's parent for some reason. Example search:

POST my_index/address/_search
{
    "query": {
        "has_parent": {
            "type": "user",
            "query": {
                "has_parent": {
                    "type": "account",
                    "query": {
                        "term": {"is_private": false}
                    }
                }
            },
            "inner_hits": {}
        }
    }
}

This returns 12 answer hits, but no user inner_hits. What could that mean about how I've wired up my address, user, and account objects? What is wrong?

Aucun commentaire:

Enregistrer un commentaire