博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Elasticsearch学习---Bool Query(布尔查询)
阅读量:2493 次
发布时间:2019-05-11

本文共 3428 字,大约阅读时间需要 11 分钟。

版本说明

本文基于Elasticsearch6.4.0版本

在这里插入图片描述

演示数据

mapping映射

{
"emp": {
"mappings": {
"base_info": {
"properties": {
"age": {
"type": "integer" }, "dept": {
"type": "long" }, "detail": {
"type": "text" }, "name": {
"type": "keyword" }, "salary": {
"type": "double" }, "sex": {
"type": "integer" } } } } }}

当前已有数据

{
"took": 11, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 6, "max_score": 1, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "10", "_score": 1, "_source": {
"name": "王者", "age": 26, "sez": 1, "salary": 6666, "detail": "渣渣辉" } }, {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 1, "_source": {
"name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": null } }, {
"_index": "emp", "_type": "base_info", "_id": "2", "_score": 1, "_source": {
"name": "李四", "age": 22, "sex": 0, "salary": 18888, "detail": "才华横溢、天赋异禀" } }, {
"_index": "emp", "_type": "base_info", "_id": "1", "_score": 1, "_source": {
"name": "张三", "age": 18, "sex": 1, "salary": 6000, "detail": "普通人" } }, {
"_index": "emp", "_type": "base_info", "_id": "3", "_score": 1, "_source": {
"name": "王五", "age": 23, "sex": 1, "salary": 10000, "detail": "全能王者" } }, {
"_index": "emp", "_type": "base_info", "_id": "11", "_score": 1, "_source": {
"name": "小王", "age": 26, "sez": 1, "salary": 1234, "detail": "隔壁老王" } } ] }}

Bool Query

可以用来组合多个条件实现复杂的查询

must:相当于并且的关系,条件必须满足

filter:与must相似,但不会计算得分,且会缓存文档查询结果
should:想等于或者的关系,满足其中一个条件即可
must_not:与must相反,不能满足任何一个。

查询name为“赵六”,且年龄不在10-20岁之间,或者年龄等于25或者salary等于111

GET /emp/base_info/_search{
"query": {
"bool": {
"must": {
"match": {
"name": "赵六" } }, "should": [ {
"match": {
"age": 25 } }, {
"term": {
"salary": 111 } } ], "must_not": {
"range": {
"age": {
"gte": 10, "lte": 20 } } } } }}

查询结果

{
"took": 2, "timed_out": false, "_shards": {
"total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": {
"total": 1, "max_score": 0.6931472, "hits": [ {
"_index": "emp", "_type": "base_info", "_id": "5", "_score": 0.6931472, "_source": {
"name": "赵六", "age": 24, "sex": 1, "salary": 8888, "detail": null } } ] }}

转载地址:http://ghlrb.baihongyu.com/

你可能感兴趣的文章
通向财务自由之路01_导读
查看>>
通向财务自由之路02_成功的决定因素:你
查看>>
中低频量化交易策略研发01_引言
查看>>
中低频量化交易策略研发06_推进的择时策略
查看>>
史丹·温斯坦称傲牛熊市的秘密
查看>>
期货市场技术分析01_理论基础
查看>>
期货市场技术分析02_趋势的基本概念
查看>>
期货市场技术分析03_主要反转形态
查看>>
期货市场技术分析04_持续形态
查看>>
期货市场技术分析05_交易量和持仓兴趣
查看>>
TB交易开拓者入门教程
查看>>
TB创建公式应用dll失败 请检查用户权限,终极解决方案
查看>>
python绘制k线图(蜡烛图)报错 No module named 'matplotlib.finance
查看>>
talib均线大全
查看>>
期货市场技术分析06_长期图表和商品指数
查看>>
期货市场技术分析07_摆动指数和相反意见理论
查看>>
满屏的指标?删了吧,手把手教你裸 K 交易!
查看>>
不吹不黑 | 聊聊为什么要用99%精度的数据回测
查看>>
对于模拟交易所引发的思考
查看>>
高频交易的几种策略
查看>>