有一个非固定的数组查询条件类似于:
条件1 且|或 条件2 且|或 条件3 且或 条件是
写了个函数 转换成sql语句类似下面的逻辑:
条件1 且|或 条件2 且|或 条件3 且或 条件是
写了个函数 转换成sql语句类似下面的逻辑:
$arr = [
'age|1,2,3','and','sex|1','or','scholar|1,4,5','or','company|7,8,9','and','part|12'
];
sex in (1,2,3) and age = 1
sql: select * from test where (sex in(1,2,3) and age =1)
sex in (1,2,3) and age = 1 or scholar in(1,4,5)
sql: select * from test where ((sex in(1,2,3) and age =1) or scholar in(1,4,5))
sex in (1,2,3) and age = 1 or scholar in(1,4,5) or company in(7,8,9)
sql: select * from test where (((sex in(1,2,3) and age =1) or scholar in(1,4,5)) or company in(7,8,9))
最终:
sex in (1,2,3) and age = 1 or scholar in(1,4,5) or company in(7,8,9) and part = 12
sql: select * from test where ((((sex in(1,2,3) and age =1) or scholar in(1,4,5)) or company in(7,8,9)) and part=12)
每个组合条件都套一层括号,这个逻辑如果用es应该怎么组合呢,求大佬指点 已经晕了
1 个回复
heli - 90后IT男
赞同来自:
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"terms": {
"sex": [
"1",
"2",
"3"
]
}
},
{
"term": {
"age": {
"value": "1"
}
}
}
]
}
},
{
"terms": {
"scholar": [
"1",
"4",
"5"
]
}
}
]
}
},
{
"terms": {
"company": [
"7",
"8",
"9"
]
}
}
],
"minimum_should_match": 1
}
},
{
"term": {
"part": {
"value": "12"
}
}
}
]
}
}
}
注意父子和兄弟关系,套用bool查询就行