栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/687632.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号