Couchbase中N1QL查询和索引的问题

时间:2018-06-04 13:54:06

标签: couchbase spring-data-couchbase

我有一个简单的N1QL查询:

"select xrefId, CLSTR, CLUSTER_NAME, SHORTENED_NAME, "
        + "PRMRY, SET_NUM, TYPE from myBucket "
        + "where _class=\"com.package.model.ClusterCodeXref\"

以及相应的二级索引(由于内部原因,我不允许在此存储桶上创建主索引):

CREATE INDEX `idx_xref` ON `myBucket`(`xrefId`,`CLSTR`,`CLUSTER_NAME`,
`SHORTENED_NAME`,`PRMRY`,`SET_NUM`,`TYPE`)
where _class=`com.package.model.ClusterCodeXref` using GSI

然而,当我运行查询(使用spring-data-couchbase)时,我收到以下错误:

"No index available on keyspace myBucket that matches your query. 
Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online."

有人可以解释什么是错的吗?

2 个答案:

答案 0 :(得分:3)

需要在Query WHERE子句中出现前导索引键和Index WHERE子句以选择索引。 索引where子句使用反向标记(它表示文档中的字段)。将它们更改为双引号并添加_class作为前导索引键,如下所示。

CREATE INDEX `idx_xref` ON `myBucket`(_class, `xrefId`,`CLSTR`,`CLUSTER_NAME`,
`SHORTENED_NAME`,`PRMRY`,`SET_NUM`,`TYPE`)
where _class="com.package.model.ClusterCodeXref" using GSI

答案 1 :(得分:0)

尝试:

SELECT custom_data.xrefId, custom_data.CLSTR, custom_data.CLUSTER_NAME, custom_data.SHORTENED_NAME, custom_data.PRMRY, custom_data.SET_NUM, custom_data.TYPE
FROM `myBucket` AS custom_data
WHERE  (`_class` = "com.package.model.ClusterCodeXref");

字符串格式:

"SELECT custom_data.xrefId, custom_data.CLSTR, "
      + "custom_data.CLUSTER_NAME, custom_data.SHORTENED_NAME, " 
      + "custom_data.PRMRY, custom_data.SET_NUM, " 
      + "custom_data.TYPE "
      + "FROM `myBucket` AS custom_data "
      + "WHERE  (`_class` = \"com.package.model.ClusterCodeXref\");"