3

Is it possible to programmatically create and publish secondary indexes using Couchbases Java Client 2.2.2? I want to be able to create and publish my custom secondary indexes Running Couchbase 4.1. I know this is possible to do with Couchbase Views but I can't find the same for indexes.

Marquis Blount
  • 7,585
  • 8
  • 43
  • 67

4 Answers4

6

couchbase-java-client-2.3.1 is needed in order to programmatically create indexes primary or secondary. Some of the usable methods can be found on the bucketManger same that is used to upsert views. Additionally the static method createIndex can be used it support DSL and String syntax

There are a few options to create your secondary indexes.

Option #1:

Statement query = createIndex(name).on(bucket.name(), x(fieldName));
N1qlQueryResult result = bucket.query(N1qlQuery.simple(query));

Option #2:

String query = "BUILD INDEX ON `" + bucket.name() + "` (" + fieldName + ")";
N1qlQueryResult result = bucket.query(N1qlQuery.simple(query));

Option #3 (Actually multiple options here since method createN1qlIndex is overloaded

bucket.bucketManager().createN1qlIndex(indexName, fields, where, true, false);
Marquis Blount
  • 7,585
  • 8
  • 43
  • 67
4

Primary index:

// Create a N1QL Primary Index (ignore if it exists)
bucket.bucketManager().createN1qlPrimaryIndex(true /* ignore if exists */, false /* defer flag */);

Secondary Index:

    // Create a N1QL Index (ignore if it exists)
    bucket.bucketManager().createN1qlIndex(
            "my_idx_1",
            true, //ignoreIfExists
            false, //defer
            Expression.path("field1.id"),
            Expression.path("field2.id"));

or

    // Create a N1QL Index (ignore if it exists)
    bucket.bucketManager().createN1qlIndex(
            "my_idx_2",
            true, //ignoreIfExists
            false, //defer
            new String ("field1.id"),
            new String("field2.id"));

The first secondary index (my_idx_1) is helpful if your document is something like this:

{
    "field1" : {
        "id" : "value"
    },
    "field2" : {
        "id" : "value"
    }
}

The second secondary index (my_idx_2) is helpful if your document is something like this:

{
    "field1.id" : "value",
    "field2.id" : "value"
}
Pradeep Anchan
  • 1,469
  • 1
  • 11
  • 11
1

You should be able to do this with any 2.x, once you have a Bucket

bucket.query(N1qlQuery.simple(queryString))

where queryString is something like

String queryString = "CREATE PRIMARY INDEX ON " + bucketName + " USING GSI;";

1

As of java-client 3.x+ there is a QueryIndexManager(obtained via cluster.queryIndexes()) which provides an indexing API with the below specific methods to create indexes:

createIndex(String bucketName, String indexName, Collection<String> fields)
createIndex(String bucketName, String indexName, Collection<String> fields, CreateQueryIndexOptions options)
createPrimaryIndex(String bucketName)
createPrimaryIndex(String bucketName, CreatePrimaryQueryIndexOptions options)
Roman Sinyakov
  • 559
  • 1
  • 6
  • 24