Python-ElasticSearch搜索查询的讲解


Posted in Python onFebruary 25, 2019

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有最先进,高性能和全功能搜索引擎功能的库。但是 Lucene 仅仅只是一个库。为了利用它,你需要编写 Java 程序,并在你的 java 程序里面直接集成 Lucene 包。 更坏的情况是,你需要对信息检索有一定程度的理解才能明白 Lucene 是怎么工作的。Lucene 是 很 复杂的。

在上一篇文章中介绍了ElasticSearch的简单使用,接下来记录一下ElasticSearch的查询:

查询所有数据

# 搜索所有数据
es.search(index="my_index",doc_type="test_type")
# 或者
body = {
  "query":{
    "match_all":{}
  }
}
es.search(index="my_index",doc_type="test_type",body=body)

term与terms

# term
body = {
  "query":{
    "term":{
      "name":"python"
    }
  }
}
# 查询name="python"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)
# terms
body = {
  "query":{
    "terms":{
      "name":[
        "python","android"
      ]
    }
  }
}
# 搜索出name="python"或name="android"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

match与multi_match

# match:匹配name包含python关键字的数据
body = {
  "query":{
    "match":{
      "name":"python"
    }
  }
}
# 查询name包含python关键字的数据
es.search(index="my_index",doc_type="test_type",body=body)
# multi_match:在name和addr里匹配包含深圳关键字的数据
body = {
  "query":{
    "multi_match":{
      "query":"深圳",
      "fields":["name","addr"]
    }
  }
}
# 查询name和addr包含"深圳"关键字的数据
es.search(index="my_index",doc_type="test_type",body=body)

ids

body = {
  "query":{
    "ids":{
      "type":"test_type",
      "values":[
        "1","2"
      ]
    }
  }
}
# 搜索出id为1或2d的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

复合查询bool

bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)

body = {
  "query":{
    "bool":{
      "must":[
        {
          "term":{
            "name":"python"
          }
        },
        {
          "term":{
            "age":18
          }
        }
      ]
    }
  }
}
# 获取name="python"并且age=18的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

切片式查询

body = {
  "query":{
    "match_all":{}
  }
  "from":2  # 从第二条数据开始
  "size":4  # 获取4条数据
}
# 从第2条数据开始,获取4条数据
es.search(index="my_index",doc_type="test_type",body=body)

范围查询

body = {
  "query":{
    "range":{
      "age":{
        "gte":18,    # >=18
        "lte":30    # <=30
      }
    }
  }
}
# 查询18<=age<=30的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

前缀查询

body = {
  "query":{
    "prefix":{
      "name":"p"
    }
  }
}
# 查询前缀为"赵"的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

通配符查询

body = {
  "query":{
    "wildcard":{
      "name":"*id"
    }
  }
}
# 查询name以id为后缀的所有数据
es.search(index="my_index",doc_type="test_type",body=body)

排序

body = {
  "query":{
    "match_all":{}
  }
  "sort":{
    "age":{         # 根据age字段升序排序
      "order":"asc"    # asc升序,desc降序
    }
  }
}

filter_path

响应过滤

# 只需要获取_id数据,多个条件用逗号隔开
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"])
# 获取所有数据
es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])

count

执行查询并获取该查询的匹配数

# 获取数据量
es.count(index="my_index",doc_type="test_type")

度量类聚合

  • 获取最小值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "min_age":{         # 最小值的key
      "min":{         # 最小
        "field":"age"    # 查询"age"的最小值
      }
    }
  }
}
# 搜索所有数据,并获取age最小的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取最大值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "max_age":{         # 最大值的key
      "max":{         # 最大
        "field":"age"    # 查询"age"的最大值
      }
    }
  }
}
# 搜索所有数据,并获取age最大的值
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取和
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "sum_age":{         # 和的key
      "sum":{         # 和
        "field":"age"    # 获取所有age的和
      }
    }
  }
}
# 搜索所有数据,并获取所有age的和
es.search(index="my_index",doc_type="test_type",body=body)
  • 获取平均值
body = {
  "query":{
    "match_all":{}
  },
  "aggs":{            # 聚合查询
    "avg_age":{         # 平均值的key
      "sum":{         # 平均值
        "field":"age"    # 获取所有age的平均值
      }
    }
  }
}
# 搜索所有数据,获取所有age的平均值
es.search(index="my_index",doc_type="test_type",body=body)

