Query technical question

First the look of my fields.

  • id
  • parent_id
  • type

I want to write a query that show all the results that have this criteria :

  • Each result's id shouldn't be a parent_id of another result (parent_id 0 doesn't count)
  • The results should only be of type 1

For example :
Id = 1, parent_id = 11, type = 1
Id = 2, parent_id = 1, type = 2
Id = 3, parent_id = 4, type =2
Id = 4, parent_id = 7, type = 1
Id = 5, parent_id = 8, type = 2
Id = 6, parent_id = 9, type = 1
Id = 7, parent_id = 0, type = 3
Id = 8, parent_id = 0, type = 3
Id = 9, parent_id = 0, type = 3

So from these data, the result should give me the data of Id 6 only in this example

In other words, I want to verify before showing the result that each of the current data does not match a parent_id of any other data.

In other other words, I want to put a must_not id = parent_id and must type = 1.

How to do this in elasticsearch?
I hope my question is clear!

You can filter by type using term query and you can use has_child query wrapped into boolean must not to filter out records with children.

1 Like

How to write it so that I would tell that the child shouldn't have a parent_id of the parent's id? I would need to compare in the child all other data, and see if their parent_id is equal to the id we're trying comparing them to. Your idea is good, but I can't seem to see how to compare them this way in ES?

I've been told to use scripts inside the child to do so, but that will be extremely slow to use scripts + has_child. Is there another way to do that?

Isn't that what has_child query is doing? If not I probably don't understand what you are trying to achieve. Sorry.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.