1.创建索引(database)
es.indices.create(index="aaa")
2.插入数据
body = {
"name":"bb",
"age":30,
"city":"dd"
}
es.index(index='aaa',doc_type='default',id=2,body=body)
- 根据id查询
es.get(index='aaa',doc_type="default",id=2)
3.查询idnex下所有数据 match_all
query = {
"query": {
"match_all": {}
}
}
r = es.search(index='aaa', body=query)
print(r['hits']['hits'][0]['_id'])
{'took': 6, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0},
'hits': {'total': 2, 'max_score': 1.0, 'hits': [{'_index': 'aaa', '_type': 'default', '_id': '2', '_score': 1.0,
'_source': {'name': 'bb', 'age': 30, 'city': 'dd'}},
{'_index': 'aaa', '_type': 'default', '_id': '1', '_score': 1.0,
'_source': {'name': 'aa', 'age': 20, 'city': 'dd'}}]}}
4.term 精确匹配值 数字、日期、布尔
query = {
"query":{
"term":{
"age":20
}
}
}
res = es.search(index='aaa', body=query)
print(res)
{'took': 3, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'aaa', '_type': 'default', '_id': '1', '_score': 1.0, '_source': {'name': 'aa', 'age': 20, 'city': 'dd'}}]}}
- 5.terms 多条匹配
query = {
"query":{
"terms":{
"name":['aa','bb']
}
}
}
res = es.search(index='aaa', body=query)
print(res)
- 6.range 范围查询
gt:大于
gte:大于等于
lt:小于
lte:小于等于
query = {
"query": {
"range": {
"age": {
"gt": 20
},
"age": {
"lt": 40
}
}
}
}
res = es.search(index='aaa', body=query)
print(res)
{'took': 2, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'aaa', '_type': 'default', '_id': '2', '_score': 1.0, '_source': {'name': 'bb', 'age': 30, 'city': 'dd'}}]}}
- 7.exists / missing 查找文档中是否包含指定字段或没有某个字段,类似sql ISNULL
query = {
"query": {
"exists": {
"field": "name"
}
}
}
res = es.search(index="aaa", body=query)
print(res)
{'took': 4, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 3, 'max_score': 1.0, 'hits': [{'_index': 'aaa', '_type': 'default', '_id': '2', '_score': 1.0, '_source': {'name': 'bb', 'age': 30, 'city': 'dd'}}, {'_index': 'aaa', '_type': 'default', '_id': '1', '_score': 1.0, '_source': {'name': 'aa', 'age': 20, 'city': 'dd'}}, {'_index': 'aaa', '_type': 'default', '_id': '3', '_score': 1.0, '_source': {'name': 'bb', 'age': 40, 'city': 'dd'}}]}}
- 8.bool过滤 合并多个过滤条件结果的bool逻辑
must:多个条件完全匹配 相当于and
must_not:多个条件的相反匹配 相当于not
should: 至少有一个条件匹配 相当于or
query = {
"query": {
"bool": {
"must_not": {
"term": {"age": 40},
"term": {"name": "aaa"},
}
}
}
}
res = es.search(index='aaa', body=query)
print(res)
# 条件1:name != sally
# 条件2:字段age必须存在
# 条件3:age = 21
query = {
"query": {
"bool": {
"must_not": {
"term": {"name": "sally"}
},
"must": {
"exists": {"field": "age"}
},
"must": {
"term": {"age": 21}
}
}
}
}
- 9.multi_match 在match查询的基础上同时查询多个字段,在多个字段中同时查一个
query = {
"query": {
"multi_match": {
"query": "haha",
"fields": ["name", "hobbies"]
}
}
}
# 只要有一个字段满足"haha"
- 10.wildcard 使用标准的shell通配符查询 类似mysql LIKE
query = {
"query": {
"wildcard": {
"name": "*a"
}
}
}
res = es.search(index='aaa', body=query)
print(res)
{'took': 35, 'timed_out': False, '_shards': {'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0}, 'hits': {'total': 1, 'max_score': 1.0, 'hits': [{'_index': 'aaa', '_type': 'default', '_id': '1', '_score': 1.0, '_source': {'name': 'aa', 'age': 20, 'city': 'dd'}}]}}
- 11.regexp 正则
query = {
"query": {
"regexp": {
"name": ".a.*"
}
}
}
res = es.search(index='aaa', body=query)
print(res)
12.prefix前缀
query = {
"query": {
"prefix": {
"name": "wang"
}
}
}
res = es.search(index='aaa', body=query)
print(res)
13. match_phrase 短语匹配
query = {
"query": {
"match_phrase": {
"name": "dancing"
}
}
}
res = es.search(index='aaa', body=query)
print(res)
14. 根据id删除
es.delete(index="aaa", doc_type='default', id=1)
15. 根据查询条件删除
query = {
"query": {
"match": {
"name": "sally"
}
}
}
res = es.delete_by_query(index='aaa', body=query)
print(res)
16. 删除索引 (库)
es.indices.delete(index='aaa')
17. 检查索引是否存在 (库)
es.indices.exists(index='aaa')
- 18.更新
全局更新
在 Elasticsearch 中,通过指定文档的 _id, 使用 Elasticsearch 自带的 index api 可以实现插入一条 document , 如果该 _id 已存在,将直接更新该 document
因此,通过index API来对已有的文档实现更新,其实是进行了一次reindex的操作
es.index(index="test", doc_type="doc", id="dfebcXcBCWwWKoXwQ2Gk", body={
"name": "Python编程实战",
"num": 5})
局部更新
Update更新操作允许ES获得某个指定的文档,可以通过脚本等操作对该文档进行更新。
可以把它看成是先删除再索引的原子操作,只是省略了返回的过程,这样即节省了来回传输的网络流量,也避免了中间时间造成的文档修改冲突。
es.update(index="test", doc_type="doc", id="4Z6XcXcBChYTHL1ZdwjL", body={"doc": {"name": "Jerry", "age": 25}})
搜索更新
update_by_query
update_by_query,顾名思义,这种更新方式,即通过查询再更新。
该方法的优点是可以指定某些数据,然后达到更新的目的
在ES中,我们通过update_by_query中的query和script来实现先查询再更新的机制

在上面的操作中:
query字段,表示我们要查询的条件,根据该条件找到对应的数据
script字段包含以下关键字:
source是将要执行的脚本内容;
lang表示的是当前脚本的语言*;
param则是脚本执行的参数;
参考详情:https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-painless.html
批量更新
在实际需求中,面对最多的还是批量更新
当然你也可以通过 for 循环一条一条来更新,不过这种方法效率太低了。
尤其是面对数据量很大的时候,那真的是急死人…
好在 ES 有提供批量操作的接口 bulk
在Python 中可以直接导入使用
from elasticsearch.helpers import bulk
actions = []
for item in data_list:
_id = item.get("_id")
doc = item.get("doc")
index_action = {
'_op_type': 'update',
'_index': index_name,
'_type': "doc",
'_id': _id,
'doc': doc
}
actions.append(index_action)
if actions:
bulk(es, actions)
可以看到有个 doc 的参数,和上面介绍的 update 方法类似,doc中的值便是我们需要修改的字段内容
_op_type 为操作类型为update,表明是更新的操作
以该种方式组合的 index_action 组成数组,通过 bulk 便能实现批量更新
网友评论