python控制elasticsearch搜索模块使用方法

python控制elasticsearch搜索模块使用方法,第1张

python控制elasticsearch搜索模块使用方法
# -*-coding: utf-8 -*-
from elasticsearch import Elasticsearch

es = Elasticsearch('http://127.0.0.1:9200/')
#查看es信息
# info = es.info(pretty=True)
# print(info)
#创建索引 相当于数据库,需要先配置好mappings,在python中相当于字典
#1.配置mappings
mappings = {
    "mappings":{
        'properties':{
            'title':{
               'type':'text',
                'analyzer': 'ik_smart',#安装的中文分词
            },
            'content':{
                'type':'text',
                'analyzer': 'ik_smart',
            }
        }
    }
}
#创建索引也就是数据库
ret = es.indices.create('test-demo1',body = mappings,request_timeout=50,ignore=[400])#ignore可以忽略400 等错误
print(ret)
#删除索引
es.indices.delete(index='test-demo1')

#查看mappings信息
ret = es.indices.get_mapping(index='test-demo')
print(ret)

#添加文档 如果文档已经存在就会更新文档,不存在就创建
doc = {
    'title':'seo测试文档',
    'content': 'seo测试内容',
}
ret = es.index(index='test-demo',document=doc,id=2)
print(ret)

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5688148.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存