如何使用 typesense 即时搜索适配器在过滤器多边形内使用地理位置
How to use geo location within filter polygon using typesense instantsearch adapter
我用 typesense 创建了一个演示即时搜索 js。
问题是当我搜索城市时,所有结果都没有使用 _geoloc 和多边形进行过滤。
我使用 _geoloc 字段在 typesense 中存储带有 float 数组的经纬度。
{"name": "_geoloc", "type": "float[]" , "facet": true },
并且 _geoloc 在 Typesense instantSearch 适配器中传递 geoLocationField 参数。
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",
},
});
从 v2.1.0 of the Typesense Instantsearch adapter, you can use the configure
InstantSearch.js 开始,这个小部件,而不是将其传递到 Typesense 适配器。
像这样:
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,
});
感谢您帮助@ErJab
经过一些研究,我得到了一个新的解决方案,而且它运行良好。
Typesense 适配器更新了他们的多边形搜索代码。
现在我们可以在多边形内部搜索了。
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,
}),
]);
我用 typesense 创建了一个演示即时搜索 js。
问题是当我搜索城市时,所有结果都没有使用 _geoloc 和多边形进行过滤。
我使用 _geoloc 字段在 typesense 中存储带有 float 数组的经纬度。
{"name": "_geoloc", "type": "float[]" , "facet": true },
并且 _geoloc 在 Typesense instantSearch 适配器中传递 geoLocationField 参数。
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",
},
});
从 v2.1.0 of the Typesense Instantsearch adapter, you can use the configure
InstantSearch.js 开始,这个小部件,而不是将其传递到 Typesense 适配器。
像这样:
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,
});
感谢您帮助@ErJab
经过一些研究,我得到了一个新的解决方案,而且它运行良好。
Typesense 适配器更新了他们的多边形搜索代码。
现在我们可以在多边形内部搜索了。
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,
}),
]);