Elasticsearch-bool组合查询

# bool组合查询
# filter:过滤,不参与打分
# must:如果有多个条件,这些条件都必须满足  and与
# should:如果有多个条件,满足一个或多个即可 or或
# must_not:和must相反,必须都不满足条件才可以匹配到 !非


GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      }, 
      "filter": {
        "term": {
          "salary": 6666
        }
      
      }
    }
  }
}

# select * from job where salary=6666 or salary=7777
# 查询所有数据,筛选出工资等于6666或者7777的数据
GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      }, 
      "filter": {
        "terms": {
          "salary": [6666,7777]
        }
      
      }
    }
  }
}
# 查询salary等于6666或者title等于python、salary不等于7777、salary不等于8888

GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "salary": {
            "value": 6666
          }
        }},
        {"term": {
          "title": {
            "value": "python"
          }
        }}
      ],
      "must_not": [
        {"term": {
          "salary": {
            "value": 7777
          }
        }},
        {"term": {
          "salary": {
            "value": 8888
          }
        }}
      ]
    }
  }
}

# salary等于6666或者title等于python salary不等于9999 title不等于redis

GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "salary": {
            "value": 6666
          }
        }},
        {"term": {
          "title": {
            "value": "python"
          }
        }}
      ],
      "must_not": [
        {"term": {
          "salary": {
            "value": 9999
          }
        }},
        {
          "term": {
            "title": {
              "value": "redis"
            }
          }
        }
      ]
    }
  }
}
# 查询title为python或者(title为搜索并且salary等于6666)的数据
GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "title": {
            "value": "python"
          }
        }},
        {
          "bool": {
            "must": [
              {"term": {
                "title": {
                  "value": "搜索"
                }
              }},
              {
                "term": {
                  "salary": {
                    "value": 6666
                  }
                }
              }
            ]
          }
          
        }
      ]
    }
  }
}


# 添加空值测试数据
PUT nulldb/test/_bulk
{"index":{"_id":1}}
{"tags":["IT","python"]}
{"index":{"_id":2}}
{"tags":["java","python"]}
{"index":{"_id":3}}
{"tags":null}
{"index":{"_id":4}}
{"tags":["IT","php"]}
{"index":{"_id":5}}
{"tags":[null,"python"]}


# 处理null空值
# 过滤空值
# exists 处理值不为空或者值不为null的数据
GET nulldb/test/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}

# 筛选出tags值为空或者没有tags属性的数据
GET nulldb/test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  }
}












你可能感兴趣的:(开发)