Same index definition, different results

Sorry if there’s something obvious I’m missing. I have two indices with the same definition but they give different results. With the same query, the first one only pulls up one matching document, while the second one pulls up 2 (correctly).

The first one was created before any documents were added to the collection. The second one was created after two matching documents were added to the collection. It’s been at least 5 minutes now, so I don’t believe it’s a caching issue. Is there something I’m missing?

Index 1:

{
  name: "emails_by_orgId",
  unique: false,
  serialized: true,
  source: "emails",
  terms: [
    {
      field: ["data", "orgId"]
    }
  ]
}

Index 2:

{
  name: "emails_by_orgId_test",
  unique: false,
  serialized: true,
  source: "emails",
  terms: [
    {
      field: ["data", "orgId"]
    }
  ]
}

Query 1:

Count(Match(Index("emails_by_orgId_test"), "302733096327840256"))

(result is 2)

Query 2:

Count(Match(Index("emails_by_orgId"), "302733096327840256"))

(result is 1)

1 Like

When creating an index, it is possible to set active: true. When you do so, that tells the indexing infrastructure that you want the index to created and ready for use immediately, without investigating the index’s source to look for existing documents. That could, potentially, explain the mismatch that you are seeing.

The remedy is simple (whatever the cause): update the documents that you think are missing from a particular index, and the update will cause them to become indexed.

You could try a query like this:

Map(
  Paginate(Documents(Collection("emails"))),
  Lambda(
    "ref",
    Do(
      Update(Var("ref"), { data: { atemporaryfield: true } }),
      Update(Var("ref"), { data: { atemporaryfield: null } }),
    )
  )
)

That query paginates through the documents in the emails collection (the default pagination size is 64; increase the size if you have more documents, up to 100K), and for each document, it is updated with a (hopefully) novel field that needs to be added, and is updated again setting that field to null to remove it from the document.

Hi Ryan,

Did Paul and Ewan’s suggestions answer your question? Is there anything else you need help with? If not we’d like to mark this question as Answered.

Cory