Skip to content

Instantly share code, notes, and snippets.

@victortsaitw12
Last active August 8, 2018 12:58
Show Gist options
  • Select an option

  • Save victortsaitw12/5cc2404ba906ff38270732c17f11cc61 to your computer and use it in GitHub Desktop.

Select an option

Save victortsaitw12/5cc2404ba906ff38270732c17f11cc61 to your computer and use it in GitHub Desktop.
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