使用的是Elasticsearch 7.9.x,Elasticsearch rest high level client.
要构建的查询条件长这样:
{
"bool" : {
"filter" : [
{
"bool" : {
"filter" : [
{
"bool" : {
"should" : [
{
"bool" : {
"should" : [
{
"term" : {
"security.provider" : {
"value" : [
"cob"
]
}
}
}
]
}
},
{
"bool" : {
"must" : [
{
"bool" : {
"should" : [
{
"terms_set" : {
"security.type" : {
"terms" : [
"AMC_KYC:D10",
"AMC_KYC:D126"
],
"minimum_should_match_script" : {
"source" : "2",
"lang" : "painless"
}
}
}
},
{
"bool" : {
"must_not" : [
{
"exists" : {
"field" : "security.type"
}
}
]
}
},
{
"terms_set" : {
"security.entitlement" : {
"terms" : [
"OASYS_MSTR_AGMT_STATUS:EXECUTED",
"OASYS_LEGAL_ENTITY:CBNA"
],
"minimum_should_match_script" : {
"source" : "2",
"lang" : "painless"
}
}
}
}
]
}
},
{
"bool" : {
"should" : [
{
"bool" : {
"must_not" : [
{
"exists" : {
"field" : "security.entitlement"
}
}
]
}
}
]
}
},
{
"bool" : {
"should" : [
{
"terms_set" : {
"security.country" : {
"terms" : [
"AMC_KYC|CN"
],
"minimum_should_match_script" : {
"source" : "1",
"lang" : "painless"
}
}
}
}
]
}
}
]
}
}
]
}
}
]
}
}
]
}
}
下面是使用Rest high level client 构建的java代码:
private static void buildUserEntitlementQueryBuilder(BoolQueryBuilder boolQuery,
DMCUserEntitlement userEntitlement) {
BoolQueryBuilder userBoolQuery = new BoolQueryBuilder();
BoolQueryBuilder userCombainBoolQuery = new BoolQueryBuilder();
BoolQueryBuilder providerBoolQuery = new BoolQueryBuilder();
providerBoolQuery.should(QueryBuilders.termQuery("security.provider",userEntitlement.getProvider()));
BoolQueryBuilder tripleBoolQuery = new BoolQueryBuilder();
//Build the first must condition typeBoolQuery
BoolQueryBuilder typeBoolQuery = new BoolQueryBuilder();
QueryBuilder typeTermSetQuery = null;
if(userEntitlement.getType() != null) {
String minumLen = userEntitlement.getType().size() + "";
//terms_set
typeTermSetQuery = (new TermsSetQueryBuilder("security.type", userEntitlement.getType())).setMinimumShouldMatchScript(new Script(minumLen));
}
if(typeTermSetQuery != null) {
typeBoolQuery.should(typeTermSetQuery);
}
BoolQueryBuilder typeInnerBoolQuery = new BoolQueryBuilder();
typeInnerBoolQuery.mustNot(new ExistsQueryBuilder("security.type"));
typeBoolQuery.should(typeInnerBoolQuery);
//Cobaine the first must condition typeBoolQuery
tripleBoolQuery.must(typeBoolQuery);
//Build the second must condition typeBoolQuery
BoolQueryBuilder entitlementBoolQuery = new BoolQueryBuilder();
QueryBuilder entitlementTermSetQuery = null;
if(userEntitlement.getEntitlement() != null) {
String minumLen = userEntitlement.getEntitlement().size() + "";
//terms_set
entitlementTermSetQuery = (new TermsSetQueryBuilder("security.entitlement", userEntitlement.getEntitlement())).setMinimumShouldMatchScript(new Script(minumLen));
}
if(entitlementTermSetQuery != null) {
typeBoolQuery.should(entitlementTermSetQuery);
}
BoolQueryBuilder entitlementInnerBoolQuery = new BoolQueryBuilder();
entitlementInnerBoolQuery.mustNot(new ExistsQueryBuilder("security.entitlement"));
entitlementBoolQuery.should(entitlementInnerBoolQuery);
//Cobaine the second must condition entitlementBoolQuery
tripleBoolQuery.must(entitlementBoolQuery);
//Build the third must condition typeBoolQuery
BoolQueryBuilder countryBoolQuery = new BoolQueryBuilder();
QueryBuilder countryTermSetQuery = null;
if(userEntitlement.getCountry() != null) {
String minumLen = userEntitlement.getCountry().size() + "";
//terms_set
countryTermSetQuery = (new TermsSetQueryBuilder("security.country", userEntitlement.getCountry())).setMinimumShouldMatchScript(new Script(minumLen));
}
if(countryTermSetQuery != null) {
countryBoolQuery.should(countryTermSetQuery);
}
BoolQueryBuilder countryInnerBoolQuery = new BoolQueryBuilder();
countryInnerBoolQuery.mustNot(new ExistsQueryBuilder("security.country"));
countryInnerBoolQuery.should(countryInnerBoolQuery);
//Cobaine the third must condition countryBoolQuery
tripleBoolQuery.must(countryBoolQuery);
userCombainBoolQuery.should(providerBoolQuery).should(tripleBoolQuery);
userBoolQuery.filter(userCombainBoolQuery);
boolQuery.filter(userBoolQuery);
}
上面这个构建条件基本上能覆盖大部分查询条件的构建,能理解,其他都是小case.