连接 Es:
import elasticsearch
es = elasticsearch.Elasticsearch([{‘host’: ‘127.0.0.1’, ‘port’: 9200}])
先看一下搜索,q
是指搜索内容,空格对 q
查询结果没有影响,size
指定个数,from_
指定起始位置,filter_path
可以指定需要显示的数据,如本例中显示在最后的结果中的只有 _id
和 _type
。
res_3 = es.search(index=“bank”, q=“Holmes”, size=1, from_=1)
res_4 = es.search(index=“bank”, q=" 39225 5686 ", size=1000, filter_path=[‘hits.hits._id’, ‘hits.hits._type’])
查询指定索引的所有数据:
其中,index 指定索引,字符串表示一个索引;列表表示多个索引,如 index=["bank", "banner", "country"]
;正则形式表示符合条件的多个索引,如 index=["apple*"]
,表示以 apple
开头的全部索引。
search
中同样可以指定具体 doc-type
。
from elasticsearch_dsl import Search
s = Search(using=es, index=“index-test”).execute()
print s.to_dict()
根据某个字段查询,可以多个查询条件叠加:
s = Search(using=es, index=“index-test”).query(“match”, sip=“192.168.1.1”)
s = s.query(“match”, dip=“192.168.1.2”)
s = s.excute()
多字段查询:
from elasticsearch_dsl.query import MultiMatch, Match
multi_match = MultiMatch(query=‘hello’, fields=[‘title’, ‘content’])
s = Search(using=es, index=“index-test”).query(multi_match)
s = s.execute()
print s.to_dict()
还可以用 Q()
对象进行多字段查询,fields
是一个列表,query
为所要查询的值。
from elasticsearch_dsl import Q
q = Q(“multi_match”, query=“hello”, fields=[‘title’, ‘content’])
s = s.query(q).execute()
print s.to_dict()
Q()
第一个参数是查询方法,还可以是 bool
。
q = Q(‘bool’, must=[Q(‘match’, title=‘hello’), Q(‘match’, content=‘world’)])
s = s.query(q).execute()
print s.to_dict()
通过 Q()
进行组合查询,相当于上面查询的另一种写法。
q = Q(“match”, title=‘python’) | Q(“match”, title=‘django’)
s = s.query(q).execute()
print(s.to_dict())
{“bool”: {“should”: […]}}
q = Q(“match”, title=‘python’) & Q(“match”, title=‘django’)
s = s.query(q).execute()
print(s.to_dict())
{“bool”: {“must”: […]}}
q = ~Q(“match”, title=“python”)
s = s.query(q).execute()
print(s.to_dict())
{“bool”: {“must_not”: […]}}
过滤,在此为范围过滤,range
是方法,timestamp
是所要查询的 field
名字,gte
为大于等于,lt
为小于,根据需要设定即可。
关于 term
和 match
的区别,term
是精确匹配,match
会模糊化,会进行分词,返回匹配度分数,(term
如果查询小写字母的字符串,有大写会返回空即没有命中,match
则是不区分大小写都可以进行查询,返回结果也一样)
范围查询
s = s.filter(“range”, timestamp={“gte”: 0, “lt”: time.time()}).query(“match”, country=“in”)
普通过滤
res_3 = s.filter(“terms”, balance_num=[“39225”, “5686”]).execute()
其他写法:
s = Search()
s = s.filter(‘terms’, tags=[‘search’, ‘python’])
print(s.to_dict())
{‘query’: {‘bool’: {‘filter’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}}
s = s.query(‘bool’, filter=[Q(‘terms’, tags=[‘search’, ‘python’])])
print(s.to_dict())
{‘query’: {‘bool’: {‘filter’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}}
s = s.exclude(‘terms’, tags=[‘search’, ‘python’])
或者
s = s.query(‘bool’, filter=[~Q(‘terms’, tags=[‘search’, ‘python’])])
print(s.to_dict())
{‘query’: {‘bool’: {‘filter’: [{‘bool’: {‘must_not’: [{‘terms’: {‘tags’: [‘search’, ‘python’]}}]}}]}}}
聚合可以放在查询,过滤等操作的后面叠加,需要加 aggs
。
bucket
即为分组,其中第一个参数是分组的名字,自己指定即可,第二个参数是方法,第三个是指定的 field
。
metric
也是同样,metric
的方法有 sum
、avg
、max
、min
等,但是需要指出的是,有两个方法可以一次性返回这些值,stats
和 extended_stats
,后者还可以返回方差等值。
实例1
s.aggs.bucket(“per_country”, “terms”, field=“timestamp”).metric(“sum_click”, “stats”, field=“click”).metric(“sum_request”, “stats”, field=“request”)
实例2
s.aggs.bucket(“per_age”, “terms”, field=“click.keyword”).metric(“sum_click”, “stats”, field=“click”)
实例3
s.aggs.metric(“sum_age”, “extended_stats”, field=“impression”)
实例4
s.aggs.bucket(“per_age”, “terms”, field=“country.keyword”)
实例5,此聚合是根据区间进行聚合
a = A(“range”, field=“account_number”, ranges=[{“to”: 10}, {“from”: 11, “to”: 21}])
res = s.execute()
最后依然要执行 execute()
,此处需要注意,s.aggs
操作不能用变量接收(如 res=s.aggs
,这个操作是错误的),聚合的结果会保存到 res
中显示。
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)
dnimg.cn/img_convert/6c361282296f86381401c05e862fe4e9.png)
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以扫码获取!!!(备注:Python)