python下elasticsearch简单接口操作
# -*- 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