python操作

一、连接elasticsearch

   // 下载es第三方库
   pip install elasticsearch
   // 导包
   from elasticsearch import Elasticsearch
   // 连接
   es = Elasticsearch(['127.0.0.1:9200'])
   // 创建索引
   es.indices.create(index='test_index1', ignore=400)
   """
   官方文档:https://elasticsearch-py.readthedocs.io/
   """

二、基本操作

   // 删除索引
   es.indices.delete(index='test_index1')
   // 插入数据
   es.index(index='test_index1', doc_type='doc', id=1, body={"name": "kitty", "age": 18})
   """
   注意:若不指定id,则id为20为随机码
   """

三、基本查询

   // 按id查询
   es.get(index='test_index1', doc_type='doc', id=1)
   // 全查(此时不允许出现id!)
   body = {
       "query": {
           "match_all": {}
       }
   }
   es.search(index='test_index1', doc_type='doc', body=body)
   // 按条件查询
   body = {
       "query": {
           "bool": {
               "must": [
                   "match": {
                       "name": "zqx"
                   }
               ],
               "filter": {
                   "range": {
                       "age": {"gte": 18  }
                   }
               }
           }
       }
   }
   es.search(index='goods', doc_type='fruit', body=body)
   // 按条件删除
   1)es.delete(index='test_index1', doc_type='doc', id=1)
   2)body = {
         "query": {
              "match": {
                  "name": "zqx"
              }
         }
      }
      es.delete_by_query(index='test_index1', doc_type='doc', body=body)

四、创建mapping

   body = {
       "mappings": {
           "doc": {
               "properties": {
                   "name": {
                       "type": "text",
                  },
                   "age": {
                       "type": "long"
                   }
               }
           }
       }
   }
   es.indices.create(idnex='shang', body=body)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • python操作Redis 一. Redis是什么 Redis 是一个开源(BSD许可)的,内存中的数据结构存储系...
    shu_ke阅读 3,906评论 0 9
  • Python操作MySQL 一. python操作数据库介绍 Python 标准数据库接口为 Python DB-...
    shu_ke阅读 620评论 0 3
  • 使用简单的纯文本方式只能实现有限的功能,不能进行快速查询,只有把数据全部读到内存中才能自己遍历。不过在实际应用中,...
    泷汰泱阅读 161评论 0 0
  • 今天数学课我们学习了20-几。语文,我们课上写了一张卷子。下午社团课,老师让我们用纸杯,彩笔。胶棒,还有剪刀。...
    刘家成同学阅读 386评论 0 0
  • 藤原定家《拾遺愚草》“風のうへに星のひかりは冴えながらわざともふらぬ霰をぞ聞く” 风上的星辰明耀,飘着无人在意的霜...
    穆耳忒亚阅读 201评论 0 0