在python中使用elasticsearch做為搜索引擎
一直想找一個快速全文搜索的工具,目前找到的有Sphinx,xapian,Lucene,solr, elasticsearch ,whoosh,hyper estraier等,原本一直不太喜歡用java系的,內存大戶傷不起啊。嘗試了sphinx,xapian,hyper estraier,其中xapian資料太少,hyper estraier雖然比較簡單,但資料也少。sphinx到是有一個中文化的分支coreseek,然后看到文檔里面提到sphinx支持一元切分,但根 據查詢的例子去查的結果不是我想要的,不知道是不是我的查詢語句用錯了。而且因為我是在windows上測試的,而我的python又是2.7的版本,無 法在 coreseek 上直接使用,應該需要重新編譯。后來看到 elasticsearch ,真是亮瞎老夫的狗眼啊,這貨直接可以用restful json操作又有pyes,pyelasticsearch這些已經封裝好的操作庫。 elasticsearch 還是支持分布式,擴展也方便了。由于是java開發的,跨平臺也無問題,默認單機嘗試的時候無須改配置,直接運行 bin/elasticsearch.bat 就可以了。
安裝pyes
pip install pyes
使用例子
#coding:utf-8 import pyes conn = pyes.ES(['127.0.0.1:9200'])#連接es conn.create_index('test-index')#新建一個索引 #定義索引存儲結構 mapping = { u'parsedtext': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', "term_vector" : "with_positions_offsets"}, u'name': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', "term_vector" : "with_positions_offsets"}, u'title': {'boost': 1.0, 'index': 'analyzed', 'store': 'yes', 'type': u'string', "term_vector" : "with_positions_offsets"}, u'position': {'store': 'yes', 'type': u'integer'}, u'uuid': {'boost': 1.0, 'index': 'not_analyzed', 'store': 'yes', 'type': u'string'} } conn.put_mapping("test-type", {'properties':mapping}, ["test-index"])#定義test-type conn.put_mapping("test-type2", {"_parent" : {"type" : "test-type"}}, ["test-index"])#從test-type繼承 #插入索引數據 #{"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}: 文檔數據 #test-index:索引名稱 #test-type: 類型 #1: id 注:id可以不給,系統會自動生成 conn.index({"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1}, "test-index", "test-type", 1) conn.index({"name":"data1", "value":"value1"}, "test-index", "test-type2", 1, parent=1) conn.index({"name":"Bill Baloney", "parsedtext":"Bill Testere nice guy", "uuid":"22222", "position":2}, "test-index", "test-type", 2) conn.index({"name":"data2", "value":"value2"}, "test-index", "test-type2", 2, parent=2) conn.index({"name":u"百 度 中 國"}, "test-index", "test-type")#這個相當于中文的一元切分吧-_- conn.index({"name":u"百 中 度"}, "test-index", "test-type") conn.default_indices=["test-index"]#設置默認的索引 conn.refresh()#刷新以獲得最新插入的文檔 q = pyes.TermQuery("name", "bill")#查詢name中包含bill的記錄 results = conn.search(q) for r in results: print r #查詢name中包含 百度 的數據 q = pyes.StringQuery(u"百 度",'name') results = conn.search(q) for r in results: print r #查詢name中包含 百度 或著 中度 的數據 q = pyes.StringQuery(u"百 度 OR 中 度",'name') results = conn.search(q) for r in results: print r
本文由用戶 fnme 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!