Couchbase return null value after save and read document using n1ql

604 views Asked by At

I insert a document in couchbase using repository.save() and after that, I make a query to find duplicates on another document.

query is:

SELECT ARRAY_AGG(i.serialnumber) serialNumbers
FROM default tempItem
UNNEST items i
WHERE tempItem.class = "com.inventory.model.item.TempItem"
    AND META(tempItem).id = '4390dd9e-e392-4432-939f-ebf046570086'
    and i.serialnumber in (select raw serialnumber from default where class = 'com.inventory.model.item.Item'
    AND status != 'DELETED' and serialnumber is not missing)

the result of the query is:

[
  {
    "serialNumbers": [
      "9121945901",
      "9121955901",
      "9211965901"
    ]
  }
]

The document that saved is like below:

[
  {
    "tempItem": {
      "class": "com.inventory.model.item.TempItem",
      "items": [
        {
          "categoryId": "67aaca7b-90b1-43e4-a6c6-0e9567bf283e",
          "clientIds": [
            "919d0ca7-c8d4-4283-8b0a-b6f2a7b39753"
          ],
          "description": "bla bla",
          "initial": 1,
          "productId": "db5c81c4-0fec-407e-8703-6f5fb69a070c",
          "serialnumber": "9121945901",
          "simType": "PREPAID",
          "status": "ACTIVE",
          "stock": 1,
          "title": "bla bla"
        }
      ]
    }
  }
]

and another document to check is :

{
  "categoryId": "67aaca7b-90b1-43e4-a6c6-0e9567bf283e",
  "class": "com.inventory.model.item.Item",
  "clientIds": [
    "919d0ca7-c8d4-4283-8b0a-b6f2a7b39753"
  ],
  "createdts": 1601801989176,
  "creator": "919d0ca7-c8d4-4283-8b0a-b6f2a7b39753",
  "description": "bla bla",
  "initial": 1,
  "prefix1": "912",
  "prefix2": "194",
  "productId": "db5c81c4-0fec-407e-8703-6f5fb69a070c",
  "serialnumber": "9121945901",
  "simType": "PREPAID",
  "status": "ACTIVE",
  "stock": 1,
  "title": "bla bla"
}

in spring boot when I run the query immediately after seve the document its return null result and if I make some milliseconds sleep after save and before the run query returns the value

what is that problem? can anybody help this issue?

2

There are 2 answers

0
Matthew Groves On

This is probably because of ScanConsistency. Indexes in Couchbase are built asynchronously. So if you are using the default "NOT_BOUNDED" consistency and query the data with N1QL immediately after you write it, it may not be indexed yet.

I don't know how to change this in Spring, but the other options are:

  • REQUEST_PLUS - will likely take a bit longer to return the results but the query engine will make sure that it is as up-to-date as possible.
  • consistentWith(MutationState) a.k.a AT_PLUS - for a more narrowed-down scan consistency, depending on the index update rate this might provide a speedier response.

Again, not sure about Spring, but you don't have to set this globally. Each query can use a different scan consistency. So, if you value maximum performance over up-to-the-second accuracy, you can go with the default. If you value up-to-the-second accuracy over maximum performance, you can go to with REQUEST_PLUS or AT_PLUS.

0
Alessandro Vitale On

Using the CouchbaseRepository then it is possible to annotate every query using the annotation @ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS) to enforce the desidered Scan consistency on each query. Have a look at the official documentation Querying with consistency