用于ElasticSearch1的Python客户端
elasticsearch1-dsl的Python项目详细描述
ElasticSearchDSL是一个高级库,其目标是帮助编写和 对ElasticSearch运行查询。它是建立在官方之上的 低级客户端(elasticsearch-py)。
它提供了一种更方便、更惯用的方式来编写和操作 询问。它与elasticsearch的json dsl很接近,反映了它的 术语和结构。它公开了python中dsl的整个范围 直接使用定义的类或类查询集表达式。
它还提供了一个可选的包装器,用于将文档用作python 对象:定义映射、检索和保存文档、包装 在用户定义的类中记录数据。
要使用其他ElasticSearch API(如群集运行状况),只需使用 潜在客户。
兼容性
库与自1.x以来的所有ElasticSearch版本兼容,但是 必须使用匹配的主版本:
对于elasticsearch 2.0及更高版本,请使用 图书馆。
对于elasticsearch 1.0及更高版本,请使用 图书馆。
在setup.py中设置需求的推荐方法 requirements.txt是:
# Elasticsearch 2.x elasticsearch-dsl>=2.0.0,<3.0.0 # Elasticsearch 1.x elasticsearch-dsl<2.0.0
开发分别在master和1.x分支上进行。
搜索示例
让我们将一个典型的搜索请求直接写为dict:
fromelasticsearch1importElasticsearchclient=Elasticsearch()response=client.search(index="my-index",body={"query":{"filtered":{"query":{"bool":{"must":[{"match":{"title":"python"}}],"must_not":[{"match":{"description":"beta"}}]}},"filter":{"term":{"category":"search"}}}},"aggs":{"per_tag":{"terms":{"field":"tags"},"aggs":{"max_lines":{"max":{"field":"lines"}}}}}})forhitinresponse['hits']['hits']:print(hit['_score'],hit['_source']['title'])fortaginresponse['aggregations']['per_tag']['buckets']:print(tag['key'],tag['max_lines']['value'])
这种方法的问题是它非常冗长,容易出现语法错误 不正确的嵌套、难以修改(例如添加另一个过滤器)和 写起来肯定不好玩。
让我们使用python dsl重写示例:
fromelasticsearch1importElasticsearchfromelasticsearch1_dslimportSearch,Qclient=Elasticsearch()s=Search(using=client,index="my-index") \ .filter("term",category="search") \ .query("match",title="python") \ .query(~Q("match",description="beta"))s.aggs.bucket('per_tag','terms',field='tags') \ .metric('max_lines','max',field='lines')response=s.execute()forhitinresponse:print(hit.meta.score,hit.title)fortaginresponse.aggregations.per_tag.buckets:print(tag.key,tag.max_lines.value)
如您所见,图书馆负责:
- creating appropriate ^{tt8}$ objects by name (eq. “match”)
- composing queries into a compound ^{tt9}$ query
- creating a ^{tt10}$ query since ^{tt11}$ was used
- providing a convenient access to response data
- no curly or square brackets everywhere
持久性示例
让我们用一个简单的python类来表示博客系统中的一篇文章:
fromdatetimeimportdatetimefromelasticsearch1_dslimportDocType,String,Date,Integerfromelasticsearch1_dsl.connectionsimportconnections# Define a default Elasticsearch clientconnections.create_connection(hosts=['localhost'])classArticle(DocType):title=String(analyzer='snowball',fields={'raw':String(index='not_analyzed')})body=String(analyzer='snowball')tags=String(index='not_analyzed')published_from=Date()lines=Integer()classMeta:index='blog'defsave(self,**kwargs):self.lines=len(self.body.split())returnsuper(Article,self).save(**kwargs)defis_published(self):returndatetime.now()>self.published_from# create the mappings in elasticsearchArticle.init()# create and save and articlearticle=Article(meta={'id':42},title='Hello world!',tags=['test'])article.body=''' looong text '''article.published_from=datetime.now()article.save()article=Article.get(id=42)print(article.is_published())# Display cluster healthprint(connections.get_connection().cluster.health())
在本例中,您可以看到:
- providing a default connection
- defining fields with mapping configuration
- setting index name
- defining custom methods
- overriding the built-in ^{tt12}$ method to hook into the persistence life cycle
- retrieving and saving the object into Elasticsearch
- accessing the underlying client for other APIs
您可以在文档的持久性一章中看到更多内容。
从elasticsearch-py
迁移您不必移植整个应用程序即可获得 python dsl,您可以通过从 现有的^ {TT7}$,使用API修改它并将其序列化为 dict:
body={...}# insert complicated query here# Convert to Search objects=Search.from_dict(body)# Add some filters, aggregations, queries, ...s.filter("term",tags="python")# Convert back to dict to plug back into existing codebody=s.to_dict()
许可证
2013 ElasticSearch版权所有
根据apache许可证2.0版(以下简称“许可证”)授权; 除非符合许可证,否则您不能使用此文件。 您可以在
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则软件 根据许可证分发是按“原样”分发的, 无任何明示或默示的保证或条件。 有关管理权限的特定语言和 许可下的限制。