elasticsearch,python包pyes进行的处理
elasticsearch:高性能搜索引擎,官網:https://www.elastic.co/products/elasticsearch/
對于它相信大家都不陌生,es的使用已經廣泛存在 各大網站中。對于python的支持也有很多優秀的框架,如pyes,elasticsearch等
雜家使用最新的es2.2并本地集群。pyes最新版本0.99.5
可以自信的說,如果你想通過中文搜索查出pyes的使用文章,本篇將是最新的,可使用的文章。
由于網上基本找不到相關中文文章支持最新的2.2es和pyes0.99,我從github上認真瀏覽了作者的test。
1:創建刪除 index
conn = pyes.ES(ES_PATH, timeout=200.0)
conn.indices.create_index(name)
#主動創建mapping ? #name,index的name,index_type為您自己設定的type,FIELD_MAPPING 為您的mapping設計
conn.indices.put_mapping(index_type, {'properties':FIELD_MAPPING}, [name])
?
刪除index
conn.indices.delete_index(index_name)
?
2:插入數據:es支持多中數據類型,你可以仔細瀏覽es官網的mapping field介紹
conn.index(params,index_name,index_type)
#params 為插入數據的字典類型
#如您的mapping設計如下
index_mapping = {
"id" :
{"index":"no","type":u'integer'},
"sha1" :
{"index":"analyzed","type":u'string','store': 'yes'},
#標題
"title":
{"index":"analyzed","type":u'string','store': 'yes',},
#作者
"author" :
{"index":"analyzed","type":u'string','store': 'yes',},
#創建時間
"creation_time" :
{"index":"analyzed","type":u'date'},
}
您的params 將為 params_dic = {"id":"123","sha1":"sfsafsfsdfd","title":"我是銀哥哥","author":"yingege","creation_time":datatime(2016,1,23)}
?
3:簡單的term查詢
q = TermQuery(field, query)
results = conn.search(query = q,indices=index_name,doc_types=index_type)
循環即可得到所有的results數據
4:bool查詢即進行 and or not處理查詢
must1 = pyes.TermQuery(field1,query1)
must2 = pyes.TermQuery(field2,query2)
must= [must1,must2]
query = pyes.BoolQuery(must = must)
conn.search(query = query,indices = index_name,doc_types = index_type)
如何你and的條件多個,append must列表即可
?
接下來就是 參數must_not,should了,您可以清楚的了解到 or=should,must_not = not了
5:排序查詢?SortOrder類,具體定義您可以help(SortOrder)查看他的源碼定義
search = Search(query)
sort_order = SortOrder(sort_field, sort_type)
search.sort.add(sort_order)
conn.search(search,indices=index_name)
上面的query及為 bool查詢query,或者term查詢
sort_type 當然是排序類型。desc,asc等,具體可以查看es官網:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
當然以后官網更新了,大家可以去具體查找。
6:range范圍查詢,
有時間范圍查詢,pyes.RangeQuery(pyes.ESRange(field, from_value=start_date, to_value=end_date))
地理位置范圍查詢,這個具體和?GeoPolygonFilter類相關
7:匹配查詢MatchQuery
此查詢可以應用在 系統的用戶搜索功能上:
matchq =?MatchQuery(field,'hhhh')
conn.search(matcdq)
8:django類型model查詢
from pyes.queryset import generate_model
generate_model(index_name, index_type,es_url=ES_PATH)
這樣的話,上面的返回值即可使用django的orm查詢了。如:
article =?generate_model(index_name, index_type,es_url=ES_PATH)
article_all = article.objects.all()
其他 article.objects.filter()
?
下一章:銀哥哥具體描述pyes的其他支持,如raw_search等的應用。如果對es知識不了解的,大家可以逛逛官網,看看神奇的DSL
?
轉載于:https://www.cnblogs.com/yinxingpan/p/5366958.html
總結
以上是生活随笔為你收集整理的elasticsearch,python包pyes进行的处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 农村供暖锅炉可以用生物质的吗
- 下一篇: leetcode 21 Merge T