• es + python


    简介:

    ​ es 是一个服务,可以理解为通过api进行交互

    ​ 可以查看:

    http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html 基础api介绍

    python 实践

    https://elasticsearch-py.readthedocs.io/en/v7.14.1/ # python es 文档

    es文档

    https://www.elastic.co/guide/cn/elasticsearch/guide/current/index-doc.html 详细文档

    增加:

    es结构:

    {
        "_index": "s2",
        "_type": "doc",
        "_id": "nQaN3XsBA-J7-iJLq7Va",
        "_score": 25.414799,
        "_source": {
        }
    }
    
    
    • _index 是索引, 可以理解为一个数据库
    • _type: 可以理解为表明
    • _id: 数据id 可以自己提供也可以i让其自动生成
    • _source: 数据

    例如:

    {
      "_index" :   "megacorp",  
      "_type" :    "employee",
      "_id" :      "1",
      "_version" : 1,
      "found" :    true,
      "_source" :  {
          "first_name" :  "John",
          "last_name" :   "Smith",
          "age" :         25,
          "about" :       "I love to go rock climbing",
          "interests":  [ "sports", "music" ]
      }
    }
    

    一个名为 megacorp ,存 employee 数据;

    这条数据的id 为 1;

    版本是 1, 数据修改过后,版本会变;

    具体存的数据是

    {
          "first_name" :  "John",
          "last_name" :   "Smith",
          "age" :         25,
          "about" :       "I love to go rock climbing",
          "interests":  [ "sports", "music" ]
      }
    

    https://www.cnblogs.com/mrzhao520/p/14120991.html

    python-elasticsearch基本用法

    一、安装

    pip install elasticsearch			
    pip install elasticsearch[async]	#支持异步
    

    二、实例化es对象,创建index

    from elasticsearch import Elasticsearch
    from elasticsearch import AsyncElasticsearch
    
    es = Elasticsearch(host="localhost", port=9200)
    #es = AsyncElasticsearch()
    
    body = {
      "settings": {
        "number_of_shards": 5
      },
      "mappings": {
          "properties":{
            "url":{
              "type": "text"
            },
            "key_zh": {
              "type": "text",
              "analyzer": "ik_max_word"
            },
            "key_ug": {
              "type": "text",
              "analyzer": "ik_max_word"  # 指定分词器,不同字段可以使用不同的分词器
              
            },
            "x_axis":{
              "type": "float"
            },
            "y_axis":{
              "type": "float"
            },
            "have_word":{
              "type": "float"
            }
          }
        }
    }
    #创建 index
    es.indices.create(index = "test", body = body)
    #删除 index
    es.indices.delete(index = 'test')
    

    三、增删改查

    #插入数据, 向索引 index 中,_doc 文件中插入 id 为1 内容为 {"id":1, "name":"小明"} 的数据,
    # index 可以不指明id 会自己创建
    es.index(index = "test", doc_type = "_doc", id = 1, body = {"id":1, "name":"小明"})
    #create 不可以不指明id
    es.create(index="test", doc_type = "_doc",id = 2, body = {"id":2, "name":"小红"})
    
    #删除指定数据
    es.delete(index='test', doc_type='_doc', id=1)
    
    #修改字段, 没更新一次 version 字段会变
    es.update(index = "test", doc_type = "_doc", id = 1, body = {"doc":{"name":"张三"}})
    
    #查询数据, 根据id 查询具体数据
    es.get(index = "test", doc_type = "_doc", id = 1)
    
    # query 是 搜索表达式,在 https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html 有介绍
    query = {
      "query" : { "match" : { "key_zh" : "很"}}
    }
    
    """
    query = {
      "query" : { 
      	"match" : { 
      		"key_zh" : {
          		"query": "开心",
          		"analyzer": "ik_max_word"  # 查询指明分词器
      				}
      			}
      	}
    }
    
    """
    es.search(index = "test", doc_type = "_doc", body = query)
    #滚动分页的func,第四块部分 分页查询中 说明
    es.scroll(scroll_id = "scroll_id", scroll = "5m")
    
    
    #批量插入&批量更新
    """
    {
    	'_index':'test',
    	'_type':'_doc',
    	'_id':20,
    	'doc':{'name':'李四', 'id':20},
    }
    	插入的每一个数据中,都需要指定 _index,_type,_id 更新、插入的数据放在doc中
    """
    from elasticsearch.helpers import async_bulk,bulk
    async def main():
        await async_bulk(es, data)
    bulk(es, data)
    

    四、es.search筛选数据的参数

    es.search(index = "test", doc_type = "_doc", body = body, size = 10)
    """
    index、doc_type、body
    size = 10 : 返回的数据量
    filter_path = [ 'hits.hits._id','hits.hits._type']: 用于指定响应的内容
    default_operator: 指定查询的运算符 AND或者OR 默认值为:OR
    from_ = 0 : 返回数据的起始值,用于分页
    scroll = "5m" : 是否记录滚动翻页的索引值, 记录5分钟
    """
    
    
    #返回所有数据
    body = {"query":{"match_all": {}}}
    #指定检索字段
    body = {"query":{"match": {"name":"小明"}}}
    #范围查询
    """
    gt:大于
    gte:大于等于
    lt:小于
    lte:小于等于
    """
    {"query":{"range":{"testTime":{"gte":"2020-12-01", "lte":"2020-12-31"}}}}
    
    
    
    #排序
    body = {
        "query":{...}, 
        "sort":[
            {
                "id(排序字段)":{
                    "order" : "desc"   #ascdesc  升序降序
                }
            }
        ]
    }
    
    
    
    #分页,从第10个数据开始,返回10条数据。   ==   [10:20]
    es.search(size = 10, from_ = 10)
    
    #使用滚动分页,速度快,查询后会记录最后一条数据,不适用跳页查询。
    #响应返回 _scroll_id字段、调用es.scroll方法返回下一页。
    es.search(scroll = "5m")
    es.scroll(scroll_id = "_scroll_id", scroll = "5m")
    
  • 相关阅读:
    YunTable开发日记(16)教程(0.9版RC)
    DevOps,不是一个传说!
    C/C++可变参数函数(转载) zjhfqq的专栏 52RD博客_52RD.com
    c++大写
    fabric install depernedecy
    mysql 在int ,bit类型中都 支持not 取反操作
    关于realloc的原理,与实现方法 C/C++ / C语言
    了解YunTable | 人云亦云
    如何让编译时的出错提示由中文变为英文的? 查看主题 • Ubuntu中文论坛
    Tomcat配置技巧Top 10
  • 原文地址:https://www.cnblogs.com/ShanCe/p/15271751.html
Copyright © 2020-2023  润新知