Cloudant搜索索引,查询获取错误结果

时间:2016-01-03 15:25:57

标签: search lucene ibm-cloud cloudant nosql

所以我有一个json文档,结构如下:

 {
   "_id": "xxx/room/BACK/reservation",
   "_rev": "9-096ac8d55a8fc0400111afdbcb71ff13",
   "reservationList": [
     {
       "contactPerson": {
         "_id": "xxx/hutomo",
         "fullName": "Hutomo",
         "email": "hutomo@xxx.com"
       },
       "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": "hutomo@xxx.com"
       },
       "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": "hutomo@xxx.com"
       },
       "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": "hutomo@xxx.com"
       },
       "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"
     }
   ]
 }

然后我使用这个函数创建了一个搜索索引:

 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});
   }
 }

使用此

运行lucene查询
 id:xxx* AND workstationName:16JT02
然后它返回

 {
   "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": [
       "hutomo@xxx.com",
       "hutomo@xxx.com",
       "hutomo@xxx.com",
       "hutomo@xxx.com"
     ],
     "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"
     ]
   }
 }

我们可以看到,结果包含workstationName 16JT04和16JT10

我做错了什么?以及如何解决这个问题?非常感谢= D

2 个答案:

答案 0 :(得分:1)

我相信这些可以回答你的问题:

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

基本上,你可以(a)将reservationList个内容分成他们自己的文档,或者(b)为workstationName(或任何所需的字段)创建一个视图:

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

使用该视图,您可以获得workstationName(即密钥)为&#34; 16JT02&#34;用:

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

答案 1 :(得分:0)

结果看起来正确。基于搜索索引的查询返回匹配的文档详细信息。如果您在查询中添加include_docs:true,则可以清楚地看到这一点。 您的要求似乎与Search Index_query可以提供的内容不匹配。正如其他人已经指出的那样,试试一下。