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. Unity3d copy gameobject from one scene to another

    scene-copy-game-objects-from-one-scene-to-anotherhttp://forum.unity3d.com/threads/scene-copy-game-ob ...

  2. checkbox点击后出现div

    HTML: <div class="msg_editUrl_function"> <label class="labelChecked" fo ...

  3. VMware中linux配置1-安装VMware tool 共享文件夹

    linux:ubuntu 14 安装Linux,使用的ubuntu-14.04.1-desktop-amd64.iso 安装的,这个就不写了. 为了在linux中访问windows的目录,需要安装VM ...

  4. archlinux 安装手记

    Wiki常用软件 https://wiki.archlinux.org/index.php/Common_Applications -> 移动硬盘等的自动挂载 pacman -S gvfs-af ...

  5. zend create project prepare

    1.php ini 安装pear 设置include_path 2.apache AllowOverride LoadModule rerwite去掉注释 <VirtualHost *:> ...

  6. 很重要的vue的生命周期

  7. Swift 05.Block

    Swift的函数用法还真是灵活.但是个人感觉更灵活的还是闭包. swift闭包的概念大抵相当于OC的block的概念.如果对于block的理解很透彻的话,闭包的原理性的东西还是很好理解的. 剩下的就是 ...

  8. KETTLE 配置资源库

    KETTLE 是一款开源的ETL工具,通过图形界面进行设计,可以对数据进行转换.设计好的文件分为两类,一类是trans,一类是job,这些文件可以存储到文件系统中.   也可以存储到数据库中.   如 ...

  9. scala getter and setter

    package exp { object Main { def main(args: Array[String]): Unit = { B.name ="Fred"; printl ...

  10. PHP数学函数

    Abs: 取得绝对值. Acos: 取得反余弦值. Asin: 取得反正弦值. Atan: 取得反正切值. Atan2: 计算二数的反正切值. base_convert: 转换数字的进位方式. Bin ...