目录
1、索引操作(增、删)
1.1 增加一条索引
res=es.index(index='my_index',doc_type='my_doc_type',body=doc)
或者
es.indices.create(index='my-index',ignore=400)
es.index(index='my_index',doc_type='my_doc_type',id=1,body{}) #增加一条
1.2 查看索引下的数据(get接口)
result=es.get(index='my_index',doc_type='my_doc_type',id=1)
1.3 删除索引
es.indices.delete(index='my_index')
2、数据操作(增、删、改、bulk批量插入)
2.1 增加一条数据
es.index(index='my_index',doc_type='my_doc_type',id=1,body{})
2.2 修改数据
有具体id的数据,直接使用update:
new_doc_body={}
es.update(index='my_index',id=1,body=new_doc_body)
或者使用update_by_query接口:
query={
'query':{
'match':{
'last_name':'xiao'
}}}
result=es.update_by_query(index='my_index',body=query)
2.3 删除数据
有指定id,使用delete:
es.delete(index='my-index',doc_type='my_doc_type',id=1)
没有id,需要满足查询条件再删除,使用delete_by_query:
query={
'query':{
'match':{
'name':'xiao'}}}
result=es.delete_by_query(index='my_index',body=query)
2.4 bulk批量插入数据
actions参数传入数组的情况:
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import random
client = Elasticsearch("localhost:9200")
levels = ['今天是个好日子', '中国万岁', '天安门广场', '现在在那里干嘛呢?']
actions = []
#构造模拟数据
for i in range(100):
level = levels[random.randrange(0, len(levels))]
action = {'_index': 'index', # 操作 index update create delete
'_type': 'level,
'_source': {'level': level}}
actions.append(action)
# bulk批量插入
helpers.bulk(client=client,actions=actions)
actions参数传入一个生成器:
from elasticsearch import Elasticsearch
from elasticsearch import helpers
import random
client = Elasticsearch("localhost:9200")
levels = ['今天是个好日子', '中国万岁', '天安门广场', '现在在那里干嘛呢?']
actions = []
# 生成器
def gendata():
for i in range(100):
level = levels[random.randrange(0, len(levels))]
action = {'_op_type': 'index', # 操作 index update create delete
'_index': 'level',
'_type': 'doc',
'_source': {'level': level}}
yield action
# bulk批量插入
helpers.bulk(client=client,actions=gendata())
3、数据查询(DSL语句)
3.1 match查询
标准查询,用于对某个字段内某个值进行精确搜索:
query={
'query':{
'match':{
'about':'rock'
}
}
}
result=es.search(index='my_index',body=query)
3.2 multi_match查询
multi_match查询是在match查询的基础上同时搜索多个字段:
query={
'query':{
'multi_match':{
'query':'music',
'fields':['about','interests']
}
}
}
result=es.search(index='my_index',body=query)
备注:这里fields指的是在about和interests这两个字段中搜索含有music这个单词的文档。
3.3 match_all查询
查询所有的文档:
query={
'query':{
'match_all':{}
}
}
result=es.search(index='my_index',body=query)
3.1 term过滤
主要用于精确匹配,使用search接口:
query = {
"query": {
"term":{
'age': 32
}
}
}
result=es.search(index='my_index',body=query)
3.2 terms过滤
terms过滤与term过滤相似,但是terms过滤允许指定多个匹配条件。
query = {
"query": {
"terms":{
'age': [32, 25]
}
}
}
result=es.search(index='my_index',body=query)
3.3 range过滤
按照指定的范围查找:
- gt:大于
- gte:大于等于
- lt:小于
- lte:小于等于
query = {
"query": {
"range":{
'age': {
"gt":34
}
}
}
}
result=es.search(index='my_index',body=query)
3.4 exists过滤和missing过滤
- exits过滤查找文档中是否含有某个字段;
- missing过滤查找文档中没有某个字段;
query={
'query':{
'exists':{
'field':'first_name'
}
}
}
result=es.search(index='my_index',body=query)
3.5 bool过滤
用于合并多个过滤条件:
- must :多个条件必须同时满足,相当于and;
- must_not:条件的对立面,相当于not;
- should:至少有一个满足一个条件,相当于or。
query={
'query':{
'bool':{
'must':{
'term':{'_score':1},
'term':{'age':32}
}}}}
result=es.search(index='my_index',body=query)
3.6 bool查询
bool查询与bool过滤相似,用于合并多个查询子句,不同的是bool过滤直接给出是否匹配成功,而bool查询要计算每一个查询子句的相关性分(_score):
query = {
"query": {
"bool": {
"must": {
"match": { "last_name": 'Smith' }
},
"must_not":{
"exists": {
"field": "name"
}
}
}
}
}
result = es.search(index="my_index", body=query)
3.7 统计功能
如统计about字段中,含有I love这两个单词的文档数量,使用count接口:
query={
'query':{
'match_phrase':{
'about':'I love'
}
}
}
result=es.count(index='my_index',body=query)