Last active
August 8, 2018 12:58
-
-
Save victortsaitw12/5cc2404ba906ff38270732c17f11cc61 to your computer and use it in GitHub Desktop.
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
| def saveToELK(item): | |
| if Utility.elk is None: | |
| Utility.elk = Elasticsearch(hosts= 'elasticsearch') | |
| # 檢查如果沒有這個 index 則新增 | |
| if Utility.elk.indices.exists(index='article') is not True: | |
| res = Utility.elk.indices.create(index="article", body={ | |
| "mappings": { | |
| "article": { | |
| "properties": { | |
| "title": { | |
| "type": "text", | |
| "index": True, | |
| # 新增跟搜尋都使用中文分詞 | |
| "analyzer": "ik_max_word", | |
| "search_analyzer": "ik_max_word" | |
| }, | |
| "content": { | |
| "type": "text", | |
| "index": True, | |
| # 新增跟搜尋都使用中文分詞 | |
| "analyzer": "ik_max_word", | |
| "search_analyzer": "ik_max_word" | |
| }, | |
| "time": { | |
| "type": "date", | |
| } | |
| } | |
| } | |
| } | |
| }) | |
| # 新增文章 | |
| response = Utility.elk.index(index='article', doc_type='article', | |
| id=item['article_id'], | |
| body=item) | |
| print(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment