Geospatial queries for Point and LineString

Performing geospatial queries for Point and LineString.

Performing geospatial queries for Point and LineString. This tutorial shows how to search with polygons and intersections.

Defining schemas for geospatial Point and LineString types

Define the schema for geospatial fields types, for example:
<?xml version="1.0" ?>
<schema name="spatial-no-jts" version="1.5">
    <types>
        <fieldType name="string" class="solr.StrField" />
        <fieldType name="boolean" class="solr.BoolField" />
        <!-- When geo="false", indicate worldBounds using ENVELOPE(minX, maxX, maxY, minY) notation -->
        <fieldType name="rpt"
                   class="solr.SpatialRecursivePrefixTreeFieldType"
                   geo="false"
                   worldBounds="ENVELOPE(-1000, 1000, 1000, -1000)"
                   maxDistErr="0.001"
                   units="degrees" />
    </types>

    <fields>
        <field name="id" type="string" indexed="true" stored="true" />
        <field name="point" type="rpt" indexed="true" stored="true" />
        <field name="linestring" type="rpt" indexed="true" stored="true" />
    </fields>

<uniqueKey>id</uniqueKey>

</schema>

Solr geospatial field types

For Solr geospatial field types, declare each geospatial field type in the table schema. For example:
CREATE TABLE test (
id text PRIMARY KEY, 
point 'PointType', linestring 'LineStringType');

Inserting or updating geospatial data

To insert or update data in the database, specify geotypes in the INSERT or UPDATE command. For example:
INSERT INTO test (id, point, linestring) VALUES ('1', 'POINT(5 50)', 
    'LINESTRING (30 10, 10 30, 40 40)' );
INSERT INTO test (id, point, linestring) VALUES ('2', 'POINT(100 100)', 
    'LINESTRING (50 20, 20 40, 50 50)' );

Querying geospatial data

Find points within a 10 unit radius from point (4, 49):
SELECT * FROM test WHERE solr_query=' {"q":"*:*", "fq":"point:\"IsWithin(BUFFER(POINT(4.0 49.0), 10.0))\""}';

Find linestring that contains the point (10, 30):

SELECT * FROM test WHERE solr_query='linestring:"Intersects(POINT(10 30))"';