zoukankan      html  css  js  c++  java
  • pyes-elasticsearch的python客户端使用笔记

    elasticsearch入门: 

    http://www.qwolf.com/?p=1387

    一.重要的概念

    http://834945712.iteye.com/blog/1915432 这篇文章很多类比做的很好,便于快速理解pyes的结构

    http://blog.plotcup.com/a/106   很清晰的示例代码

    1. 使用pip install pyes 或者 easy_install pyes安装pye
    2. 测试使用pyes官方文档或者其他pyes文档的API的增删改

     1 import pyes
     2 conn = pyes.ES('127.0.0.1:9200')
     3 conn.create_index("human") #human 是一个新的索引库,相当于create database操作
     4 mapping = {u'firstname': {'index': 'analyzed', #使用分词器
     5                          'type': u'string',
     6                          'analyzer':'ik'}, #分词器为ik
     7            u'lastname': {'index': 'not_analyzed',
     8                         'type': u'string'},
     9            u'age': {'index': 'not_analyzed', #不使用分词器
    10                    'type': u'long'}} #mapping 是字段,相当于数据库的表的列名
    11 conn.put_mapping("man", {'properties':mapping}, ["human"]) #在human库中创建man,相当于create table操作
    12 conn.put_mapping("woman", {'properties':mapping}, ["human"]) #woman同样相当于一张表
    13 conn.index({'firstname':'David', 'lastname':'White', 'age':18}, 'human', 'man', 'David White', True) #向human的man中添加索引数据,相当于insert into操作
    14 conn.index({'firstname':'Suzan', 'lastname':'Black', 'age':28}, 'human', 'woman', 'Suzan Black', True) #向human的woman中添加索引数据
    15 conn.index({'firstname':'Uni', 'lastname':'Lavender', 'age':18}, 'human', 'man', 'Uni Lavender', True)
    16 conn.index({'firstname':'Jann', 'lastname':'White', 'age':18}, 'human', 'woman', 'Jann White', True)
    17 conn.index({'firstname':'Suzan', 'lastname':'White', 'age':18}, 'human', 'woman', 'Suzan White', True) #注意第四个参数是index的id,具有唯一性,因此更新数据,可以按照id使用index即可
    18 conn.index({'firstname':'Jann', 'lastname':'White', 'age':28}, 'human', 'woman', 'Jann White', True) #将年龄由18更新到28

    3. 测试使用pyes官方文档的API的查询
    使用res = conn.search(pyes.BoolQuery(must=must), 'human', 'woman', start=0, size=10, sort='age')查询,支持分页
    a. 查找firstname为Suzan的女人的index数据
    条件:must = pyes.StringQuery('Suzan', ['firstname',]) #must = pyes.StringQuery('Suzan', 'firstname')
    相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
    b. 查找lastname为white的女人的index数据
    条件:must = pyes.StringQuery('White', ['lastname',]) #must = pyes.StringQuery('White', ['lastname',])或者must = pyes.TermQuery('lastname', 'White')
    相当于sql查询 select * from human.woman where lastname = 'White'
    c. 查找age为18,20,28的女人的index数据
    条件:must = pyes.TermsQuery('age', [18,28])
    相当于sql查询 select * from human.woman where age=18 or age = 28
    d. 查找age为18,28并且firstname为Suzan的女人的index数据
    条件:must = [pyes.TermsQuery('age', [18,28]), pyes.StringQuery('Suzan', 'firstname')]
    相当于sql查询 select * from human.woman where (age=18 or age = 28) and firstname = 'Suzan'
    e. 查找firstname或者lastname中出现Rich单词的女人的index数据
    条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='or')
    相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' or lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
    f. 查找firstname并且lastname中出现Rich单词的女人的index数据
    条件:must = pyes.StringQuery('Suzan', ['firstname',‘lastname’], default_operator='and')
    相当于sql查询 select * from human.woman where firstname regexp '[^a-zA-Z]Suzan[^a-zA-Z]' and lastname regexp '[^a-zA-Z]Suzan[^a-zA-Z]'
    g. 查找年龄在18到28之间的女人的index数据
    条件:must = pyes.RangeQuery(pyes.ESRange('age', from_value=18, to_value=28))
    相当于sql查询 select * from human.woman where age between 18 and 28]
    h. 查找以Whi开头的lastname的女人的index数据
    条件:must = pyes.PrefixQuery('lastname', 'Whi')
    相当于sql查询 select * from human.woman where lastname like 'Whi%'

    文章末尾:

    这个很好用还是

    搜索小知识:可以借助百度实现站内全文搜索,将http://www.baidu.com/s?wd=网络中心&pn=10&ct=2097152&ie=utf-8&si=www.stcsm.gov.cn&format=json(绿色部分替换成关键词,红色部分替换站内地址)

    二.好用的工具

    linux下的es命令行工具,es2unix

    这个工具挺好用,不过使用时候遇到一些问题,

    1.es2unix  find or load main class

    2./bin/sh: 1: lein: not found

    了解到一个好不从的工具,Leiningen教程中文版

    3.Clojure

    Clojure(发音类似"closure")[2]是一套现代的Lisp语言的动态语言版。它是一个函数式多用途的语言。

    http://zh.wikipedia.org/zh-cn/Clojure

  • 相关阅读:
    基于集合成工控机Ubuntu系统安装分区详解
    MySql连接问题
    再记一个提供webServices的网址
    J​a​y​r​o​c​k​.​J​s​o​n​读​取​j​s​o​n​数​据​(​n​e​t​)
    jquery mobile页面跳转后,必须重新刷新页面js方可有效
    删除数据库失败 因为当前数据库正在使用
    如何解决自定义404页面在IE等浏览器中无法显示问题
    HTTP协议详解(真的很经典)
    如何在XAMPP中设置多个网站
    apache php 多站点配置 重新整理
  • 原文地址:https://www.cnblogs.com/Tigerlee/p/3611995.html
Copyright © 2011-2022 走看看