Can nested documents have multiple parents in Elasticsearch - elasticsearch

I recently found several references that nested documents can have multiple parents
http://vesess.com/elasticsearch-for-hiveage/
https://qbox.io/blog/elasticsearch-performance-considerations-parent-child-relationships
But I don't find any reference to this in the docs. It seems that there would be only one (I understand I could index the same document twice, but I wouldn't think that's the intent.)
Can someone confirm, deny, or qualify this claim that nested documents can have multiple parents?

It is not possible for a child to have multiple parents. Parent child is 1 to N relation.
A child can have also other child, but each document has at maximum one parent.

Related

ES 6.x, join type, when parent and child doc have the same field. store it as one field or two?

As we know, ES6.x store parent and child type in one document one type.
My model is:
parent: {id, name, point}
child: {id, point}
What is the suggestion to design es document?
{id, name, point} or {id, name, point, child_point}
The question is: when parent and child have the field with the same name, should I store it in one field or two?
I believe you can go both ways. Which one to go depends on what kind of queries you want to perform against parent and child documents.
if you reuse field names
Let's say, you make name reused field name.
You can query all documents by name - parent documents will be returned along with child documents. This may be undesirable (or may be not, depends on what you want).
In order to query by name only parent or only child documents you can still use a must like this: name: X AND type: parent (or child).
If you reuse text fields, the relevance scoring might be affected - because a field with the same name will use the same inverted index, and parent token frequencies will affect relevance of child text searches.
if you don't reuse field names
Let's say you decide for parent documents to only populate "parent fields", and for child documents - only "child fields". For example, in parent you would use name and in child childName.
In this case to search for only parent documents by name it would be enough to use one query: name: X. To search for only child documents by name: childName: X.
If you will need to mix the results (parent and child documents in the same result set) you can still combine these two queries via a should.
The index for full text search of parent's name will not affect that one of childName.
So, which one to choose?
has_child and has_parent queries seem to not affect the choice, since they will distinguish the parent from child automatically.
Select reuse of the fields if you do a lot of queries that query parent and child documents in the same way.
Otherwise, do not reuse the field names.
Hope that helps!

Which is the best way to index the data from relational database table of One to many relationship

Can you please let me know which is the best way to index the records in elastic search for my scenario.
My Scenario is :
1) Need to index around 40 million records from oracle table which has entries having one to many relationship records. And the uniqueness of the records is based on the composite key with 4 columns
2) After indexing , Search should support "full text search" on all the fields
3) Filters and sorting on selected fields needs to be supported.
After going through the official documentation i found couple of options , but want to know which approach would be most useful among below
1) For each record in table create a entry in the elastic index
2) Create a nested json object based on the composite key and then add this elastic index
3)Parent child Relationship mechanism and application side joins are not suitable for my scenario
Thanks
Girish T S
Your question is not particularly clear, here's how I understand it: you have 40M child records in one table, each with a reference to a parent record.
You want to index your records so as to be able to search for a parent record whose children match certain criteria.
There are two solutions here:
Indexing one document per parent, with all children indexed as nested documents within the parent
Indexing each child record as a separate document, with a parent-child relationship in ElasticSearch
The first solution will have better performance, but it means that every time a child is updated, the full parent document must be reindexed with all its children.
In any case you're saying that a parent-child scheme is not suitable for your case, so you're left with only the first solution.

Not all results included with parent-child query

I'm trying to query ES (v. 2.2.0) and get child documents with their parents.
My Structure: post is a parent of campaignPost; campaignPost is a child of post.
And here is mapping for campaignPost:
http://pastebin.com/iAaAzdp3
Now in my index I've got 20 posts and 10 campaignPosts mapped to 10 of existing posts. The problem is that I'm expecting 10 records in the result but I've got only 4! Here is how I query ES:
http://pastebin.com/1vWwbMA2
Thanks for any suggestions!
I had a similiar problem with parent child relationship where my child type was parent of another type. Problem relied on routing parameter what was unsuitable for my granchildren. According to ES documentation my _routing parameter had to point on grandfather type.
The parent parameter is used to link the children with its parent, but the routing parameter ensures that it is stored on the same shard as its parent and grandparent. The routing value needs to be provided for all single-document requests.
Try to set your _routing parameter properly and let me know if it helps.

parent-child documents relation retrieval

could you help me with little problem regarding parent-child documents relation?
Considering JSON, I have objects, each of them contains an array of sub-objects. Sub-objects contain some text fields.
I need to maintain full-text-search on these objects and construct snippets. I need highlighting for building snippets.
If I use nested objects, highlighting does not deal with them.
Therefore, I use Parent-Child relationships.
Now I need to retrieve Parent-documents, which children match the query_string. Furthermore, I need to get highlighted fields of matched children and associate each one(each child) with corresponding parent to construct snippets in my application.
Is it possible to accomplish my goal in one query?
I think that you should consider using the children aggregation. With that you can retrieve children items within their parents. It's aggregation so you are not able to get the whole document (just id) but with that you retrieve the relationship... Then with another query you can get document details quickly.
Link here : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html
And more details : https://www.elastic.co/guide/en/elasticsearch/guide/current/children-agg.html

Return Child Properties In ElasticSearch Query

I have had to refactor some of my code in my app to use a Child/Parent relationship in ElasticSearch. I use the Parent as the main search object, and use HasChild to filter on any child properties.
As it stands now, the return object is the complete parent object. But in my code, I also require access to some of the child properties (Namely the child ID). Is this possible to be returned in the result set?
If you need data from the child, then you're actually looking for a query on the child object with a has_parent clause to filter by some condition on the parent.
Think of it this way: your results should include one hit per matching child, even if two children share the same parent. So the primary object you're searching is actually the child and not the parent.
You could also consider using a (top-children deprecated since ~1.7) has-child query which would give you parents and their matching children, however this requires some estimations regarding how many children you expect to see per parent.

Resources