【发布时间】:2016-08-02 22:33:47
【问题描述】:
使用com.couchbase.client, java-client 版本2.2.7 我无法使使用参数化IN 子句的n1ql 二级索引正常工作。请参阅下面的示例索引、查询和 java 代码
索引
CREATE INDEX `indexName` ON `bucketName`(id,docType) USING GSI ;
查询
public static final String COUNT_STATEMENT = "select count(*) as count " +
"from bucketName " +
"where docType = 'docId' " +
"and id IN $ids " +
"and publishTimestamp between $startTime and $endTime";
提交查询的代码
public int getCountForDuration(Long startTime, Long endTime, Collection<String> ids){
List<String> idList = new ArrayList<>(ids);
JsonObject placeHolders = JsonObject.create()
.put("ids", JsonArray.from(idList))
.put("startTime", startTime)
.put("endTime", endTime);
N1qlQuery query = N1qlQuery.parameterized(COUNT_STATEMENT, placeHolders)
N1qlQueryResult result = bucket.query(query);
...
}
在添加参数化之前,我的查询正确使用了这个二级索引。如果我使用主索引,我的查询也有效。
我的问题是如何创建二级索引 将被我的查询使用。
【问题讨论】:
-
您是否在 IN 的右侧使用了固定数量的参数?也就是说,你的数组中有固定数量的元素吗?
-
嘿,@geraldss 不,传递给我的集合中没有固定数字我无法保证用户将在 ids 变量中传递多少元素
-
@geraldss 重读您的评论我不确定我的示例是否符合固定条件。在运行时,当此方法被调用时,它将在调用该方法时被修复;但是大小可以根据不同的方法调用而改变。一个调用可能有 5 个 id,而下一个调用可能只传入 1 个
标签: java couchbase n1ql secondary-indexes