Skip to content

support geopolygon search and field type#2150

Merged
kishorenc merged 3 commits into
typesense:v28from
krunal1313:geopolygon_search
Jan 22, 2025
Merged

support geopolygon search and field type#2150
kishorenc merged 3 commits into
typesense:v28from
krunal1313:geopolygon_search

Conversation

@krunal1313

@krunal1313 krunal1313 commented Jan 17, 2025

Copy link
Copy Markdown
Contributor

Change Summary

  • add new field type geopolygon that can be added by giving polygon coordinates
  • allow to filter on this new field to find whether a geopoint is inside any geopolygon
  • add test

Geopolygon field type

Geopolygon field type supports adding geographical area in polygon form by supplying lattitude,longitude coordinates.

In following way we can use geopolygon field type,
create a schema with geopolygon field type,

{
        "name": "coll_geopolygon",
        "fields": [
            {"name": "name", "type": "string"},
            {"name": "area", "type": "geopolygon"}
        ]
}

Now add records to the created collection,

{"name":"sqaure","area":[0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 1.0]}
{"name":"rectangle","area":[2.0, 2.0, 5.0, 2.0, 5.0, 4.0, 2.0, 4.0]}

Here, area field is passed with latitude,longitude coordinates in series for square and rectangle areas.

Note : Always pass coordinates in CCW or CW order for adding geopolygon field without any issues.

Now we can use a search with filter like following to search any geopoint,

curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" "http://localhost:8108/collections/coll_geopolygon/documents/search?q=*&filter_by=area:(0.5, 0.5)"

Here we're searching geopolygon field area with a geopoint coordinate.

Subsequently it'll return the area which contains the passed geopoint like below,

{"facet_counts":[],"found":1,"hits":[{"document":{"area":[0.0,0.0,1.0,0.0,1.0,1.0,0.0,1.0],"id":"0","name":"square"},"highlight":{},"highlights":[]}],"out_of":2,"request_params":{"collection_name":"coll_geopolygon","first_q":"*","per_page":10,"q":"*"},"search_cutoff":false}

To remove the polygon, one need to simply remove the related document.

PR Checklist

@kishorenc kishorenc merged commit 98dfb5b into typesense:v28 Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants