I have a number of documents like this:
{
"name": "Fred",
"email": "[email protected]"
}
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:[email protected]:
{
"email": "[email protected]",
"provisional": true
}
When indexing data using Cloudant Search, it's important not to index
undefinedvalues. So your document:has a missing
nameattribute, but your index document is attempting to index the missingnamefield - 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:
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
undefinedvalue.For more information on guard clauses, see the Cloudant documentation.