Python3操作ES7
1、连接ES7,创建索引
from elasticsearch import Elasticsearch
es = Elasticsearch(['127.0.0.1:9200'])
index_name = 'student'
request_body = {
"mappings": {
"properties": {
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"height": {
"type": "integer"
}
}
}
}
es.indices.create(index=index_name, body=request_body)
2、判断索引是否存在
from elasticsearch import Elasticsearch
es = Elasticsearch(['127.0.0.1:9200'])
index_name = "student"
if es.indices.exists(index_name) == True:
print('索引存在')
else:
print("索引不存在")
3、删除ES7的 student 索引
from elasticsearch import Elasticsearch
es = Elasticsearch(['127.0.0.1:9200'])
index_name = 'student'
es.indices.delete(index=index_name)
4、Python3 操作 ES7(查询向)
from elasticsearch import Elasticsearch
es = Elasticsearch(['127.0.0.1:9200'])
index_name = 'student'
dsl = {
"query":{
"match_all":{}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"term": {
"name": "张三"
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"terms": {
"name": [
"张三", "李四"
]
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"match": {
"name": "张三"
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"multi_match": {
"query": "李四",
"fields": ["name", "addr"]
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"ids": {
"values": [
"1", "2"
]
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"bool": {
"must": [
{
"term": {
"name": "张三"
}
},
{
"term": {
"age": "16"
}
}
]
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"from":0,
"size":1
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"range": {
"age": {
"gte": 18,
"lte": "30"
}
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"prefix": {
"name": "李"
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query": {
"wildcard": {
"name": "*三"
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"sort":{
"age":{
"order":"asc"
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"sort":[
{
"age":{
"order":"asc"
}
},
{
"name":{
"order":"asc"
}
}
],
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
res = es.search(index=index_name, doc_type='doc', filter_path=["hits.hits._*"])
print(res)
res = es.count(index=index_name, doc_type='doc')
print(res)
dsl = {
"query":{
"match_all":{}
},
"aggs":{
"min_age":{
"min":{
"field":"age"
}
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"aggs":{
"max_age":{
"max":{
"field":"age"
}
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl ={
"query":{
"match_all":{}
},
"aggs":{
"sum_age":{
"sum":{
"field":"age"
}
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"aggs":{
"avg_age":{
"sum":{
"field":"age"
}
}
}
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)
dsl = {
"query":{
"match_all":{}
},
"size":"50",
"from":"0"
}
res = es.search(index=index_name, doc_type='doc', body=dsl)
print(res)