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操作)