使用 shuf 来打乱一个文件中的行或是选择文件中一个随机的行。

bool嵌套组合查询条件

Elasticsearch | 作者 Xiaoming | 发布于2019年12月09日 | 阅读数:3467

有一个非固定的数组查询条件类似于:
条件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应该怎么组合呢,求大佬指点 已经晕了
 
已邀请:

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查询就行

要回复问题请先登录注册