当前位置:首页 > 日记本 > 正文内容

python下elasticsearch简单接口操作

zhangchap3年前 (2021-09-09)日记本361
# -*- coding:utf-8 -*-
# elasticsearch 默认算法bm25
from elasticsearch import Elasticsearch
es = Elasticsearch(hosts=['http://127.0.0.1:9200'])
info = es.info(pretty=True,human=True)
mappings = {
   "mappings":{
      "properties":{
         "title":{
            "type":"text",
            "analyzer":"ik_smart"
         },
         "content":{
            "type":"text",
            "analyzer":"ik_smart"
         }

      }
   }
}
# 创建索引
# ret = es.indices.create('test-demo',request_timeout=20,ignore=[400],body=mappings)
# 删除索引
# ret = es.indices.delete("test-demo")
# 查看 mappings 信息
# ret = es.indices.get_mapping(index='test-demo')
# 添加文档
doc = {
   "title":"python测试文档2",
   "content":"python测试内容"
}
# index 添加文档,如果文档已经存在,就回更新文档,如果不存在就回创建,id = 1是指定ID,不指定则为默认
# ret = es.index(index='test-demo',body=doc,id = 1)
ret = es.index(index='test-demo',body=doc)

# create 添加文档,如果文档已经存在,就报错,如果不存在就回创建
# ret = es.create(index='test-demo',body=doc)


# 查看搜索索引里面的内容:http://127.0.0.1:9200/{索引名称}/_search
# http://127.0.0.1:9200/test-demo/_search

#搜索索引内容
# 搜索title:http://127.0.0.1:9200/test-demo/_search?q=title:python
# 搜索全部字段:http://127.0.0.1:9200/test-demo/_search?q=python

# 查看索引是否存在
ret = es.indices.exists(index='test-demo')
print(ret)

默认端口地址

http://127.0.0.1:9200/

查看索引库信息

http://127.0.0.1:9200/_cat/indices?v


标签: elasticsearch
分享给朋友:

相关文章

elasticsearch相关概念

集群:有多台服务器,每台服务器都运行这ESindex:索引,相当于数据库index_type:"_doc" # 数据表document:数据信息mapping映射字段类型:bina...

elasticsearch 127.0.0.1:9200 连接被重置

之前安装过7.0的版本没有问题,现在用最新版的8.3.2的版本,启动后访问 9200端口提示连接被重置一下是解决方案:config目录中的elasticsearch.yml将xpack.securit...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。