布尔查询(三)

布尔查询时最常用的组合查询,根据子查询的规则,只有当文档满足所有子查询条件时,es 才会返回查询结果,支持的布尔查询有:

  • must(and):和,两个条件必须都满足才返回
  • should(or):或
  • must_not(not):非
  • filter

1. 准备数据

PUT f1/doc/1
{
  "name": "rose",
  "age": 18,
  "from": "美国",
  "tags": ["白", "漂亮", "高"]
}

PUT f1/doc/2
{
  "name": "lila",
  "age": 19,
  "from": "瑞士",
  "tags": ["白", "漂亮", "性感"]
}


PUT f1/doc/3
{
  "name": "john",
  "age": 20,
  "from": "美国",
  "tags": ["黑", "帅气", "高"]
}

2. must

查询所有来自美国的:

# 查询所有来自美国的
GET f1/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "美国"
          }
        }
      ]
    }
  }
}

must 是一个列表。可以接多个查询条件。

查询结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "rose",
          "age" : 18,
          "from" : "美国",
          "tags" : [
            "白",
            "漂亮",
            "高"
          ]
        }
      },
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "john",
          "age" : 20,
          "from" : "美国",
          "tags" : [
            "黑",
            "帅气",
            "高"
          ]
        }
      }
    ]
  }
}

查询所有来自美国的,年龄是 18 岁的

# 查询所有来自美国的,年龄是 18 岁的
GET f1/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "美国"
          }
        },
        
        {
          "match": {
            "age": 18
          }
        }
      ]
    }
  }
}

查询结果:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.5753641,
    "hits" : [
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 1.5753641,
        "_source" : {
          "name" : "rose",
          "age" : 18,
          "from" : "美国",
          "tags" : [
            "白",
            "漂亮",
            "高"
          ]
        }
      }
    ]
  }
}

3. should

查询来自美国或瑞士的:

# 查询来自美国或瑞士的
GET f1/doc/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "from": "美国"
          }
        },
        
        {
          "match": {
            "from": "瑞士"
          }
        }
      ]
    }
  }
}

查询结果:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "2",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "lila",
          "age" : 19,
          "from" : "瑞士",
          "tags" : [
            "白",
            "漂亮",
            "性感"
          ]
        }
      },
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "rose",
          "age" : 18,
          "from" : "美国",
          "tags" : [
            "白",
            "漂亮",
            "高"
          ]
        }
      },
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "3",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "john",
          "age" : 20,
          "from" : "美国",
          "tags" : [
            "黑",
            "帅气",
            "高"
          ]
        }
      }
    ]
  }
}

4. must_not

查询既不来自美国也不来自瑞士的:

# 查询既不来自美国也不来自瑞士的
GET f1/docs/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "from": "美国"
          }
        },
        
        {
          "match": {
            "from": "瑞士"
          }
        }
      ]
    }
  }
}

查询结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

5. filter

查询来自美国,年龄大于 16 小于 19:

# 查询来自美国,年龄大于 16 小于 19 
GET f1/doc/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "from": "美国"
          }
        }
      ],
      
      "filter": {
        "range": {
          "age": {
            "gt": 16,
            "lt": 19
          }
        }
      }
    }
  }
}
  • must:查询所有来自美国的
  • filter:条件过滤查询,范围用 range 表示,gt 表示大于,lt 表示小于

查询结果:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "f1",
        "_type" : "doc",
        "_id" : "1",
        "_score" : 0.5753642,
        "_source" : {
          "name" : "rose",
          "age" : 18,
          "from" : "美国",
          "tags" : [
            "白",
            "漂亮",
            "高"
          ]
        }
      }
    ]
  }
}

还可以直接使用 filter 来查询:

# 查询年龄小于等于 19 岁的
GET f1/doc/_search
{
  "query": {
    "bool": {
      "filter": {
        "range": {
          "age": {
            "lte": 19
          }
        }
      }
    }
  }
}

注意:filter 不要与 should 搭配使用,而应与 must 搭配使用;这是因为布尔查询优先经过 filter 过滤,而 should 是或关系,有可能出现意想不到的结果~

原文地址:https://www.cnblogs.com/midworld/p/13782864.html