Reputation: 1607
I have data similar to file paths that I would like to index in elasticsearch. (basically list of tokens separated by delimiter)
Ex data:
a/b/c/d/e
a/b/c/
a/m/n
x/y/z
Once I index, I should be able to query to get the immediate children for a given token as shown below.
For prefix of a, immediate children are [b, m]
For prefix of x, immediate children are [y]
Also tokens at root would be [a,x]
Upvotes: 2
Views: 3659
Reputation: 770
I don't think that there is an out of the box way to do it in Elasticsearch so you'll need to model the hierarchy yourself. One way to do it according to your example:
Index the data
POST index/type/id1
{
"a":["b","c","d","e"]
}
POST index/type/id2
{
"a":["b","c"]
}
POST index/type/id3
{
"a":["m","n"]
}
POST index/type/id4
{
"x":["y","z"]
}
Query 'a' children
POST index/type/_search
{
"size":0,
"aggs": {
"paths": {
"terms": {
"field": "a",
"size": 0
}
}
}
Query tokens at root. In the response look for the keys without '_'
POST index/type/_search
{
"size":0,
"aggs": {
"Field names": {
"terms": {
"field": "_field_names",
"size": 0
}
}
}
}
Hope I have managed to help! :)
Upvotes: 1
Reputation: 505
Have you considered a parent-child relationship?
Indexed as such you'd be able to query with the following
{
"query": {
"has_parent": {
}
}
{
"query": {
"has_child": {
}
}
Upvotes: 1