elastic / elasticsearch-dsl-py

High level Python client for Elasticsearch
http://elasticsearch-dsl.readthedocs.org
Apache License 2.0
3.83k stars 801 forks source link

'join' field type support? #934

Closed jaddison closed 6 years ago

jaddison commented 6 years ago

I've been using the low level elasticsearch-py for a while now - dealing with complex dict-type query layouts, etc. I'm interested in moving towards using this DSL library.

At the same time, I'm looking at upgrading from ES 5.x to 6.x - however, this means I need to switch my mappings to be 'typeless' (a single mapping per index) and maintain a parent/child relationship. My understanding is that I need to use the underlying join field type.

So, I currently have Business and Activity type mappings (Business is the parent of Activity) in a single index. With ES 5.x, I would obviously just create an ES-DSL Document for each: BusinessDocument and ActivityDocument, and set up things with _parent and whatnot, I assume.

How would this work with ES 6.x join fields though (given that a single mapping exists for an index)? I don't think I'd want a combined BusinessActivityDocument, because I would still want to do BusinessDocument.search().filter(...) separately from ActivityDocument.search().filter(...) and have relevant fields?

Am I missing something? Thanks!

honzakral commented 6 years ago

You can have a look in the examples directory where there is a complex model using parent/child via the join field.

Hope this helps!

jaddison commented 6 years ago

I was just going to say that I found https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/parent_child.py - I think it would be good to have a mention about this in the docs, however.

honzakral commented 6 years ago

It is mentioned in the readme and at the top of the documentation: http://elasticsearch-dsl.readthedocs.io/en/latest/#examples

If there is more places where we could mention it i would be very happy for the suggestion. Thanks!

jaddison commented 6 years ago

@HonzaKral Yes, I did notice that link - but I was searching the docs for the keyword join and nothing came up.

Anyhow, sorry for the noise.

jaddison commented 6 years ago

@HonzaKral, sorry to bother you again on this!

In looking at the parent/child example, it doesn't look like it sets the mapping fields for the Question and Answer documents into the index - only that of the 'abstract' base Post document: https://github.com/elastic/elasticsearch-dsl-py/blob/master/examples/parent_child.py#L174

Can fields from two different documents be set in the same index? The example doesn't seem to cover this.

honzakral commented 6 years ago

@jaddison yes, fields from two document classes can be defined on the same index and the example does use this approach since Question and Answer extend Post and don't override class Index the mappings will be merged and the base template then contain fields from both classes.

jaddison commented 6 years ago

@HonzaKral I think I see now: the meta Index class on a parent (ie. Post) is shared amongst all subclasses that don't have their own Index meta class in place.

Meaning, subclass field definitions 'get applied' to the Post's Index meta class, correct?

Regardless, thank you for pointing me in the right direction.