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

huangapple go评论91阅读模式
英文:

Why are some of my Cloudant documents not being indexed when using Cloudant Search?

问题

我有许多类似的文档:

{ 
  "name": "Fred",
  "email": "fred@aol.com"
}

然后我使用类似以下索引函数的 Cloudant Search 索引它们:

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

在某些情况下,查询我的数据库中存在的电子邮件地址返回空。例如,对于搜索 q=email:bob@aol.com,不返回此文档:

{ 
  "email": "bob@aol.com",
  "provisional": true
}
英文:

I have a number of documents like this:

{ 
  "name": "Fred",
  "email": "fred@aol.com"
}

And I index them using Cloudant Search with an index function like this:

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

In some cases, queries for an email address that exists in my database returns nothing. e.g. this document is not returned for a search q=email:bob@aol.com:

{ 
  "email": "bob@aol.com",
  "provisional": true
}

答案1

得分: 3

When indexing data using Cloudant Search, it's important not to index undefined values. So your document:

{ 
  "email": "bob@aol.com",
  "provisional": true
}

has a missing name attribute, but your index document is attempting to index the missing name field - this will produce an error and no fields from that document will make it into the index. This is why searches on the document's email field fail.

The solution is to add "guard clauses" into your index function:

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

The "if" statements around each "index" function call will ensure that even if a field is missing from a document, it won't attempt to index an undefined value.

For more information on guard clauses, see the Cloudant documentation.

英文:

When indexing data using Cloudant Search, it's important not to index undefined values. So your document:

{ 
  "email": "bob@aol.com",
  "provisional": true
}

has a missing name attribute, but your index document is attempting to index the missing name field - this will produce an error and no fields from that document will make it into the index. This is why searches on the document's email field fail.

The solution is to add "guard clauses" into your index function:

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

The "if" statements around each "index" function call will ensure that even if a field is missing from a document, it won't attempt to index an undefined value.

For more information on guard clauses, see the Cloudant documentation.

huangapple
  • 本文由 发表于 2023年3月8日 19:32:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672477.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定