Efficient Indexing with Couchbase in Angular Service

Demonstrate how to create and use efficient indexing in Couchbase through an Angular service to speed up queries.
import { Injectable } from '@angular/core';
import { Couchbase } from 'ngx-couchbase';

  providedIn: 'root'
export class CouchbaseService {
  private bucket: any;

  constructor() {
    // Initialize Couchbase connection
    this.bucket = new Couchbase('bucket_name');

  createIndex() {
    // Define a N1QL primary index on the bucket for improved performance
    this.bucket.query('CREATE PRIMARY INDEX ON `bucket_name`',
    function (err, result) {
      if (err) {
        console.error('Index creation failed', err);
      console.log('Index created successfully', result);

  // Function to execute a query using the index
  executeQuery(queryStr: string) {
    return new Promise((resolve, reject) => {
      this.bucket.query(queryStr, function(err, rows, meta) {
        if (err) {
        } else {
This Angular service includes methods for creating a primary index and executing queries using the Couchbase SDK. The 'createIndex()' method initializes a primary index to enhance query performance. The 'executeQuery(queryStr: string)' method runs a N1QL query string against the Couchbase server and returns a promise that resolves with the query results.