官網下載 http://www.elasticsearch.org/download/
解開壓縮檔後,至bin目錄下即可執行啟動 elasticsearch
> elasticsearch.bat 或 > elasticsearch.sh
安裝 urllib3 package
> easy_install urllib3 或 > pip install urllib3
或至官網下載https://pypi.python.org/pypi/elasticsearch
接著利用下列程式測試一下...
# -*- coding: utf-8 -*-
import urllib3
http = urllib3.proxy_from_url('http://proxy.hinet.net/')
r = http.request('GET', 'http://tekibrain.blogspot.com/')
print r.status # 正常的話應該會印出 '200',網站傳回 HTTP Return code '200'
print r.data # 印出網頁原始碼資料
安裝 python client for elasticsearch package
> easy_install elasticsearch
或者
> pip install elasticsearch
或至官網下載 https://pypi.python.org/pypi/urllib3
接著利用下列程式 estest.py 測試一下...
# -*- coding: utf-8 -*-
from datetime import datetime
from elasticsearch import Elasticsearch
# 預設連線至 ElasticSearch Server Port 9200, localhost:9200
es = Elasticsearch()
# 加入資料進行索引, 自己設定 id = 1
# http://localhost:9200/social/tweet/1
setdata = es.index(index="social", doc_type="tweet", id=1, body={"content": "大家好","user":{"name":"老王","id":670085},"tags":["demo","test"], "timestamp": datetime.now()})
print 'set data => '
print setdata
setdata = es.index(index="social", doc_type="tweet", id=2, body={"content": "大家好2","user":{"name":"小王","id":670086},"tags":["demo","test2"], "timestamp": datetime.now()})
print 'set data => '
print setdata
# 取得 id=1 資料
getdata = es.get(index="social", doc_type="tweet", id=1)['_source']
print '-------------------------'
print 'get data <= '
es.indices.refresh(index="social")
# Search:
qdoc = {
"query": {
"match" : {
"tags" : "demo"
}
}
}
getdata = es.search(index="social", body=qdoc)
print 'get data <= '
#print type(getdata)
print("Got %d Hits:" % getdata['hits']['total'])
for hit in getdata['hits']['hits']:
print("%(content)s %(user)s: %(tags)s" % hit["_source"])
測試成功結果 (timestamp依照系統時間而不同):
> python estest.py
set data =>
{u'_type': u'tweet', u'_id': u'1', u'ok': True, u'_version': 44, u'_index': u'social'}
set data =>
{u'_type': u'tweet', u'_id': u'2', u'ok': True, u'_version': 42, u'_index': u'social'}
-------------------------
get data <=
get data <=
Got 2 Hits:
大家好2 {u'name': u'\u5c0f\u738b', u'id': 670086}: [u'demo', u'test2']
大家好 {u'name': u'\u8001\u738b', u'id': 670085}: [u'demo', u'test']
#
沒有留言:
張貼留言