# Filter condition to check if 2 docs lie in each other's distance range

(Rahul Arora) #1

Hi, I have a use case for which I have to filter out all the documents whose coordinates lie in each other's range. I know how to filter out results based on one doc's location and search radius but I can't figure out how to do this for both sides

Example:

{
"id": 1,
"name": "Rahul",
"location": "20.00000, 77.00000",
"range": 20km""
}

{
"id": 2,
"name": "Shikher",
"location": "21.00000, 77.00000",
"range": 5km""
}

Now, I have to run a filter query with one person's coordinates and only those docs should be shown to me which lie in each other's range. How can I do that?

(Loren Siebert) #2

If I'm reading this right, you are saying you'd want to filter out any docs within 20km of Rahul, and then any remaining docs within 5km of Shikher, and so on. If so, I cannot think of a single query that would solve this. Off the top of my head, I would just:

1. Generate a sorted set of IDs by decreasing `range`, maybe using `scan` if you have a lot of documents. 2. For each document in the set, starting with the one with the largest `range`, find the set of documents that are within range and remove those documents from the set.

I'm just guessing here that filtering by larger ranges first will eliminate more documents earlier, thus reducing the number of documents you have consider as you iterate through the set. That is, the set will get smaller as you iterate through it.

(Rahul Arora) #3

I might not have been clear on this. So, I'll walk you through it once again. I have n number of docs in my index. And I have coordinates and search radius of all of them,

Now, if some user makes a query then what I have to do is that I wanna find all the docs, which lie in this user's search radius and this user should lie in their search radius.

(Loren Siebert) #4

I think you'd still do something similar.

1. Run filter query to find docs within range of Doc X

2. For each of those candidate docs C, run a filter query where id = Doc X and within C's range.

You could also handle the work for step 2 at index time, maintaining an array of candidate doc ids for each document.

(Rahul Arora) #5

Since there could be millions of docs in the index and this(location and search radius) is a dynamic field so I don't think it would be possible to do this at the time of indexing.

And secondly, would it be scalable to go with the approach you mentioned in steps 1 and 2?

I was wondering if there is any filter which could directly give me the distance between 2 coordinates. That I could use in a range query afterwards. By doing this, I'll be able to do this in a single query only.

(Loren Siebert) #6

Performance would depend on how many candidate docs were in range of Doc X. It wouldn't scale well for lots of candidates, that's for sure.

I think this is similar to a question I also failed to answer a while back, which also seemed like a computational geometry problem that could not be solved with a single ES query. Sorry I could not be more helpful!

(system) #7

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