![](/img/trans.png)
[英]Hibernate Query to match mapped entity collection with at least one element in given collection,and must not match in another, ManyToMany relationship
[英]ElasticSearch Query : Query must match at least one of multiple values
我需要 ElasticSearch 查询的帮助,我需要将 SHOULD 和 MUST 结合起来,但我不知道该怎么做:
这是我想做的查询示例:
ap_code= ["ta", "All"] (OR condition)
AND
ra_code= ["ph", "All"] (OR condition)
AND
city = ["London"]
这是查询,但它从 ES 获取所有结果
"bool" : {
"must" : [
{
"match_phrase" : {
"city" : {
"query" : "London",
"slop" : 0,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"should" : [
{
"match_phrase" : {
"ra_code" : {
"query" : "ph",
"slop" : 0,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match_phrase" : {
"ra_code" : {
"query" : "All",
"slop" : 0,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match_phrase" : {
"ap_code" : {
"query" : "All",
"slop" : 0,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
},
{
"match_phrase" : {
"ap_code" : {
"query" : "ta",
"slop" : 0,
"zero_terms_query" : "NONE",
"boost" : 1.0
}
}
}
],
"adjust_pure_negative" : true,
"boost" : 1.0
}
}
我使用 Java API 创建查询(JDK 11)
请问有什么帮助吗? 提前致谢
您可以在如下所示的 must 子句中使用 should 查询。
{
"query": {
"bool": {
"must": [
{
"match_phrase": {
"city": {
"query": "London",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"ra_code": {
"query": "ph",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
},
{
"match_phrase": {
"ra_code": {
"query": "All",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
}
],
"minimum_should_match": 1
}
},
{
"bool": {
"should": [
{
"match_phrase": {
"ap_code": {
"query": "ta",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
},
{
"match_phrase": {
"ap_code": {
"query": "All",
"slop": 0,
"zero_terms_query": "NONE",
"boost": 1
}
}
}
],
"minimum_should_match": 1
}
}
]
}
}
}
对于 Java api,您可以尝试以下操作
BoolQueryBuilder first = QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery(field1, value1))
.should(QueryBuilders.matchQuery(field2, value2));
BoolQueryBuilder second= QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery(field1, value1))
.should(QueryBuilders.matchQuery(field2, value2));
BoolQueryBuilder filter = new BoolQueryBuilder()
.must(first)
.must(second);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.