GeoSpatial & Keywords Query All in one

Dears,

What I want to accomplish:

  • I want to create a Query that will give me products found within 10 miles which fulfills certain preferences ( product type, manufacturedby, etc…)

The document is:
{
“doctype”: “product”,
“ver”: 1,
“name”: “Product ABC”,
“description”: “hello this product is for testing”,
“preferences”: {
“product_type@xyz”: 1,
“manufacturedby@abc”: 1,
“type@organic”: 1
},
“coordinates”: [
25.047288,
55.190023
]
}

I have put together the following view, but I am not sure that will give the best results in terms of performance, etc…:

function (doc, meta) {
if (doc.doctype ==“product” ){
if (doc.preferences && doc.coordinates){
for (kw in doc.preferences) {
var vars = kw.split(’@’);
if (vars[0] && vars[1]){
emit([{type: “Point”,coordinates: doc.coordinates},vars[0], vars[1]],doc);
}
}
}
}
}

Appreciate your feed-backs…
BR,
CK

Hi,

please note that the spatial views only support numeric values as key. This means that you would need to map the categories to numbers.

Another note: You normally don’t want to emit the full document as value. You would emit null and the get the full document via a get on its ID (the ID is always part of the query result).

Cheers,
Volker

Hi,

Thanks for the answer. So I will have to first fetch products within a specific geo range. After getting the document IDs of hundreds of products, I will pull the document IDs in an array and I will call an other view that matches the Document IDs array and specific preferences.

It is inevitable to make 2 round trips. I would have to say, NOSQL is like a newborn baby :slight_smile:
Thx a lot again.
CK

Hi,

you wouldn’t use another view to get the documents by ID, you would do a multiget, which is way faster.

Or you use one of the SDKs which have an include_docs parameter, which will do it for you automatically when a spatial view is queried. Please note that currently the support for spatial views within the SDKs is still in development, so you need to check whether the SDK you use already supports it.

Cheers,
Volker