python3中的elasticsearch_dsl(例子)

下载python的模块
pip3 install elasticsearch_dsl
单一字段查询

#导入模块
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Search
#实例化
es = connections.create_connection(hosts=['10.0.122.47'])
#过滤
ser = Search(
    using = es,
#index是索引
    index = "people").filter(
       "match" , country="China"
    ).query(
# query后可以在加一个过滤的条件 
     "match" , age=35
    )
#res
res = ser.execute()

print(res)
print(ser.count())
#打印对应的值
for item in ser:
    print(item.age, item.name)

默认链接

#这个 Search 的对象也可以转换为之前的 Query DSL 格式
# print(ser.to_dict())

from elasticsearch_dsl.query import MultiMatch, Match

# m = Match(clientip={"query": "123.244.101.255"})


# mm = MultiMatch(
#     # 被搜索字段的内容
#     query="123.244.101.255",

#     # 被搜索的字典
#     fields=["clientip", 'timestamp'])
# s = Search().query(mm)
#基于Q的查询(强大的Q)
#示例
from elasticsearch_dsl import Q
# q = Q("multi_match", query="123.244.101.255", fields=["clientip", 'timestamp'])
q = Q("match", clientip="123.244.101.255") | Q("match", clientip="42.248.168.61")

s = Search().query(q)

for item in s:
    print(item.clientip)

你可能感兴趣的:(python3中的elasticsearch_dsl(例子))