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 相关文章推荐
requests和lxml实现爬虫的方法
Jun 11 Python
python利用正则表达式搜索单词示例代码
Sep 24 Python
用Python实现大文本文件切割的方法
Jan 12 Python
Python3.5内置模块之random模块用法实例分析
Apr 26 Python
Python Pandas数据结构简单介绍
Jul 03 Python
django实现web接口 python3模拟Post请求方式
Nov 19 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
Dec 10 Python
django实现模板中的字符串文字和自动转义
Mar 31 Python
使用python实现CGI环境搭建过程解析
Apr 28 Python
python获取响应某个字段值的3种实现方法
Apr 30 Python
python 使用建议与技巧分享(四)
Aug 18 Python
python基础之类方法和静态方法
Oct 24 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
从网上搜到的phpwind 0day的代码
2006/12/07 PHP
pdo中使用参数化查询sql
2011/08/11 PHP
curl不使用文件存取cookie php使用curl获取cookie示例
2014/01/26 PHP
PHP header()函数常用方法总结
2014/04/11 PHP
orm获取关联表里的属性值
2016/04/17 PHP
PHP在innodb引擎下快速代建全文搜索功能简明教程【基于xunsearch】
2016/10/14 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
在phpstudy集成环境下的nginx服务器下配置url重写
2019/12/02 PHP
JavaScript字符串对象toUpperCase方法入门实例(用于把字母转换为大写)
2014/10/17 Javascript
Node.js异步I/O学习笔记
2014/11/04 Javascript
javascript实现label标签跳出循环操作
2016/03/06 Javascript
微信小程序中使元素占满整个屏幕高度实现方法
2016/12/14 Javascript
jQuery实现别踩白块儿网页版小游戏
2017/01/18 Javascript
JavaScript常用正则函数用法示例
2017/01/23 Javascript
数组Array的排序sort方法
2017/02/17 Javascript
vue如何实现observer和watcher源码解析
2017/03/09 Javascript
Javascript实现页面滚动时导航智能定位
2017/05/06 Javascript
AngularJS实现动态添加Option的方法
2017/05/17 Javascript
深入理解Vue 组件之间传值
2018/08/16 Javascript
详解如何在JS代码中消灭for循环
2019/12/11 Javascript
Vant picker 多级联动操作
2020/11/02 Javascript
用Python的Django框架完成视频处理任务的教程
2015/04/02 Python
Python3实现的Mysql数据库操作封装类
2018/06/06 Python
利用Python求阴影部分的面积实例代码
2018/12/05 Python
python开头的coding设置方法
2019/08/08 Python
Python编程快速上手——强口令检测算法案例分析
2020/02/29 Python
Python实现ElGamal加密算法的示例代码
2020/06/19 Python
使用scrapy ImagesPipeline爬取图片资源的示例代码
2020/09/28 Python
学生会招新策划书
2014/02/14 职场文书
小学毕业寄语大全
2014/04/03 职场文书
2014年学生资助工作总结
2014/12/18 职场文书
文明礼仪主题班会
2015/08/13 职场文书
心理健康教育主题班会
2015/08/13 职场文书
Python 文本滚动播放器的实现代码
2021/04/25 Python
python geopandas读取、创建shapefile文件的方法
2021/06/29 Python
MySQL的prepare使用以及遇到的bug
2022/05/11 MySQL