Книга: Apache Solr Search Patterns
Назад: Lucene 4 spatial module
Дальше: Searching and filtering on a spatial index

Indexing for spatial search

Now that we know the features supported by Solr for spatial search, let us see how indexing should be done for spatial search. We will need to index the coordinates for point, circle, rectangle, and other spatial representations in Solr as documents before we execute a search for them. Every geographical point is represented as latitude and longitude with the format lat,lon.

We saw points being indexed in the last section in our location.csv file. They are indexed in the lat,lon format:

<field name="store">28.570485,77.324713</field>

The points can be indexed in the lat-lon format by omitting the comma:

<field name="store">28.570485 77.324713</field>

We saw that Solr also supports different geometrical shapes such as rectangle, circle, and polygon. Let us learn how to index these.

In order to index a rectangle, we need the two points that form the starting and the ending points of the rectangle. Consider a two-dimensional coordinate system with X and Y axes. The said two points represent the maximum and minimum values of X and Y coordinates, as shown in the following figure:

Indexing for spatial search

<doc> <field name="id">CXXX1</field> <field name="name">rectangle</field> <field name="store">28.57 77.32 29.67 78.41</field> </doc>

In order to index documents directly from the Solr admin interface, go to the documents section inside the collection. This will lead you to the following URL: http://localhost:8983/solr/#/collection1/documents.

Select the Document Type as XML and write the XML document(s) in the given text box. Now, click on Submit Document, as shown in the following image:

Indexing for spatial search

A circle is also specified in the following format:

<doc> <field name="id">CXXX2</field> <field name="name">circle</field> <field name="store">Circle(28.57,77.32 d=0.051)</field> </doc>

The first point is in the lat,lon format, and the point represents the center of a circle. Further, d represents the distance radius in degrees. In order to convert the radius from degree to a desired unit, such as kilometers or miles, we need to use the following conversion formula:

degreeInDesiredUnit = degrees * radiusOfEarthInDesiredUnit

In the given case, note the following:

  • degreeInDesiredUnit: This parameter represents the unit we wish to convert degree to, such as kilometers or miles
  • degrees: This parameter represents the radius in degrees (that is, the value we specify for the d parameter)
  • radiusOfEarthInDesiredUnit: This parameter represents the radius of Earth in the desired unit

Refer to the following example for better understanding:

Degree (in Kilometers) = d * 111.1951 Degree (in Miles) = d * 69.09341

As per standard WKT specifications, we can index polygons as follows:

<doc> <field name="id">CXXX3</field> <field name="name">polygon</field> <field name="store">POLYGON((20 50, 18 60, 24 68, 26 62, 30 55, 20 50))</field> </doc>

The double parentheses are a part of the WKT specification.

Execute the following Solr query to index these documents:

http://localhost:8983/solr/collection1/select/?q=*:*&fq=name:circle polygon rectangle&fl=store

The shapes that we have indexed are shown in the following image:

Indexing for spatial search

Назад: Lucene 4 spatial module
Дальше: Searching and filtering on a spatial index

Solr
Testing
dosare
121