gpt4 book ai didi

java - N1QL 二级索引不适用于参数化 IN 子句

转载 作者:行者123 更新时间:2023-12-01 18:03:34 24 4
gpt4 key购买 nike

使用com.couchbase.client,java-client版本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.

最佳答案

索引中的第一个条目(在您的情况下id)不能丢失。因此缺少 id 的文档将不会出现在索引中。因此,如果您不使用已受索引条件约束的字段,则必须指定该字段不丢失,以确保可以转到您的二级索引。

例如您只需使用 type="entityType"

即可查询以下索引

在 `bucketName`(type) 上创建索引 `indexName`,其中 `type`="entityType"

关于java - N1QL 二级索引不适用于参数化 IN 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731538/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com