在使用Java SDK 4.7.10,以下编码显示出问题

private String keyword = "Kylie cosmetics";
private void fetchOurOwnData() {
    AVQuery query = new AVQuery("Products");
// Test 1 & Test 3
    query.whereContains("searchString", keyword.toLowerCase().split(" ")[0]);
// Test 2 & Test 3
    query.whereContains("searchString", keyword.toLowerCase().split(" ")[1]);
    query.countInBackground(new CountCallback() {
        @Override
        public void done(int count, AVException e) {
            if (e != null) {
                Log.d("reload", "1 - " + e.getLocalizedMessage());
            } else {
                Log.d("Result", "Count: " + String.valueOf(count));
            }
        }
    });
}

在 test1 中,使用以下代码,获得count = 32

query.whereContains("searchString", keyword.toLowerCase().split(" ")[0]);

Log

D/===AVOS Cloud===: LogUtil$avlog->d->43: curl -X GET -H "X-LC-Id: IDX4LztPPtMsBK7gim8UJo7s-gzGzoHsz" -H "X-LC-Key: YourAppKey" -H "" -G --data-urlencode 'count=1&limit=0&where={"searchString":{"$regex":".*kylie.*"}}' https://idx4lztp.api.lncld.net/1.1/classes/Products
D/===AVOS Cloud===: LogUtil$avlog->d->43: {"results":[],"count":32}
D/===AVOS Cloud===: AVPushConnectionManager->initConnection->111: push connection is open

在 test2 中,使用以下代码,获得count = 221

query.whereContains("searchString", keyword.toLowerCase().split(" ")[1]);

Log

D/===AVOS Cloud===: LogUtil$avlog->d->43: curl -X GET -H "X-LC-Id: IDX4LztPPtMsBK7gim8UJo7s-gzGzoHsz" -H "X-LC-Key: YourAppKey" -H "" -G --data-urlencode 'count=1&limit=0&where={"searchString":{"$regex":".*cosmetics.*"}}' https://idx4lztp.api.lncld.net/1.1/classes/Products
D/===AVOS Cloud===: LogUtil$avlog->d->43: {"results":[],"count":221}
D/===AVOS Cloud===: AVPushConnectionManager->initConnection->111: push connection is open

在 test3 中,使用以下代码,获得count = 221

query.whereContains("searchString", keyword.toLowerCase().split(" ")[0]);
query.whereContains("searchString", keyword.toLowerCase().split(" ")[1]);

Log

D/===AVOS Cloud===: LogUtil$avlog->d->43: curl -X GET -H "X-LC-Id: IDX4LztPPtMsBK7gim8UJo7s-gzGzoHsz" -H "X-LC-Key: YourAppKey" -H "" -G --data-urlencode 'count=1&limit=0&where={"searchString":{"$regex":".*cosmetics.*"}}' https://idx4lztp.api.lncld.net/1.1/classes/Products
D/===AVOS Cloud===: LogUtil$avlog->d->43: {"results":[],"count":221}
D/===AVOS Cloud===: AVPushConnectionManager->initConnection->111: push connection is open

是不是whereContains在Java只会取最后一个条件?但我在iOS SDK并没有这个问题

你好,无论 iOS SDK 和 Android 的 Java SDK 上面的这个结果是相同的。

 query.whereContains("searchString", keyword.toLowerCase().split(" ")[0]);
 query.whereContains("searchString", keyword.toLowerCase().split(" ")[1]);

上面两行代码相当于只第二行有效,查询条件被覆盖了。

如果您这边的需求是多个查询条件满足一个即可,可以使用组合查询(OR 查询)。
文档参考: https://leancloud.cn/docs/leanstorage_guide-android.html#hash9883108813

1 人赞了这个帖子.