Sen
Sen

Reputation: 338

Cloudant Search Index, query get false result

So I have a json document structured like this:

 {
   "_id": "xxx/room/BACK/reservation",
   "_rev": "9-096ac8d55a8fc0400111afdbcb71ff13",
   "reservationList": [
     {
       "contactPerson": {
         "_id": "xxx/hutomo",
         "fullName": "Hutomo",
         "email": "[email protected]"
       },
       "start": "2015-12-29T12:00:00.000Z",
       "end": "2015-12-29T13:00:00.000Z",
       "title": "yuu",
       "workstationName": "16JT02",
       "id": 0,
       "reserveDate": "2015-12-18T12:00:00.000Z"
     },
     {
       "contactPerson": {
         "_id": "xxx/hutomo",
         "fullName": "Hutomo",
         "email": "[email protected]"
       },
       "start": "2015-12-29T12:00:00.000Z",
       "end": "2015-12-29T13:00:00.000Z",
       "title": "ajdjdn",
       "workstationName": "16JT10",
       "id": 1,
       "reserveDate": "2015-12-18T12:00:00.000Z"
     },
     {
       "contactPerson": {
         "_id": "xxx/hutomo",
         "fullName": "Hutomo",
         "email": "[email protected]"
       },
       "start": "2015-12-28T18:00:00.000Z",
       "end": "2015-12-28T20:00:00.000Z",
       "title": "gg",
       "workstationName": "16JT02",
       "id": 2,
       "reserveDate": "2015-12-18T12:00:00.000Z"
     },
     {
       "contactPerson": {
         "_id": "xxx/hutomo",
         "fullName": "Hutomo",
         "email": "[email protected]"
       },
       "start": "2015-12-28T22:00:00.000Z",
       "end": "2015-12-28T23:00:00.000Z",
       "title": "gg",
       "workstationName": "16JT04",
       "id": 3,
       "reserveDate": "2015-12-18T12:00:00.000Z"
     }
   ]
 }

Then I made a search index using this function:

 function (doc) {
   index("id", doc._id, {"index":true});
   for (var i = 0; i<doc.reservationList.length;i++) 
   { 
     index("workstationName", doc.reservationList[i].workstationName, {"store":true,"index":true});

     index("contactPerson._id", doc.reservationList[i].contactPerson._id, {"store": true, "index":false}); 
     index("contactPerson.fullName", doc.reservationList[i].contactPerson.fullName, {"store": true, "index":false});
     index("contactPerson.email", doc.reservationList[i].contactPerson.email, {"store": true, "index":false});

     if(doc.reservationList[i].contactPerson.mobilePhone)
     index("contactPerson.mobilePhone", doc.reservationList[i].contactPerson.mobilePhone, {"store": true, "index":false});

     index("reservationID", doc.reservationList[i].id,{"store": true,"index":false});
     index("start",doc.reservationList[i].start,{"store": true,"index":false});
     index("end",doc.reservationList[i].end,{"store": true,"index":false});
     index("title",doc.reservationList[i].title,{"store": true,"index":false});
   }
 }

Running lucene query with this

 id:xxx* AND workstationName:16JT02

then it returns

 {
   "id": "xxx/room/BACK/reservation",
   "order": [
     1.0196553468704224,
     0
   ],
   "fields": {
     "contactPerson._id": [
       "xxx/hutomo",
       "xxx/hutomo",
       "xxx/hutomo",
       "xxx/hutomo"
     ],
     "workstationName": [
       "16JT04",
       "16JT02",
       "16JT10",
       "16JT02"
     ],
     "reservationID": [
       3,
       2,
       1,
       0
     ],
     "end": [
       "2015-12-28T23:00:00.000Z",
       "2015-12-28T20:00:00.000Z",
       "2015-12-29T13:00:00.000Z",
       "2015-12-29T13:00:00.000Z"
     ],
     "title": [
       "gg",
       "gg",
       "ajdjdn",
       "yuu"
     ],
     "contactPerson.email": [
       "[email protected]",
       "[email protected]",
       "[email protected]",
       "[email protected]"
     ],
     "start": [
       "2015-12-28T22:00:00.000Z",
       "2015-12-28T18:00:00.000Z",
       "2015-12-29T12:00:00.000Z",
       "2015-12-29T12:00:00.000Z"
     ],
     "contactPerson.fullName": [
       "Hutomo",
       "Hutomo",
       "Hutomo",
       "Hutomo"
     ]
   }
 }

As we can see, the result contains workstationName 16JT04 and 16JT10

what did i do wrong? and how to fix this? Thanks a lot =D

Upvotes: 0

Views: 304

Answers (2)

Sora
Sora

Reputation: 409

The result looks right . Query based on Search Index is returning the matching document details. If you add include_docs:true to your query , you can see this clearly. It seems your requirement is not matching with what Search Index_query can offer. As others pointed out already, try a view.

Upvotes: 0

vabarbosa
vabarbosa

Reputation: 706

i believe these may answer your question:

https://stackoverflow.com/a/16336255
https://stackoverflow.com/a/16466134

basically, you could (a) separate reservationList content into their own document or (b) create a view for workstationName (or whatever fields desired):

function(doc) {
  if(doc.reservationList) {
    doc.reservationList.forEach(function(reservationList) {
      emit(reservationList.workstationName,reservationList);
    });
  }
}

using the view, you can get results where workstationName (i.e., key) is "16JT02" with:

https://[username].cloudant.com/[db_name]/_design/[designdoc_name]/_view/[view_name]?key="16JT02"

Upvotes: 1

Related Questions