python 使用 elasticsearch 常用方法(检索)
#记录es查询等方法
#清楚数据
curl -XDELETE http://xx.xx.xx.xx:9200/test6 #初始化数据
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/1' -d '{"name": "tom", "age":18, "info": "tom"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/2' -d '{"name": "jack", "age":29, "info": "jack"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/3' -d '{"name": "jetty", "age":18, "info": "jetty"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/4' -d '{"name": "daival", "age":19, "info": "daival"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/5' -d '{"name": "lilei", "age":18, "info": "lilei"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/6' -d '{"name": "lili", "age":29, "info": "lili"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/7' -d '{"name": "tom1", "age":30, "info": "tom1"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/8' -d '{"name": "tom2", "age":31, "info": "tom2"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/9' -d '{"name": "tom3", "age":32, "info": "tom3"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/10' -d '{"name": "tom4", "age":33, "info": "tom4"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/11' -d '{"name": "tom5", "age":34, "info": "tom5"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/12' -d '{"name": "tom6", "age":35, "info": "tom6"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/13' -d '{"name": "tom7", "age":36, "info": "tom7"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/14' -d '{"name": "tom8", "age":37, "info": "tom8"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/15' -d '{"name": "tom9", "age":38, "info": "tom9"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/16' -d '{"name": "john", "age":38, "info": "john"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/17' -d '{"name": "marry", "age":38, "info": "marry and john are friend"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/18' -d '{"name": "john", "age":32, "info": "john"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/19' -d '{"name": "tom is a little boy", "age":7, "info": "tom is a little boy"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/20' -d '{"name": "tom is a student", "age":12, "info": "tom is a student"}'
curl -H "Content-Type: application/json" -XPUT 'http://xx.xx.xx.xx:9200/test6/user/21' -d '{"name": "jack is a little boy", "age":22, "info": "jack"}'
from elasticsearch import Elasticsearch def foreach(doc):
doc = res['hits']['hits'] if len(doc):
for item in doc:
print(item['_source']) es = Elasticsearch(['xx.xx.xx.xx:9200']) #查询所有数据
#方法1
#res = es.search(index='test6', size=20)
#方法2
res = es.search(index='test6', size=20, body = {
"query": {
"match_all": {}
}
}) #foreach(res) #等于查询 term与terms, 查询 name='tom cat' 这个值不会分词必须完全包含
res = es.search(index='test6', size=20, body= {
"query": {
"term": {
"name": "tom cat"
}
}
})
#foreach(res) #等于查询 term与terms, 查询 name='tom' 或 name='lili'
res = es.search(index= 'test6', size= 20, body= {
"query": {
"terms": {
"name": ["tom","lili"]
}
}
})
#foreach(res) #包含查询,match与multi_match
# match: 匹配name包含"tom cat"关键字的数据, 会进行分词包含tom或者cat的
res = es.search(index='test6', size=20, body={
"query": {
"match": {
"name": "tom cat"
}
}
})
#foreach(res) #multi_match: 在name或info里匹配包含little的关键字的数据
res = es.search(index='test6', size=20, body={
"query": {
"multi_match": {
"query": "little",
"fields": ["name", "info"]
}
}
})
#foreach(res) #ids , 查询id 1, 2的数据 相当于mysql的 in
res = es.search(index='test6', size=20, body={
"query": {
"ids": {
"values": ["1", "2"]
}
}
})
#foreach(res) #复合查询bool , bool有3类查询关系,must(都满足),should(其中一个满足),must_not(都不满足)
#name包含"tom" and term包含 "18"
res = es.search(index='test6', size=20, body={
"query": {
"bool": {
"must": [
{
"term": {
"name": "tom",
}, },
{
"term": {
"age": 18,
}, },
]
}
}
})
#foreach(res) #name包含"tom" or term包含"19"
res = es.search(index='test6', size=20, body={
"query": {
"bool": {
"should": [
{
"term": {
"name": "tom",
}, },
{
"term": {
"age": 19,
}, },
]
}
}
}) #foreach(res) #切片式查询
res = es.search(index='test6', size=20, body={
"query": {
"bool": {
"should": [
{
"term": {
"name": "tom",
}, },
{
"term": {
"age": 19,
}, },
]
}
},
"from": 2, #从第二条数据开始
"size": 4, # 获取4条数据
})
#foreach(res) #范围查询
res = es.search(index='test6', size=20, body={
"query": {
"range": {
"age": {
"gte": 18, #>=18
"lte": 30 #<=30
}
}
}
})
#foreach(res) #前缀查询
res = es.search(index='test6', size=20, body={
"query": {
"prefix": {
"name": "tom"
}
}
})
#foreach(res) #通配符查询
res = es.search(index='test6', size=20, body={
"query": {
"wildcard": {
"name": "*i"
}
}
})
#foreach(res) #排序
res = es.search(index='test6', size=20, body={
"query": {
"wildcard": {
"name": "*i"
}
},
"sort": {
"age": {
"order": "desc" #降序
}
}
})
#foreach(res) # count, 执行查询并获取该查询的匹配数
c = es.count(index='test6')
print(c)
# 短语匹配 match_phrase (搜索is a little的短语,不进行切分)
res = es.search(index='test6', size=20, body={
"query": {
"match_phrase": {
"name": "is a little"
}
}
})
foreach(res)
python 使用 elasticsearch 常用方法(检索)的更多相关文章
- python 查询 elasticsearch 常用方法(Query DSL)
1. 建立连接 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:9200"]) 2. ...
- python 使用 elasticsearch 常用方法(聚合)
#记录聚合查询方法 from elasticsearch import Elasticsearch es = Elasticsearch(['xx.xx.xx.xx:9200']) #获取最小的年龄r ...
- python 使用 elasticsearch 常用方法(索引)
#记录管理索引等方法 from elasticsearch import Elasticsearch es = Elasticsearch(['xx.xx.xx.xx:9200']) #获取文档内容r ...
- Python 和 Elasticsearch 构建简易搜索
Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...
- 笔记13:Python 和 Elasticsearch 构建简易搜索
Python 和 Elasticsearch 构建简易搜索 1 ES基本介绍 概念介绍 Elasticsearch是一个基于Lucene库的搜索引擎.它提供了一个分布式.支持多租户的全文搜索引擎,它可 ...
- python中os常用方法
python中OS常用方法 Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问 ...
- Python 操作 ElasticSearch
Python 操作 ElasticSearch 学习了:https://www.cnblogs.com/shaosks/p/7592229.html 官网:https://elasticsearch- ...
- Python操作ElasticSearch
Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...
- Python Selenium Webdriver常用方法总结
Python Selenium Webdriver常用方法总结 常用方法函数 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() 最大化窗口: m ...
随机推荐
- Linux不管上一条命令成功还是失败都执行下一个命令的方法
转载请注明来源https://www.cnblogs.com/sogeisetsu/p/11407830.html Linux不管上一条命令成功还是失败都执行下一个命令的方法 Linux不管上一条命令 ...
- 项目Beta冲刺(团队)——05.29(7/7)
项目Beta冲刺(团队)--05.29(7/7) 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:记录Beta敏捷冲刺第7 ...
- DOM4j XML 工具类
之前项目有跟客户系统对接一个webservice系统,该接口有传参和返回都是xml,所以找时间百度研究了一下dom4j,dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件 ...
- 怎么避免写bug?
1:关注可能的业务逻辑异常:业务逻辑异常 可以通过各种输入输出 和 预期进行比较 即可很快发现. 2:关注可能的运行逻辑异常: 如 copy对象: 对象引用等 这类异常只看代码逻辑时很难发现的:代码写 ...
- PHP获取一年有多少周和每周开始和结束日期
/*PHP获取当前日期是第几周和本周开始日期和本周结束日期*/ //$now = '2018-11-13';周二 public function getNowTimeInfo($now) { $str ...
- [Algorithm] 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- yolov2
在这篇文章中,作者首先在YOLOv1的基础上提出了改进的YOLOv2,然后提出了一种检测与分类联合训练方法,使用这种联合训练方法在COCO检测数据集(用于检测)和ImageNet分类数据集(用于分类) ...
- Traefik 2.0 tcp 路由试用
对于tcp 的路由是基于sni (需要tls)但是可以通过统配(*) 解决不试用tls的,当然也可以让Traefik 自动生成tls 证书 以下是测试http 以及mysql 的tcp 路由配置(de ...
- [RN] React Native 解决 使用 阿里巴巴 矢量图标库 iconfont 图标不垂直居中问题
React Native 解决 使用 阿里巴巴 矢量图标库 iconfont 图标不垂直居中问题 解决方法: 添加 size, line-height ,值为和 height 一样的高度. 例如: ...
- webstorm永久破解
准备 1. webstorm下载 本次使用的是2017.3.3版本,如果找不到那么在此附上:webstorm各版本下载地址. 前段时间有朋友反馈破解不成功,今天本人亲测2017.3.3仍可以破解成功, ...