美文网首页
00101-搜索语法之 bool 组合查询

00101-搜索语法之 bool 组合查询

作者: AndyFfeng | 来源:发表于2019-08-23 10:45 被阅读0次

数据源

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "megacorp",
                "_type": "employee",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "first_name": "John",
                    "last_name": "Smith",
                    "age": 25,
                    "about": "I love to go rock climbing",
                    "interests": [
                        "sports",
                        "music"
                    ]
                }
            },
            {
                "_index": "megacorp",
                "_type": "employee",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "first_name": "Jane",
                    "last_name": "Smith",
                    "age": 32,
                    "about": "I like to collect rock albums",
                    "interests": [
                        "music"
                    ]
                }
            },
            {
                "_index": "megacorp",
                "_type": "employee",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "first_name": "Douglas",
                    "last_name": "Fir",
                    "age": 35,
                    "about": "I like to build cabinets",
                    "interests": [
                        "forestry"
                    ]
                }
            }
        ]
    }
}

bool过滤查询

bool查询可以实现组合过滤查询

格式:

{"bool" : {"must":[],"should":[],"must_not":[] } }

must:必须满足的条件 (相当于and)

should:可以满足也可以不满足的条件 (相当于or)

must_not:不需要满足的条件 (相当于not)

范围过滤

gt:>
lt:<
gte:>=
lte:<=

查询条件满足年龄大于等于30,名字包含smith

{
    "query" : {
        "bool" : {
           "must": [
                {"range" : {
                    "age" : { "gt" : 30 }
                }},
                {"match" : {
                    "last_name" : "smith"
                }}
           ]
        }
    }
}

结果:

{
    "took": 111,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.4700036,
        "hits": [
            {
                "_index": "megacorp",
                "_type": "employee",
                "_id": "2",
                "_score": 1.4700036,
                "_source": {
                    "first_name": "Jane",
                    "last_name": "Smith",
                    "age": 32,
                    "about": "I like to collect rock albums",
                    "interests": [
                        "music"
                    ]
                }
            }
        ]
    }
}

相关文章

  • 00101-搜索语法之 bool 组合查询

    数据源 bool过滤查询 bool查询可以实现组合过滤查询 格式: {"bool" : {"must":[],"s...

  • ES 3.高级查询

    3.3.1.布尔组合(bool) bool把各种其他查询通过must(与)、must_not(非)、should(...

  • Elasticsearch查询语法

    查询语法 match all query define query number term query bool ...

  • Mysql语法之组合查询

    本章讲述如何利用UNION操作符将多条SELECT语句组合成一个结果集。 一、组合查询 多数SQL查询都只包含从一...

  • ES 全文搜索

    ES 全文搜索 全文搜索 使用了match查询的多词查询只是简单地将生成的term查询包含在了一个bool查询中。...

  • es组合查询

    组合查询分类: 1、bool:布尔组合2、boost:加权3、constant:固定分值4、Dis_max:单字符...

  • 47、初识搜索引擎_多搜索条件组合查询

    1、多条件组合查询示例 bool下有属性:must、must_not、should、filter每个子查询都会计算...

  • ElasticSearch 复合查询 Compound quer

    布尔查询 Boolean query 一种查询,用于匹配与其他查询的布尔组合相匹配的文档。bool查询映射到Luc...

  • 00101-普通搜索

    通过文档Id搜索Get 结果 简单搜索 Get

  • Elasticsearch bool query小结

    背景 最近有一个线上的es查询问题,最后确定在使用bool query多条件组合查询时出现should子句查询失效...

网友评论

      本文标题:00101-搜索语法之 bool 组合查询

      本文链接:https://www.haomeiwen.com/subject/ozxzsctx.html