英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论