elasticsearch,python包pyes进行的处理
2016-04-08 10:29
218 查看
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
对于它相信大家都不陌生,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
相关文章推荐
- leetcode @python 134. Gas Station
- 据廖雪峰python3教程----python学习第九天
- python调用fortran模块
- Python读取绝对路径下的文件的问题
- 安装Python和pip
- python学习笔记(excel简单操作)
- 【python学习】日志文件里IP访问最多的3个
- leetcode @python 133. Clone Graph
- leetcode @python 132. Palindrome Partitioning II
- leetcode @python 131. Palindrome Partitioning
- Python学习笔记day12
- python(12)给文件读写上锁
- 统计中文分词(最大熵)
- Python基础教程(第2版•修订版)代码清单2-3 勘误
- python sys模块
- python随机数
- python 小模块 ---marshai模块
- python3使用urllib模块制作网络爬虫
- Python—sqlalchemy
- (Caffe)Eclipse调试的Python接口(动态链接库_caffe.so)