Implementing distance querying on entities