1

I have a map() function like this in beer design document:

function (doc, meta) {

    if(doc.brewery_id)
    emit([ doc.brewery_id, doc.abv], [doc.name, doc.abv, doc.type, doc.brewery_id, doc.style, doc.category]);
}

I need to get all doc with 2 rules:

1.[brewery_id] start with "21st"

2.[abv] between 3-4

My filter is:

startkey=["21st", 3]
endkey=["21st\uefff", 4]

But the result is not correct, the rule 1 is work as expected but the rule 2 is ignored. Please help me find out what's wrong.

Thanks!!!

Hear's the result

{"total_rows":5891,"rows":[
{"id":"21st_amendment_brewery_cafe-bitter_american","key":["21st_amendment_brewery_cafe",3.6],"value":["Bitter American",3.6,"beer","21st_amendment_brewery_cafe","Special Bitter or Best Bitter","British Ale"]},
{"id":"21st_amendment_brewery_cafe-563_stout","key":["21st_amendment_brewery_cafe",5],"value":["563 Stout",5,"beer","21st_amendment_brewery_cafe","American-Style Stout","North American Ale"]},
{"id":"21st_amendment_brewery_cafe-south_park_blonde","key":["21st_amendment_brewery_cafe",5],"value":["South Park Blonde",5,"beer","21st_amendment_brewery_cafe","Golden or Blonde Ale","North American Ale"]},
{"id":"21st_amendment_brewery_cafe-amendment_pale_ale","key":["21st_amendment_brewery_cafe",5.2],"value":["Amendment Pale Ale",5.2,"beer","21st_amendment_brewery_cafe","American-Style Pale Ale","North American Ale"]},
{"id":"21st_amendment_brewery_cafe-potrero_esb","key":["21st_amendment_brewery_cafe",5.2],"value":["Potrero ESB",5.2,"beer","21st_amendment_brewery_cafe","Special Bitter or Best Bitter","British Ale"]},
{"id":"21st_amendment_brewery_cafe-general_pippo_s_porter","key":["21st_amendment_brewery_cafe",5.5],"value":["General Pippo's Porter",5.5,"beer","21st_amendment_brewery_cafe","Porter","Irish Ale"]},
{"id":"21st_amendment_brewery_cafe-watermelon_wheat","key":["21st_amendment_brewery_cafe",5.5],"value":["Watermelon Wheat",5.5,"beer","21st_amendment_brewery_cafe","Belgian-Style Fruit Lambic","Belgian and French Ale"]},
{"id":"21st_amendment_brewery_cafe-north_star_red","key":["21st_amendment_brewery_cafe",5.8],"value":["North Star Red",5.8,"beer","21st_amendment_brewery_cafe","American-Style Amber/Red Ale","North American Ale"]},
{"id":"21st_amendment_brewery_cafe-oyster_point_oyster_stout","key":["21st_amendment_brewery_cafe",5.9],"value":["Oyster Point Oyster Stout",5.9,"beer","21st_amendment_brewery_cafe","American-Style Stout","North American Ale"]},
{"id":"21st_amendment_brewery_cafe-21a_ipa","key":["21st_amendment_brewery_cafe",7.2],"value":["21A IPA",7.2,"beer","21st_amendment_brewery_cafe","American-Style India Pale Ale","North American Ale"]}
]
}
Phillip
  • 141
  • 8
  • possible duplicate of [Composite views in couchbase](http://stackoverflow.com/questions/17076267/composite-views-in-couchbase). See my answer for it an an explanation by Filipe Manana. Short answer: couchbase has only one index in a view, but you try to use 2. – m03geek Aug 05 '13 at 08:03
  • I don't think so, as [link](http://www.couchbase.com/docs/couchbase-manual-2.0/couchbase-views-sample-patterns-multiemit.html), we can see the ?startkey=["carrot",0]&endkey=["carrot",20], the filter is base on 2 fields – Phillip Aug 05 '13 at 08:41
  • No. You don't understand me. In ?startkey=["carrot",0]&endkey=["carrot",20] first param is equal in both keys. In your example you try to use 2 ranges, i.e. ?startkey=["carrot-1",0]&endkey=["carrot-20",20]. It's 2 different things. I gave you a link to the same question. If you read it carefully and follow link to Filipe's answer, you'll understand why it's 2 different things. – m03geek Aug 05 '13 at 14:03
  • Thanks! Now I understand. But my customers want it like that, the solution that I've found out is get the view with the index 1 (rule 1), then using LinQ to filter all the next rules from the result of GetView function. Please let me know if I'm wrong. – Phillip Aug 06 '13 at 03:09

1 Answers1

1

If you need to filter your results by 2 varying ranges you can use LinQ, but if you have large number of documents it can be slow. So to make it faster you can do two things:

  1. After applying LinQ "filter" cache results in memcached or couchbase.

  2. If your datamodel allows you to create separate view for one of the ranges, i.e. if you can move one of your ranges from key to map function if like: View for 21sts:

map: function() { if (doc.subtype === "21sts") emit (doc.abv,null) }

where docs that have subtype == "21sts" are docs that you can get from view with:

map: function() { emit(doc.brewery_id, null) }

and startkey="21st", endkey="21st\uefff".

m03geek
  • 2,508
  • 1
  • 21
  • 41