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 ...
随机推荐
- ztree的添加、修改、删除及前后台交互
一.引入资源下载并引入ztree的相关js,css和img等.http://www.treejs.cn/v3/api.php ztree的核心代码jquery.ztree.core.jsztree关于 ...
- java获取一个时间段内的时间天数
package com.hzcominfo.hik.hikbigscreen.core; import java.text.SimpleDateFormat; import java.util.Arr ...
- 2019牛客暑期多校训练营(第六场)C:Palindrome Mouse(回文树+树剖)
题意:给定字符串Str,求出回文串集合为S,问S中的(a,b)满足a是b的子串的对数. 思路:开始和题解的思路差不多,维护当前后缀的每个串的最后出现位置,但是不知道怎么套“最小回文分割”,所以想到了树 ...
- Linux正则表达式与通配符
在linux中,有通配符和正则表达式,这是两个不同的概念通配符:它是由shell解析,并且一般用于匹配文件名.如:ls正则表达式:是一个字符匹配标准,可以匹配文本中的内容一些命令工具按此标准实现字符匹 ...
- webapi HttpGet标签
该标签可以指定路由如HttpGet["Test"],以前用的很顺,后来加了Area后,按照area/controller/Test的路径去访问报404,原因是HTTPGet指定路由 ...
- k-mean
import numpy as np from k_initialize_cluster import k_init np.random.seed() class YOLO_Kmeans: def _ ...
- isopod dsl 框架管理kubernetes 配置
isopod 是一个包含了丰富能力的dsl 框架我们可以不用编写yaml 文件来进行k8s 管理 说明 语法类似python,目前移植内置了一些不错的功能kube 方法 vault 集成,helm 集 ...
- Docker 更改容器映射端口
1.编辑容器的配置文件进行更改端口: docker run 运行启动时 -p 可以指定容器启动映射端口 ( ) 可以编辑配置文件 进行修改:(需要重启docker 服务 不止是是容器 才能生效.只能重 ...
- 动态规划-多维DP
1.最大正方形 我的瞎猜分析: 我的瞎猜算法: #include <stdio.h> #include <memory.h> #include <math.h> # ...
- CGLIB和Java动态代理的区别(笔记)
java常用知识点: 1.Java动态代理只能够对接口进行代理,不能对普通的类进行代理(因为所有生成的代理类的父类为Proxy,Java类继承机制不允许多重继承):CGLIB能够代理普通类:2.Jav ...