如何以编程方式使用Java客户端创建和发布索引

问题描述 投票:2回答:3

是否可以使用Couchbases Java Client 2.2.2以编程方式创建和发布二级索引?我希望能够创建和发布我的自定义二级索引Running Couchbase 4.1。我知道这可能与Couchbase Views有关,但我找不到相同的索引。

java indexing couchbase couchbase-java-api
3个回答
4
投票

需要couchbase-java-client-2.3.1才能以编程方式创建主要或次要索引。一些可用的方法可以在用于upsert视图的bucketManger上找到。另外,静态方法createIndex可以用于支持DSL和String语法

有几个选项可用于创建二级索引。

选项1:

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

选项#2:

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

选项#3(实际上这里有多个选项,因为方法createN1qlIndex过载了

bucket.bucketManager().createN1qlIndex(indexName, fields, where, true, false);

2
投票

主要指标:

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

二级指数:

    // 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"));

要么

    // 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"));

如果您的文档是这样的,那么第一个辅助索引(my_idx_1)会很有用:

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

如果您的文档是这样的,第二个辅助索引(my_idx_2)会很有用:

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

1
投票

一旦你有了一个桶,你应该能够用任何2.x做到这一点

bucket.query(N1qlQuery.simple(的queryString))

其中queryString是这样的

String queryString =“使用GSI创建" + bucketName + "的主要索引;”;

© www.soinside.com 2019 - 2024. All rights reserved.