Hi,
I currently need to run some queries on my company dataset stored in elastic. A simplified version of the mapping in place is the following:
Mapping for Child
{
"_parent": {
"type": "parent"
},
"properties": {
"childId": {
"type": "long"
},
"term": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"integerValue": {
"type": "integer"
},
"doubleValue": {
"type": "double"
},
"boolValue": {
"type": "boolean"
},
"dateTimeValue": {
"type": "date"
}
}
}
Mapping for Parent
{
"properties": {
"parentId": {
"type": "long"
},
"title": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"createdDate": {
"type": "date"
},
"createdBy": {
"type": "integer"
},
"geometry": {
"type": "geo_shape"
}
}
}
I am providing the company with a custom built query mechanism to query this dataset by using an abstract company syntax that reflects the structure we have in place. The problem I am facing at the moment is related to sorting and originated two questions.
-
How do I sort alphabetically on a child attribute in an "has_child" query? In this case how would I sort on the term.raw field of a child type document in an "has_child" query executed on the parent?
-
Assuming I am now able to sort alphabetically on the child attribute in a "has_child" query, how do I sort in a single query, on multiple fields, BOTH from child and parent while also applying some conditions?
For example, let's say I wanted to retrieve all parents (we always want to retrieve the parents, never the children, children are only used for querying) with createdBy = 123 that also have children with doubleValue = 45.4. All fine up to here, however I'd also like, whichever the results are, for them to be sorted alphabetically on the term.raw field from a child and on the createdDate field. Priority to the term.raw field.
Is it even possible to achieve this? I looked into function_score and it looked powerful but I don't see how to use it with text/keyword fields and how to use it on parent and children at the same time. Am I wandering into a dangerous place no innocent soul should go to?
Thanks in advance for the help!!!