zoukankan      html  css  js  c++  java
  • Elasticsearch布尔查询——bool

    布尔查询允许我们利用布尔逻辑将较小的查询组合成较大的查询。

    1、查询返回包含“mill”和“lane”的所有的账户

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
            {
              "query": {
                "bool": {
                  "must": [
                    { "match": { "address": "mill" } },
                    { "match": { "address": "lane" } }
                  ]
                }
              }
            }'

      在上面的例子中,bool must语句指明了,对于一个文档,所有的查询都必须为真,这个文档才能够匹配成功。

    2、查询返回地址中包含“mill”或者“lane”的所有的账户

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
            {
              "query": {
                "bool": {
                  "should": [
                    { "match": { "address": "mill" } },
                    { "match": { "address": "lane" } }
                  ]
                }
              }
            }'

      在上面的例子中,bool should语句指明,对于一个文档,查询列表中,只要有一个查询匹配,那么这个文档就被看成是匹配的。

    3、查询返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
            {
              "query": {
                "bool": {
                  "must_not": [
                    { "match": { "address": "mill" } },
                    { "match": { "address": "lane" } }
                  ]
                }
              }
            }'

      在上面的例子中, bool must_not语句指明,对于一个文档,查询列表中的的所有查询都必须都不为真,这个文档才被认为是匹配的。

      我们可以在一个bool查询里一起使用must、should、must_not。此外,我们可以将bool查询放到这样的bool语句中来模拟复杂的、多等级的布尔逻辑。

      下面这个例子返回40岁以上并且不生活在ID(daho)的人的账户:

    curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
            {
              "query": {
                "bool": {
                  "must": [
                    { "match": { "age": "40" } }
                  ],
                  "must_not": [
                    { "match": { "state": "ID" } }
                  ]
                }
              }
            }'
  • 相关阅读:
    Embeded Linux 之 PHY
    Embeded Linux之网络子系统
    语言之内联函数
    Embeded Linux之海思UART
    Windows 之samba问题
    Embeded linux 之 CIFS 文件操作源码分析
    zookeeper 都有哪些使用场景?
    如何保证分布式系统中接口调用的顺序性?
    分布式系统中接口的幂等性该如何保证?比如不能重复扣款?
    Redis 的并发竞争问题是什么?如何解决这个问题?了解 redis 事务的 CAS 方案吗?
  • 原文地址:https://www.cnblogs.com/hcy-fly/p/7986893.html
Copyright © 2011-2022 走看看