【问题标题】:Cloudant Search Index, query get false resultCloudant 搜索索引,查询得到错误结果
【发布时间】:2016-01-03 15:25:57
【问题描述】:

所以我有一个结构如下的 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"
     ]
   }
 }

如我们所见,结果包含工作站名称 16JT04 和 16JT10

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

【问题讨论】:

    标签: search lucene ibm-cloud cloudant nosql


    【解决方案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(即键)为“16JT02”的结果:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-12
        • 2022-11-02
        相关资源
        最近更新 更多