How to use geo location within filter polygon using typesense instantsearch adapter

416 Views Asked by At

I create one demo instantsearch js with typesense.

Issue is when i search city the result come all result not filtering with _geoloc and polygon.

I use _geoloc field to store lat long with float array in typesense.

{"name": "_geoloc", "type": "float[]" , "facet": true },

And _geoloc pass geoLocationField parameter in Typesense instantSearch adapter.

const polygon = [
    42.01,-124.31,
    48.835509470063045,-124.40453125000005,
    45.01082951668149,-65.95726562500005,
    31.247243545293433,-81.06578125000004,
    25.924152577235226,-97.68234374999997,
    32.300311895879545,-117.54828125      
];

const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
    server: {
        apiKey: "xyz",
        nodes: [{
            host: "localhost",
            port: "8108",
            protocol: "http",
        }, ],
        cacheSearchResultsForSeconds: 2 * 60,
    },
    insidePolygon: [polygon],
    geoLocationField: "_geoloc",
    additionalSearchParameters: {
        queryBy: "name",
    },
});
2

There are 2 best solutions below

0
On BEST ANSWER

Thank you for helping @ErJab

After some research, I got a new solution and it's working perfectly.

Typesense adapter updated their code for the polygon search.

https://github.com/typesense/typesense-instantsearch-adapter/blob/978af9577ef632003aa2de6b1761772d979377eb/src/SearchRequestAdapter.js#L167-L198

now we can able to search inside a polygon.

const polygon = [
    42.01,-124.31,
    48.835509470063045,-124.40453125000005,
    45.01082951668149,-65.95726562500005,
    31.247243545293433,-81.06578125000004,
    25.924152577235226,-97.68234374999997,
    32.300311895879545,-117.54828125      
];
const typesenseInstantsearchAdapter = new TypesenseInstantSearchAdapter({
    server: {
        apiKey: "xyz",
        nodes: [{
            host: "localhost",
            port: "8108",
            protocol: "http",
        }, ],
        cacheSearchResultsForSeconds: 2 * 60,
    },
    geoLocationField: "_geoloc",
    additionalSearchParameters: {
        queryBy: "name",
    },
});
const searchClient = typesenseInstantsearchAdapter.searchClient;

const search = instantsearch({
    searchClient,
    indexName: "airports",
});

search.addWidgets([
    searchBox({
        container: '#searchbox',
        placeholder: 'Search for products',
    }),
    configure({
        insidePolygon : polygon,
    }),
]);
0
On

As of v2.1.0 of the Typesense Instantsearch adapter, you can use the configure InstantSearch.js widget for this, instead of passing it into the Typesense adapter.

Something like this:

const polygon = [
    42.01,-124.31,
    48.835509470063045,-124.40453125000005,
    45.01082951668149,-65.95726562500005,
    31.247243545293433,-81.06578125000004,
    25.924152577235226,-97.68234374999997,
    32.300311895879545,-117.54828125      
];

instantsearch.widgets.configure({
  insidePolygon: polygon,
});