Not all results included with parent-child query - elasticsearch

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.

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!

Can nested documents have multiple parents in 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.

elastic search - how to query the child resource

I have a resource
GET /parents/{Id}
this one I can see the alias in elastic search, I am able to retrieve data in ES by doing this : (GET parents_alias/parent/5/)
However with this resource
2/
For /parents/{Id}/comment/{commentId} I do not have an alias
I do not know how to query in ES in order to get comment
any idea?
If you are trying to create a parent child association in elasticsearch I believe you need to first create the parent-child mapping as explained here - https://www.elastic.co/guide/en/elasticsearch/guide/master/parent-child-mapping.html
Also you can get parent records if you know child details and vice versa.
More details here - https://www.elastic.co/guide/en/elasticsearch/guide/master/has-child.html
https://www.elastic.co/guide/en/elasticsearch/guide/master/has-parent.html

Is it possible to find which elasticsearch shard(s) a document is on?

I'm having trouble debugging a parent child relationship query. I'd like to know ways of debugging the issue, rather than simply posting my mapping, data, query, and asking what's wrong (but I reserve the right to do so eventually!).
To that end, a start would be checking if my children and associated parent are on the same shard. I don't trust my mapping, and I don't want to calculate which shard the documents are theoretically on, using shard = hash(routing) % number_of_primary_shards. I want a query that returns a definite answer.
In your query , you can enable explain flag and it will tell you where each of the documents are from which shard and node.
You can find a sample query as follows -
{
"explain": true,
"query": {
"match_all": {}
}
}
Along with docID , index name and type name , it will also emit node ID and shard ID.
You can find samples on usage of explain API here.
To debug this you don't have to check shard and node identifiers at all.
All you have to do is make sure that _parent and/or _routing fields of the child documents match the id of the parent document. Use /_search?pretty&fields=_parent,_routing&_source=true to show these fields.
To find docs with a specific _routing or _parent id just use /_search?pretty&fields=_parent,_routing&_source=true&q=id:123 OR _routing:123 OR _parent:123 This will find parent docs and child docs.

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