为什么我的一些 Cloudant 文档在使用 Cloudant 搜索时没有被索引?

问题描述 投票:0回答:1

我有很多这样的文件:

{ 
  "name": "Fred",
  "email": "[email protected]"
}

我使用 Cloudant Search 对它们进行索引,索引函数如下:

function(doc) {
  index("name", doc.name)
  index("email", doc.email)
}

在某些情况下,查询我的数据库中存在的电子邮件地址不会返回任何内容。例如该文档未返回进行搜索

q=email:[email protected]

{ 
  "email": "[email protected]",
  "provisional": true
}
database ibm-cloud couchdb cloudant
1个回答
1
投票

使用 Cloudant Search 为数据编制索引时,重要的是不要为

undefined
值编制索引。所以你的文件:

{ 
  "email": "[email protected]",
  "provisional": true
}

缺少

name
属性,但您的索引文档正在尝试索引缺少的
name
字段 - 这将产生错误并且 该文档中的任何字段都不会进入索引。这就是搜索文档的电子邮件字段失败的原因。

解决方案是在您的索引函数中添加“保护条款”:

function(doc) {
  if (doc.name) {
    index("name", doc.name)
  }
  if (doc.email) {
    index("email", doc.email)
  }
}

围绕每个“索引”函数调用的“if”语句将确保即使文档中缺少某个字段,它也不会尝试索引

undefined
值。

有关保护条款的更多信息,请参阅 Cloudant 文档

© www.soinside.com 2019 - 2024. All rights reserved.