Last active
August 3, 2017 12:15
-
-
Save YabZhang/ad2aaf98c2ca32dbc10a695940344165 to your computer and use it in GitHub Desktop.
es demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| from datetime import datetime | |
| from elasticsearch_dsl import ( | |
| DocType, Date, Nested, Boolean, | |
| analyzer, token_filter, InnerObjectWrapper, Keyword, Text, String | |
| ) | |
| # 中文分词器 | |
| jieba_stop = token_filter( | |
| 'jieba_stop', | |
| type='stop', | |
| stopwords_path='stopwords/stopwords.txt' | |
| ) | |
| # 解析器 | |
| my_analyzer = analyzer( | |
| 'my_analyzer', | |
| tokenizer='standard', | |
| filter=[ | |
| jieba_stop, | |
| 'lowercase', | |
| 'snowball' | |
| ] | |
| ) | |
| # comment wrapper | |
| class Comment(InnerObjectWrapper): | |
| def age(self): | |
| return datetime.now() - self.created_at | |
| # post model | |
| class Post(DocType): | |
| """post model""" | |
| # meta class | |
| class Meta: | |
| doc_type = 'post' | |
| index = 'blog_post' | |
| title = String(analyzer=my_analyzer) | |
| content = Text(analyzer=my_analyzer) | |
| author = String(index='not_analyzed') | |
| created_at = Date() | |
| published = Boolean() | |
| category = Text( | |
| analyzer=my_analyzer, | |
| fields={'raw': Keyword()} | |
| ) | |
| # comment | |
| comments = Nested( | |
| doc_class=Comment, | |
| properties={ | |
| 'author': Text(fields={'raw': Keyword()}), | |
| 'content': Text(analyzer=my_analyzer), | |
| 'created_at': Date() | |
| } | |
| ) | |
| def add_comment(self, author, content): | |
| """add comment""" | |
| self.comments.append({ | |
| 'author': author, | |
| 'content': content | |
| }) | |
| def save(self, **kwargs): | |
| """save post""" | |
| self.created_at = datetime.now() | |
| return super(Post, self).save(**kwargs) | |
| if __name__ == '__main__': | |
| from elasticsearch_dsl.connections import connections | |
| from elasticsearch_dsl import Q | |
| # create connection | |
| connections.create_connection(hosts=['127.0.0.1']) | |
| Post.init() | |
| first = Post(title='第一篇文章', published=True) | |
| first.category = ['杂文', '散文'] | |
| first.content = '这是我的第一篇文章,欢迎评论!' | |
| first.save() | |
| print(first.meta.id) | |
| second = Post(title='第二篇文章', published=False) | |
| second.category = ['心情'] | |
| second.content = '今天天气晴朗,心情不错。' | |
| second.add_comment('游客', '据说中午有雨。手动滑稽~') | |
| second.save() | |
| print(second.meta.id) | |
| s = Post.search() | |
| query = Q({'term': {'published': 'True'}}) | |
| print('query', query.to_dict()) # query {'term': {'published': 'True'}} | |
| resp = s.filter(query).execute() | |
| print('query result', resp) | |
| # query result <Response: [Post(index='blog_post', doc_type='post', id='AV2n_W-PtUa0ZQe-mC7u')]> | |
| for post in resp: | |
| print(post.meta.score, post.title, post.published, post.content) | |
| # 0.0 第一篇文章 True 这是我的第一篇文章,欢迎评论! | |
| resp = s.query(Q({'match': {'title': '文章'}})).execute() | |
| for post in resp: | |
| print(post.meta.score, post.title, post.published, post.content) | |
| """ | |
| [ES] ['blog_post'] ['post'] {'query': {'match': {'title': '文章'}}} {} | |
| 0.35806638 第一篇文章 True 这是我的第一篇文章,欢迎评论! | |
| 0.35806638 第二篇文章 False 今天天气晴朗,心情不错。 | |
| """ | |
| """ | |
| [ES] ['blog_post'] ['post'] {'query': {'match': {'title': '第一篇'}}} {} | |
| 1.0387118 第一篇文章 True 这是我的第一篇文章,欢迎评论! | |
| 0.35806638 第二篇文章 False 今天天气晴朗,心情不错。 | |
| """ | |
| """ | |
| [ES] ['blog_post'] ['post'] {'query': {'match': {'content': '天气'}}} {} | |
| 1.633095 第二篇文章 False 今天天气晴朗,心情不错。 | |
| """ | |
| """ | |
| [ES] ['blog_post'] ['post'] {'query': {'match': {'content': '评论'}}} {} | |
| 1.3728157 第一篇文章 True 这是我的第一篇文章,欢迎评论! | |
| """ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment