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 ...
随机推荐
- 1.探索性数据分析(EDA,Exploratory Data Analysis)
一.数据探索 1.数据读取 遍历文件夹,读取文件夹下各个文件的名字:os.listdir() 方法:用于返回指定的文件夹包含的文件或文件夹的名字的列表.这个列表以字母顺序. 它不包括 '.' 和'.. ...
- Word 页码设置教程:如何删除封面和目录的目录?
我们常写的报告大都由封面.目录.正文和附录组成,但是页码通常是从正文开始的,所以下面介绍如何从指定页面开始设置页码. 在介绍之前需要了解一下分隔符的作用.分隔符大体分成分页符和分节符. 分页符细分的几 ...
- 关于PID控制的一点资料搜集
CMU做的控制教程 <动态系统的反馈控制> MATLAB&Simulink的PID控制(官方)
- 项目Beta冲刺 用户试用报告
课程: 软件工程1916|W(福州大学) 作业要求: 项目Beta冲刺 团队名称: 火鸡堂 作业目标: 火鸡堂 队员学号 队员姓名 博客地址 备注 221600111 彼术向 http://www.c ...
- Codeforces Round #552 (Div. 3)-D-Walking Robot-(贪心)
http://codeforces.com/contest/1154/problem/D 解题: 1.无光的时候优先使用太阳能电池. 2.有光的时候 (1)太阳能电池没满电,让它充,使用普通电池 (2 ...
- 18-Flutter移动电商实战-首页_火爆专区商品接口制作
1.获取接口的方法 在service/service_method.dart里制作方法.我们先不接收参数,先把接口调通. Future getHomePageBeloConten() async{ ...
- rhce备战笔记
1)配置selinuxvim /etc/slinux/config SELINUX=enforcingsetenforce 1getenforce两台都做 2)配置SSHvim /etc/ssh ...
- MongoDB索引存储BTree与LSM树(转载)
1.为什么 MongoDB 使用B-树,而不是B+树 MongoDB 是一种 nosql,也存储在磁盘上,被设计用在数据模型简单,性能要求高的场合.性能要求高,我们看B-树与B+树的区别: B+树内节 ...
- Traefik 2.0 发布了
Traefik 2.0 发布了,包含了很多不错的行特性 tcp 路由(同时也支持sni 路由) 参考配置 tcp: routers: to-db-1: entrypoints: - web-secur ...
- 16-网页,网站,微信公众号基础入门(网页版MQTT,页面控件位置调整入门)
https://www.cnblogs.com/yangfengwu/p/11200767.html 说一下,只要你java学的很好,那么几乎所有的语言都不在话下了 来看一下样式设置 运行 在左上角感 ...