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

elasticsearch,python包pyes进行的处理的更多相关文章

  1. 离线pip下载Python包

    离线pip下载Python包   这几天搞Windows离线断网环境下安装Python包,配置环境,各种坑!做个记录,供以后查询吧.      # 生产环境  windows xp# python 2 ...

  2. 【转】linux和windows下安装python集成开发环境及其python包

    本系列分为两篇: 1.[转]windows和linux中搭建python集成开发环境IDE 2.[转]linux和windows下安装python集成开发环境及其python包 3.windows和l ...

  3. RobotFramework中加载自定义python包中的library(一个py文件中有多个类)

    结构如下: appsdk\ appsdk.py(这里面有多个类,包括appsdk,appsdksync等类) __init__.py ... ① 有个appsdk的文件夹(符合python包的定义) ...

  4. centos 7 下安装numpy、scipy等python包

    本文适用于刚入门的小白,欢迎大牛们批评指正. 因为要开始数据分析,而python又不像R和matlab那么简洁.需要安装的包很多~ 网上找了好多牛人博客,想在centos7下安装numpy,scipy ...

  5. python 包管理工具

    python 包管理工具 Python当前的包管理工具链是 easy_install/pip + distribute/setuptools + distutils,显得较为混乱. 而将来的工具链组合 ...

  6. Python 包管理工具解惑

    Python 包管理工具解惑 本文链接:http://zengrong.net/post/2169.htm python packaging 一.困惑 作为一个 Python 初学者,我在包管理上感到 ...

  7. Python 包的相对导入讲解

    [Python 包的相对导入讲解] 参考:http://www.dai3.com/python-import.html

  8. 转: CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法

    from: http://www.linuxde.net/2014/05/15576.html CentOS 6.4安装pip,CentOS安装python包管理安装工具pip的方法 2014/05/ ...

  9. 纯Python包发布setup脚本编写示例

    如果你有多个模块需要发布,而它们又存在于多个包中,那么指定整个包比指定模块可能要容易地多.即使你的模块并不在一个包内,这种做法也行的通:你可以告诉Distutils从根包(root package)处 ...

随机推荐

  1. 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)

    一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...

  2. ArcGIS发布服务时缓存切片设置

    [文件]>[共享]>[服务]>[覆盖原有服务]或[创建新服务] 设置好相关参数后,会弹出"服务编辑框": 进入"缓存" 1."绘制此 ...

  3. 11.10 Taolu1234组信息汇总

    团队名称: Taolu1234 团队选题: <餐厅到店点餐系统>WEB版 团队博客地址: http://www.cnblogs.com/queenjuan/ 团队GITHUB地址: htt ...

  4. python 基本语法

    第一个python程序 打开Sublime Text -->输出 print"Hello World" -->保存为frist.py -->打开命令行运行,运行p ...

  5. 3d旋转

    <!DOCTYPE html><html lang="zh-cmn-Hans"><head><meta charset="utf ...

  6. Minimum Depth of Binary Tree [LeetCode]

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  7. Swift的 convenience && designated init

    http://www.th7.cn/Program/IOS/201603/789227.shtml 在 OC 中 init 方法是非常不安全的,没人能够保证 init 只被调用一次,也没有人保证在初始 ...

  8. Python 函数嵌套

    def mumber(a):      def add(b):              return a*b      return add if __name__=="__main__& ...

  9. JavaScript的面向对象编程(OOP)(一)——类

    在学习JavaScript面向对象的编程之前,需要知道,并了解面向对象的一些基本的常识.初学者中大多数都以为面向对象中,面向对象的编程是很重要和占据很大一部分精力.笔者在之前也是认为OOP是面向对象的 ...

  10. Windows Store App JavaScript 开发:获取文件和文件夹列表

    在应用程序中有时可能需要获取用户库中的内容,以便执行相关的操作.如果要获取某个用户库中的内容,需要先获取到这个用户库,获得用户库可以通过Windows.Storage命名空间中的KnownFolder ...