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

57 Views Asked by At

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
}
1

There are 1 best solutions below

0
Glynn Bird On

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

{ 
  "email": "[email protected]",
  "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.