0

The view definition emits a string field from the document as a key. The field value can be all numeric or alphanumeric. Query using key with all numeric value does not return any row but alphanumeric key returns data.

On server web console and rest api, I could see the row so view is getting updated properly and hence leaning to believe that issue is with java sdk client.

Below is the code I use to query.

CouchbaseClient couchBaseDAO; //  = initialize client.
String corelationId = "12345678";
Query query = new Query();
query.setKey(corelationId);
ViewResponse result = couchBaseDAO.query(queryConfig, query);
JSONArray jsonArray = new JSONArray();
if(result != null){
   for(ViewRow row: result){
     jsonArray.put(row.getValue());
   }
}
return jsonArray.toString();

Map:

function(doc,meta) {
   if(doc!=null && doc.requestData!=null) {
      emit(doc.requestData.corelationId, [doc.request.id, doc.status]);
   }
}

If I changed key to alphanumeric, it works.

String corelationId = "ab-12-09-a-123";

Java HotSpot 7. Couchbase java sdk 1.4.7 Couchbase Server 3.0.3

Solution

Based on the information given in answer below, below are two options you have

Option 1 Server side map change

If you are building a new map than go for it. Harmonize your key to become always string emit("" + doc.requestData.corelationId, ...);

If your view already exists then all your existing documents will not change right away.

Option 2 Client side change

If you are like me where option 1 is not possible, go for harmonizing your key in your code. It overcomes's skd's logic to treat it as numeric.

corelationId = StringUtils.isNumeric(corelationId)?"\""+corelationId+"\"":corelationId;
Jags
  • 799
  • 7
  • 19

1 Answers1

1

Your view emits the corelationId as it is, in its original type. You said that in the documents it was alternating between a numerical value and a string. If you pass the key to the SDK as a Long it will work.

(I suspect that in the web ui you naturally typed in 12345678 in the key field and not "12345678", so you did the correct equivalent of using a Long in the web UI)

If you cannot know the correct type to use for each key you search, harmonize the key type in the map function so that you know always to use strings:

emit("" + doc.requestData.corelationId, ...);
Simon Baslé
  • 27,105
  • 5
  • 69
  • 70
  • Thats strange. During set operation, in my Java POJO corelationid is defined as String variable. Then how come server is treating individual values differently (basically adding unnecessary intelligence ) Shouldn't server view processing detect that the values are string based on multiple documents scanning and treat it as String always? – Jags Jan 21 '16 at 16:46
  • what the server does is it stores what you emit in your map function (server side), creating an index. to the server, `emit(123)` and `emit("123")` are two different things, strict equality comparison won't match... hence the need to address that in the map function. – Simon Baslé Jan 21 '16 at 19:50
  • Thanks for the guidance. Since I can not change view (no access and lot of existing documents), I derived client side solution. I have edited my question with both solutions so that its helpful for people to choose whatever is best of them. – Jags Jan 22 '16 at 21:56