【ES】简单使用

 import sys
reload(sys)
sys.setdefaultencoding('utf-8') from datetime import datetime
from elasticsearch import Elasticsearch
from os import path
import jieba
import random
es = Elasticsearch() filePath = path.dirname(__file__) # index1:wordcount
# stopwords
stopWordFile = u'stopwords.txt'
stopWordList = []
for L in open(path.join(filePath , stopWordFile)).readlines():
stopWordList.append(L.strip().decode('utf-8'))
stopWordList.extend([u'腾讯',u'视频' , u'。'])
stopWordList = set(stopWordList) # information words
new = 'words.txt'
text = open(path.join( filePath , new )).read().strip('\r')
wordDict = {}
for w in jieba.cut(text):
if w not in stopWordList:
wordDict.setdefault(w , 0)
wordDict[w] += 1 for key in wordDict.keys():
data = {'word':key , 'count':wordDict[key]}
es.index(index = 'wordcount' , doc_type = 'test' , body = data)
上一篇:BaseAdapter以及对ListView的优化(转)


下一篇:C#面向对象面试题集锦