更多的搜索用法:

https://elasticsearch-py.readthedocs.io/en/master/api.html

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
Django的session中对于用户验证的支持
Jul 23 Python
Python的GUI框架PySide的安装配置教程
Feb 16 Python
python发送邮件实例分享
Jul 28 Python
Pandas中把dataframe转成array的方法
Apr 13 Python
Python中pillow知识点学习
Apr 30 Python
matplotlib调整子图间距,调整整体空白的方法
Aug 03 Python
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
Feb 28 Python
Django ORM 自定义 char 类型字段解析
Aug 09 Python
python图形开发GUI库pyqt5的详细使用方法及各控件的属性与方法
Feb 14 Python
python with语句的原理与用法详解
Mar 30 Python
使用Tensorflow-GPU禁用GPU设置(CPU与GPU速度对比)
Jun 30 Python
Python pyecharts绘制条形图详解
Apr 02 Python
Python2 Selenium元素定位的实现(8种)
Feb 25 #Python
selenium python 实现基本自动化测试的示例代码
Feb 25 #Python
详解Ubuntu16.04安装Python3.7及其pip3并切换为默认版本
Feb 25 #Python
Python3.5实现的罗马数字转换成整数功能示例
Feb 25 #Python
Python爬虫beautifulsoup4常用的解析方法总结
Feb 25 #Python
python3实现指定目录下文件sha256及文件大小统计
Feb 25 #Python
Python常用爬虫代码总结方便查询
Feb 25 #Python
You might like
一个简洁的多级别论坛
2006/10/09 PHP
提高PHP编程效率 引入缓存机制提升性能
2010/02/15 PHP
PHP的几个常用数字判断函数代码
2012/04/24 PHP
document 和 document.all 分别什么时候用
2006/06/22 Javascript
EasySlider 基于jQuery功能强大简单易用的滑动门插件
2010/06/11 Javascript
简单谈谈node.js 版本控制 nvm和 n
2015/10/15 Javascript
Javascript for in的缺陷总结
2017/02/03 Javascript
js正则表达式验证表单【完整版】
2017/03/06 Javascript
关于使用js算总价的问题
2017/06/23 Javascript
Vue2.0 vue-source jsonp 跨域请求
2017/08/04 Javascript
微信小程序使用slider设置数据值及switch开关组件功能【附源码下载】
2017/12/09 Javascript
详解Vue项目部署遇到的问题及解决方案
2019/01/11 Javascript
mpvue开发音频类小程序踩坑和建议详解
2019/03/12 Javascript
使用taro开发微信小程序遇到的坑总结
2019/04/08 Javascript
解决layui的radio属性或别的属性没显示出来的问题
2019/09/26 Javascript
vue 使用插槽分发内容操作示例【单个插槽、具名插槽、作用域插槽】
2020/03/06 Javascript
[01:12:40]DOTA2-DPC中国联赛 正赛 DLG vs XG BO3 第三场 1月25日
2021/03/11 DOTA
PHP魔术方法__ISSET、__UNSET使用实例
2014/11/25 Python
python计算N天之后日期的方法
2015/03/31 Python
qpython3 读取安卓lastpass Cookies
2016/06/19 Python
Python 'takes exactly 1 argument (2 given)' Python error
2016/12/13 Python
python3.x实现发送邮件功能
2018/05/22 Python
PyQt5重写QComboBox的鼠标点击事件方法
2019/06/25 Python
使用Python串口实时显示数据并绘图的例子
2019/12/26 Python
美国转售二手商品的电子商务平台:BLINQ
2018/12/13 全球购物
优秀食品类广告词
2014/03/19 职场文书
信用卡工作证明模板
2014/09/14 职场文书
2014年转正工作总结
2014/11/08 职场文书
办公用房租赁协议书
2014/11/29 职场文书
服务员岗位职责
2015/02/03 职场文书
2015年转正工作总结范文
2015/04/02 职场文书
2015年惩防体系建设工作总结
2015/05/22 职场文书
HTML中table表格拆分合并(colspan、rowspan)
2021/04/07 HTML / CSS
解决python存数据库速度太慢的问题
2021/04/23 Python
详解Mysql和Oracle之间的误区
2021/05/18 MySQL
使用Apache Camel表达REST服务的方法
2022/06/10 Servers