from elasticsearch_dsl import connections, Search
# 方式一:连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
# 方式二:连接es
connections.create_connection(alias="my_new_connection", hosts=["127.0.0.1:9200"], timeout=20)
# 不使用别名使用
res = Search(using=es).index("test_index").query()
# print(res)
for data in res:
print(data.to_dict())
print("*" * 100)
# 使用别名后这样使用
res2 = Search(using="my_new_connection").index('test_index').query()
# print(e)
for data in res2:
print(data.to_dict())
运行结果:
示例代码2:
from elasticsearch_dsl import connections, Search
# 方式一:连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
# 不使用别名使用
res = Search(using=es).index("test_index").query()
# print(res)
for data in res:
print(data.to_dict())
print("*" * 100)
# 书写方式一:按条件查询数据
res2 = Search(using=es).index("test_index").query("match", name="张三") # 查询时注意分词器的使用
for data in res2:
print(data.to_dict())
print("*" * 100)
# 书写方式二:按条件查询数据
res3 = Search(using=es).index("test_index").query({"match": {"name": "张三"}})
for data in res3:
print(data.to_dict())
from elasticsearch_dsl import connections, Search, Q
from elasticsearch_dsl.query import MultiMatch, Match
# 方式一:连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
# 相对与{"multi_match": {"query": "ha", "fields": ["firstname", "lastname"]}}
m1 = MultiMatch(query="Ha", fields=["firstname", "lastname"])
res = Search(using=es, index="test_index").query(m1)
print(res)
for data in res:
print(data.to_dict())
# 相当于{"match": {"firstname": {"query": "Hughes"}}}
m2 = Match(firstname={"query": "Hughes"})
res = Search(using=es, index="test_index").query(m2)
print(res)
for data in res:
print(data.to_dict())
from elasticsearch_dsl import connections, Search, Q
from elasticsearch_dsl.query import MultiMatch, Match
# 方式一:连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
# q = Q("match", city="Summerfield")
q = Q("multi_match", query="Summerfield", fields=["city", "firstname"])
res = Search(using=es, index="test_index").query(q)
print(res)
for data in res:
print(data.to_dict())
from elasticsearch_dsl import connections, Search, Q
# 方式一:连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
# res = Search(using=es, index="test_index").query("match", address__city="北京")
res = Search(using=es, index="test_index").filter("term", address__city="北京")
# print(res)
for data in res:
print(data.to_dict(), data.name)
from elasticsearch_dsl import connections, Search, A
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
res = s.query().sort('-age').execute()
# print(res)
for data in res:
print(data.to_dict())
运行结果:
要指定from、size
示例代码:
from elasticsearch_dsl import connections, Search, A
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
res = s.query()[2: 5].execute() # {"from": 2, "size": 5}
# print(res)
for data in res:
print(data.to_dict())
from elasticsearch_dsl import connections, Search
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
res = s.query()
# print(res)
for hit in res.scan():
print(hit.age, hit.address)
from elasticsearch_dsl import connections, Search, A
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
a = A("terms", field="gender")
s.aggs.bucket("gender_terms", a)
res = s.execute()
# print(res)
for hit in res.aggregations.gender_terms:
print(hit.to_dict())
运行结果:
示例代码2:
from elasticsearch_dsl import connections, Search, A
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
s.aggs.bucket("per_gender", "terms", field="gender")
s.aggs["per_gender"].metric("sum_age", "sum", field="age")
s.aggs["per_gender"].bucket("terms_balance", "terms", field="balance")
res = s.execute()
# print(res)
for hit in res.aggregations.per_gender:
print(hit.to_dict())
运行结果:
示例代码3:
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
res = s.aggs.bucket("aaa", "terms", field="gender").metric("avg_age", "avg", field="age")
print(res.to_dict())
运行结果:
示例代码4:
【聚合,内置排序】
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
'terms': {
'field': 'age',
'order': {
'_count': 'desc'
s = Search(using=es, index="account_info")
res = s.aggs.bucket("agg_age", "terms", field="age", order={"_count": "desc"})
print(res.to_dict())
response = s.execute()
for hit in response.aggregations.agg_age:
print(hit.to_dict())
'terms': {
'field': 'age',
'order': {
'_count': 'asc'
'aggs': {
'avg_age': {
'avg': {
'field': 'age'
s2 = Search(using=es, index="account_info")
res2 = s2.aggs.bucket("agg_age", "terms", field="age", order={"_count": "asc"}).metric("avg_age", "avg", field="age")
print(res2.to_dict())
response = s2.execute()
for hit in response.aggregations.agg_age:
print(hit.to_dict())
运行结果:
示例代码5:
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
'aggs': {
'avg_age': {
'avg': {
'field': 'age'
s = Search(using=es, index="account_info").query("range", age={"gte": 28})
res = s.aggs.metric("avg_age", "avg", field="age")
print(res.to_dict())
response = s.execute()
print(response)
for hit in response:
print(hit.to_dict())
运行结果:
示例代码:
【目前似乎没有效果,待验证】
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="test_index")
res = s.highlight("id").execute().to_dict()
print(res)
运行结果:
source限制返回字段
示例代码:
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="account_info")
res = s.query().execute()
for hit in res:
print(hit.to_dict())
# 限制返回字段
s2 = Search(using=es, index="account_info")
res2 = s2.query().source(['account_number', 'address']).execute()
for hit in res2:
print(hit.to_dict())
运行结果:
调用Search对象上的delete方法而不是execute来实现删除匹配查询的文档
示例代码:
from elasticsearch_dsl import connections, Search, Q
# 连接es
es = connections.create_connection(hosts=["127.0.0.1:9200"], timeout=20)
# print(es)
s = Search(using=es, index="test_index")
res = s.query("match", name="张").delete()
print(res)