Question


I have wirtten a program to manage tv series and I am stuck at an issue with lightcouch and a specific database query. This is what I have so far. To setup the database views I used the following lines:

MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
  "function(doc) { "
   + "  emit(doc.seriesName, doc.season)"
   + "}");
get_numberOfSeasonsMR.setReduce(
  "function (key, values, rereduce) {"
    + "return Math.max.apply({}, values)"
    + "}");

map.put("get_numberOfSeasons", get_numberOfSeasonsMR);

In Futon everything appears normal (see http://i.stack.imgur.com/1hgSJ.png).

However, when I try to execute the following line, I get an exception, instead of the results that appear in Futon.

int nr = client.view("design/get_numberOfSeasons").key("Arrow").queryForInt();

Exception:

org.lightcouch.NoDocumentException: Expecting exactly a single result of this view query, but was: 0
org.lightcouch.View.queryValue(View.java:246)
org.lightcouch.View.queryForInt(View.java:219)
....db.Server.getNumberOfSeasons(Server.java:237)
...

I tried to emit Strings in my map() function instead on ints, but it did not make any difference. What am I doing wrong? Or can someone post an example of a successful lightcouch map()+reduce() operation? The tutorials I found only used map() without reduce().

Thanks in advance ;)

Was it helpful?

Solution

Nothing seems wrong with your code, here is the full version:

CouchDbClient dbClient = new CouchDbClient();

DesignDocument designDocument = new DesignDocument();
designDocument.setId("_design/mydesign");
designDocument.setLanguage("javascript");

MapReduce get_numberOfSeasonsMR = new MapReduce();
get_numberOfSeasonsMR.setMap(
  "function(doc) { "
   + "  emit(doc.seriesName, doc.season)"
   + "}");
get_numberOfSeasonsMR.setReduce(
  "function (key, values, rereduce) {"
    + "return Math.max.apply({}, values)"
    + "}");

Map<String, MapReduce> view = new HashMap<>();
view.put("get_numberOfSeasons", get_numberOfSeasonsMR);

designDocument.setViews(view);

dbClient.design().synchronizeWithDb(designDocument);

int count = dbClient.view("mydesign/get_numberOfSeasons").key("Arrow").queryForInt();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top