我正在使用couchbase java-client版本2.5.4和jdk 1.8。我想在单个查询中使用AND和OR运算符进行查询。但我无法做到这一点。
用户:
firstname lastname age gender
Ani Mary 12 Female
John William 16 Male
John William 12 Male
John Sam 13 Male
我想使用带有conjuctions和dijunctions而不是N1qlQuery的searchQuery来查询如下所示
select *
from `users`
where
(firstname="john" and lastname="william" and age = "12") or
(firstname="ani" and age="12" and gender="female")
java代码
Map<String, String> userParams1 = new HashMap<String, String>();
userParams1.put(“firstname”, “john”)
userParams1.put(“lastname”, “william”)
userParams1.put(“age”, “12”)
Map<String, String> userParams2 = new HashMap<String, String>();
userParams2.put(“firstname”, “ani”)
userParams2.put(“gender”, “female”)
userParams2.put(“age”, “12”)
List<MatchPhraseQuery> queryforUserParams1 new ArrayList<MatchPhraseQuery>();
List<MatchPhraseQuery> queryforUserParams2 = new ArrayList<MatchPhraseQuery>();
for (Entry<String, String> userParam : userParams1.entrySet()) {
MatchPhraseQuery query = SearchQuery.matchPhrase(userParam.getValue()).field(userParam.getKey());
queryforUserParams1.add(query);
}
for (Entry<String, String> userParam : userParams2.entrySet()) {
String key = userParam.getKey();
String value = userParam.getValue();
MatchPhraseQuery query = SearchQuery.matchPhrase(value).field(key);
queryforUserParams2.add(query);
}
AbstractFtsQuery[] queries1 = queryforUserParams1.toArray(new AbstractFtsQuery[queryforUserParams1.size()]);
AbstractFtsQuery[] queries2 = queryforUserParams2.toArray(new AbstractFtsQuery[queryforUserParams2.size()]);
ConjunctionQuery query = SearchQuery.conjuncts(queries1).and(SearchQuery.disjuncts(queries2));
System.out.println("QUERY: " + query);
bucket = couchbaseCluster.openBucket(“users”);
SearchQueryResult result = bucket.query(new SearchQuery("users_fts", query));
System.out.println("RESULT: " + result);
日志:
QUERY: {&#34;合取项&#34;:[{&#34;字段&#34;:&#34;姓名&#34;&#34; match_phrase&#34;:&#34;约翰&#34;},{ &#34;字段&#34;:&#34;姓&#34;&#34; match_phrase&#34;:&#34;威廉&#34;},{&#34;字段&#34;:&#34 ;年龄&#34;&#34; match_phrase&#34;:&#34; 12&#34;},{&#34;析取项&#34;:[{&#34;字段&#34;:&#34;姓名&#34;&#34; match_phrase&#34;:&#34; ANI&#34;},{&#34;字段&#34;:&#34;两性&#34;&#34; match_phrase&#34 ;:&#34;女性&#34;},{&#34;字段&#34;:&#34;年龄&#34;&#34; match_phrase&#34;:&#34; 12&#34;}] }]}
2019年2月9日上午11:08:32 com.couchbase.client.core.node.CouchbaseNode signalConnected INFO:已连接到节点localhost / localhost 2009年2月, 2018 11:08:32 AM com.couchbase.client.core.config.DefaultConfigurationProvider $ 8 call
信息:已打开的存储桶用户结果:DefaultSearchQueryResult {error = null, status = DefaultSearchStatus {totalCount = 6,errorCount = 0, successCount = 6},hits = [],errors = [],facets = {}, metrics = DefaultSearchMetrics {took = 1006179,totalHits = 0,maxScore = 0.0}}
结果如下, 用户:
firstname lastname age gender
Ani Mary 12 Female
John William 12 Male
如何使用连接和析取来编写代码以在couchbase SearchQuery中获得此结果?
答案 0 :(得分:0)
我认为这与Java严格相关,我认为这是N1QL查询。根据您撰写的查询和您提供的示例数据,我相信您有两件事需要改变。
where firstname="john"
- 根据您的示例数据,值为&#34; John&#34;不是&#34;约翰&#34;。因此,您的查询无法匹配(它的区分大小写)。and age = "12"
- 我假设您的示例数据将年龄存储为整数,而不是字符串。 &#34; 12&#34;与12不一样。尝试改为运行此查询:
select *
from `users`
where
(firstname="John" and lastname="William" and age = 12) or
(firstname="Ani" and age=12 and gender="Female");