Solr spatial search using an indexed field for rad

2019-06-14 02:16发布

So I have an index of cities, looks something like this:

{
  "location": "41.388587, 2.175888",
  "name": "BARCELONA",
  "radius": 20
}

We have a few dozen of these. I need to be able to query this index with a single lat/lng combination and see if it falls inside one of our "cities".

The location property is the centre of the city, and the radius is the radius of the city in km (assuming all the cities are circles). We can also assume no cities overlap.

How can I return whether or not a lat/lng combination falls within a city?

For example, given the point 40.419691, -3.701254, how can I determine if this falls within BARCELONA?

1条回答
啃猪蹄的小仙女
2楼-- · 2019-06-14 02:56

you can do it easily, in either Lucene, Solr or ES.

In Solr for example:

  1. declare a type of SpatialRecursivePrefixTreeFieldType. This allows you to index different shapes, not just points
  2. by using lat/long and the radius, you create an specific circle for each city, and you index that shape, in a field called 'shape' for example:

    {
      "location": "41.388587, 2.175888",
       "name": "BARCELONA",
       "shape": "CIRCLE (2.175888 41.388587, 20)"
    }
    
  3. then you just query for any doc that intersects with your point (untested):

    fq=shape:"Intersects(40.419691 -3.701254)"
    

Be sure to check the docs and javadocs for the specific version of Lucene/Solr/ES you are using, as APIs have been changing in this space

查看更多
登录 后发表回答