java N1QL二级索引不使用参数化IN子句
使用com.couchbase.client, java-client
version 2.2.7
我无法获得使用参数化IN子句的n1ql二级索引。请参阅下面的示例索引、查询和java代码
Index
CREATE INDEX `indexName` ON `bucketName`(id,docType) USING GSI ;
Query
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";
Code to submit Query
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);
...
}
在添加参数化之前,我的查询正确地使用了这个二级索引。如果我使用主索引,我的查询也可以工作
My question is this how do I create a secondary index which will be used by my query.
# 1 楼答案
索引中的第一个条目(在您的例子中是
id
)不能丢失。因此,缺少id
的文档将不在索引中。因此,如果不使用已受索引条件约束的字段,则必须指定不缺少该字段,以确保可以转到辅助索引例如,你可以用
type="entityType"
查询以下索引CREATE INDEX `indexName` ON `bucketName`(type) WHERE `type`="entityType"
# 2 楼答案
我通过添加一个额外的
is not missing
子句解决了这个问题,出于某种原因,这个问题得到了解决。同样的解决方案也适用于我。以下是更新后的查询: