Couchbase is all about enabling more and more enterprise applications to leverage and adopt NoSQL/JSON data model. N1QL simplifies this transition from traditional Relational databases, and is built with tons of features to achieve best of both worlds. Continuing the train of Couchbase Server 4.5, the 4.5.1 release brings multiple functionality, usability and performance improvements in N1QL. These enhancements address many of our customer critical issues, and in general showcase the strength & sophistication of N1QL. Kudos to N1QL team !!
While some of the new improvements enhance existing functionality, others such as SUFFIXES() function enrich N1QL querying with magnitude performance improvement to LIKE queries. Further improvements enhance dynamic creation & manipulation of JSON objects, precision of numbers, UPDATE syntax for nested arrays etc.,
I am sure, this will need a series, but in this blog I will highlight some of the salient N1QL features in 4.5.1. See Couchbase Server 4.5.1what’s newandrelease notes for full list of N1QL enhancements.
Efficient pattern matching LIKE queries with SUFFIXES()Pattern matching is a widely used functionality in SQL queries, and is typically achieved using the LIKE operator. Especially, efficient wildcard matching is very important. LIKE ‘foo%’ can be implemented efficiently with a standard index, but not LIKE ‘%foo%’. Such pattern matching with leading wildcard is vital, for every application that has a search box to match partial words or to smart-suggest matching text. For example,
A travel booking site, that wants to pop-up matching airports as the user starts to enter few letters of the airport name. A user finding all e-mails with a specific word or partial word in the subject. Finding all topics of a forum or blog posts with specific keywords in the title.In Couchbase Server 4.5.1, N1QL addresses this problem by adding a new string function SUFFIXES(), and combining that with the Array Indexing functionality introduced in Couchbase Server 4.5. Together, it brings magnitude difference to performance of LIKE queries with leading wildcards such as LIKE "%foo%". Core functionality of SUFFIXES() is very simple, basically it produces an array of all possible suffix substrings of a given string. For example,
SUFFIXES("N1QL") = [ "N1QL", "1QL", "QL", "L" ]Following picture depicts the unique technique to leverage SUFFIXES() function with Array Indexing to achieve the LIKE query performance.

Step1 (in blue) shows the array of suffix substrings generated by SUFFIXES() for doc1.title Step2 (in green) shows theArray Index created with the suffix substrings generated in step1. Note that substring "wood" points to doc1 and doc4, as that is one of the suffixes for titles of both the documents. Similarly, "ood" points to doc1, doc4, and doc8. Step3 runs a query equivalent to pattern matching for LIKE "%wood%". This predicate is converted touse the array index with the ANY predicate. Note that, the leading wildcard is removed in the newLIKE "wood%" predicate. This is accurate transformation, because the array index entry for " wood" points to all documents whose title hastrailing substring"wood" In Step4, N1QL looksup inthe Array Index to find all documents matching "wood%". That returns {doc1, doc3, doc4}, because the index lookup produces followingspan, which gets documents from "wood" to "wooe" doc1 and doc4 are matched because of index entry "wood" that is generated by the SUFFIXES() when creating the array index. doc3 is matched because of its respective entry for "woodland" Finally, in step5,N1QL returnsthe query results.
Let's see a working example with the travel-sample documents, whichalso shows a 12x boost in performance for the query.
Assume a document with a string field whose value is few words of text or a phrase. For example, title of a landmark, address of a place, name of restaurant, full name of a person/place etc., For this explanation, we consider title of landmark documents in travel-sample . Create secondary index on title field using SUFFIXES() as: CREATE INDEX idx_title_suffix ON `travel-sample`(DISTINCT ARRAY s FOR s IN SUFFIXES(title) END) WHERE type = "landmark";SUFFIXES(title) generates all possible suffix substrings of title , and the index will have entries for each of those substrings, all referencing to corresponding documents.
Now consider following query, which finds all docs with substring "land" in title .This query produces following plan, and runs in roughly 120ms in my laptop. You can clearly see, it fetches landmark documents, and then apply the LIKE predicate to find all titles that have matching substring "land" . EXPLAIN SELECT * FROM `travel-sample` USE INDEX(def_type) WHERE type = "landmark" AND title LIKE "%land%"; [ { "plan": { "#operator": "Sequence", "~children": [ { "#operator": "IndexScan", "index": "def_type", "index_id": "e23b6a21e21f6f2", "keyspace": "travel-sample", "namespace": "default", "spans": [ { "Range": { "High": [ "\"landmark\"" ], "Inclusion": 3, "Low": [ "\"landmark\"" ] } } ], "using": "gsi" }, { "#operator": "Fetch", "keyspace": "travel-sample", "namespace": "default" }, { "#operator": "Parallel", "~child": { "#operator": "Sequence", "~children": [ { "#operator": "Filter", "condition": "(((`travel-sample`.`type`) = \"landmark\") and ((`travel-sample`.`title`) like \"%land%\"))" } ] } In Couchbase 4.5.1, this query can be rewritten to leverage the array index idx_title_suffix created in (2) above. EXPLAIN SELECT title FROM `travel-sample` USE INDEX(idx_title_suffix) WHERE type = "landmark" AND ANY s IN SUFFIXES(title) SATISFIES s LIKE "land%" END; [ { "plan": { "#operator": "Sequence", "~children": [ { "#operator": "DistinctScan", "scan": { "#operator": "IndexScan", "index": "idx_title_suffix", "index_id": "75b20d4b253214d1", "keyspace": "travel-sample", "namespace": "default", "spans": [ { "Range": { "High": [ "\"lane\"" ], "Inclusion": 1, "Low": [ "\"land\"" ] } } ], "using": "gsi" } }, { "#operator": "Fetch", "keyspace": "travel-sample", "namespace": "default" }, { "#operator": "Parallel", "~child": { "#operator": "Sequence", "~children": [ { "#operator": "Filter", "condition": "(((`travel-sample`.`type`) = \"landmark\") and any `s` in suffixes((`travel-sample`.`title`