3

I'm using com.couchbase.client maven dependency in java spring project. I fetch a couchbase document from code in following way:

JsonDocument document = bucket.get(id); //matches id case sensitively

But i've got a new requirement. I have to fetch the documents based on case insensitive matching of id.

For example: Let's say couchbase has a document with id heLLo_Doc

When i execute bucket.get("hello_doc") i want the document with id heLLo_Doc to be returned. If there are more than one document with same lowercase id(ex: Hello_Doc,hEllo_doc etc) i need to get all those documents.

Is there any method provided by couchbase to do this?

The worst way to do this is to search for all possibilities, that i don't want to do.

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
manish reddy
  • 157
  • 1
  • 11

2 Answers2

3

There is not such a feature if you just want to use the KV engine. But, you can still add a lowercase version of your id into your document and search it via n1ql with "LOWER" https://docs.couchbase.com/server/5.5/n1ql/n1ql-language-reference/stringfun.html#fn-str-lower.

if you need to bring variations of your ID, like 'HelloDoc', you also can use Full-Text search with some fuzziness level: https://blog.couchbase.com/fuzzy-matching/

deniswsrosa
  • 2,421
  • 1
  • 17
  • 25
  • I can use n1ql with "LOWER" but what if the document id has upper case letters in couchbase. And also can you explain what is a KV engine? – manish reddy Oct 17 '18 at 11:31
  • 1
    You will need to also store it with lower or apply it during the comparison, like "lower(myId) = lower(Hello_Doc) ". About the key-value store checkout this answer https://stackoverflow.com/questions/26527466/what-is-the-key-value-storage-in-couch-base – deniswsrosa Oct 17 '18 at 11:50
3

If you want use N1QL. You need to create functional index on document key

CREATE INDEX ix1 ON default(LOWER(META().id));
SELECT * FROM default WHERE LOWER(META().id) = "hello_doc";

OR

SELECT META().id FROM default WHERE LOWER(META().id) = "hello_doc";
The second query gives actual document keys then you can pass this to your existing bucket.get() call too.
vsr
  • 7,149
  • 1
  • 11
  • 10
  • This will work. Thanks @[vsr](https://stackoverflow.com/users/6080536/vsr). Doesn't couchbase SDK provides a method for this kind of id matching? (this will help if i don't want to use N1QL queries) – manish reddy Oct 30 '18 at 13:23