Python之Elasticsearch8.2.0

一、Elasticsearch

1、概念名词了解
  • 非常强大的搜索引擎,便于存储和检索,可以快速存储、搜索和分析海量数据,维基百科/Stack Overflow/GitHub都采用其实现
  • 一个分布式的实时文档存储库,每个字段都可以被索引与搜索
  • 一个分布式的实时分析搜索引擎
  • 能胜任上百个服务节点的扩展,并支持PB级别的结构化或者非结构化数据
  • 分布式数据库,允许多台服务器协同工作,每台服务器均可运行多个Elasticsearch实例
  • 节点Node:单个Elasticsearch实例
  • 集群Cluster:一组节点构成一个集群
  • 索引index:Elasticsearch会索引所有字段,经过处理后写入一个反向索引(inverted index),就相当于MongoDB/Mysql中的数据库概念,每个索引(数据库)的名字必须小写
  • 文档document:索引里的单条记录称为文档,许多条文档构成一个索引,一个索引里的文档结构可以不同,但是不建议这样做
  • 类型Types:文档可以分组,虚拟逻辑分组,用来过滤文档,类似MongoDB中的集合,MySQL中的数据表
  • 字段Fields:每个文档类似一个JSON结构,包含很多字段,每个字段都有值,多个字段组成一个文档
  • 总结:Elasticsearch: 索引index>类型Types>文档document>字段Fields
  • es8.x彻底删除了type!,es是面向文档的,es全部都是JSON,ELK是ElasticSearch, Logstash, Kibana三大开源框架首字母大写的简称
2、Elasticsearch安装

推荐参考安装文章

  • ElasticSearch是基于lucence开发的,也就是运行需要java jdk支持,所以要先安装JAVA环境,文档目录三JDK1.8安装 ,然后cmd窗口输入java -version如下展示代表java环境安装成功
    在这里插入图片描述

  • Elasticsearch下载地址,然后解压
    在这里插入图片描述

  • 然后进入config目录下,修改如下两个文件,elasticsearch.yml修改部分配置,jvm.options修改es内存大小(添加两行这个-Xms1g)
    在这里插入图片描述

  • elasticsearch.yml修改部分配置

    # elasticsearch.yml文件下修改如下
    cluster.name: mysy-es
    network.host: localhost
    http.port: 9200
    # 是否启用ssl,若不改为false则无法连接端口,http访问
    xpack.security.enabled: false
    
  • jvm.options修改es内存大小:添加两行这个-Xms1g
    在这里插入图片描述

  • 然后进入bin目录下,双击执行elasticsearch.bat,稍等片刻如下加载好,注意这个cmd窗口不要关,否则es不能连接成功,除非你已配置为服务自启
    在这里插入图片描述

  • 然后打开http://localhost:9200/,出现如下界面代表安装成功
    在这里插入图片描述

  • 设置ES_HOME环境变量
    在这里插入图片描述

3、ik分词插件安装
  • Elasticsearch检索功能,对于中文来说,需要安装一个分词插件elasticsearch-analysis-ik,注意安装版本与Elasticsearch版本一致

  • 到这里下载对应安装包https://github.com/medcl/elasticsearch-analysis-ik/releases,注意下载的版本和Elasticsearch一致
    在这里插入图片描述

  • 在Elasticsearch的plugins目录下,将刚刚下载的压缩包解压到该文件夹下,并重命名为ik(貌似也可以不用重命名)
    在这里插入图片描述
    在这里插入图片描述

  • 然后再次重启elasticsearch.bat,如图已加载
    在这里插入图片描述

4、kibana可视化安装
5、Windows配置ElasticSearch服务
  • 打开cmd窗口到bin目录下执行elasticsearch-service.bat install ,然后elasticsearch-service.bat start启动服务,我这里没试成功,所以就手动双击的bin目录下的elasticsearch.bat文件,参考文章
    在这里插入图片描述
  • elasticsearch-service.bat install: 安装Elasticsearch服务
  • elasticsearch-service.bat remove: 删除已安装的Elasticsearch服务(如果启动则停止服务)
  • elasticsearch-service.bat start: 启动Elasticsearch服务(如果已安装)
  • elasticsearch-service.bat stop: 停止服务(如果启动)
  • elasticsearch-service.bat manager:启动GUI来管理已安装的服务

二、python操作Elasticsearch

  • pip install elasticsearch
  • 创建一个es实例,更多参数说明参考文档
    from elasticsearch import Elasticsearch
    es = Elasticsearch(hosts=['http://localhost:9200/']).options(
        request_timeout=20,
        retry_on_timeout=True,
        ignore_status=[400, 404]
    )
    
  • 总的代码,其中ignore_status=[400, 404]里面的400代表,如果索引已存在,会返回400但是不会抛出报错导致接下来代码无法运行,就是忽略索引已存在重复创建的错误;404则是忽略因索引不存在而删除失败导致程序中断的问题
    from elasticsearch import Elasticsearch
    es = Elasticsearch(hosts=['http://localhost:9200/']).options(
        request_timeout=20,
        retry_on_timeout=True,
        ignore_status=[400, 404]
    )
    # 删除索引
    result = es.indices.delete(index="news")
    print(result)  # {'acknowledged': True}
    # 创建索引
    result = es.indices.create(index="news")
    print(result)  # {'acknowledged': True, 'shards_acknowledged': True, 'index': 'news'}
    # 插入数据
    result = es.create(index='news', id='1', document={
          
          "title": "你好周六"})   # 需指定id
    print(result)  # {'_index': 'news', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
    result = es.index(index='news', document={
          
          "title": "你好周日"})  # 自动生成id
    print(result)  # {'_index': 'news', '_id': 'zT_HwIABRhdG867DnYdw', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
    # 更新数据
    result = es.index(index='news', id='1', document={
          
          "title": "你好周日", "en": "hello zhou liu"})
    print(result)  # {'_index': 'news', '_id': '1', '_version': 2, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}
    # 删除数据
    result = es.delete(index='news', id='1')
    print(result) # {'_index': 'news', '_id': '1', '_version': 3, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1}
    
    
1、创建索引
  • 如创建索引news:es.indices.create(index="news"),然后打开http://127.0.0.1:9200/news也可看到数据
    from elasticsearch import Elasticsearch
    es = Elasticsearch(hosts=['http://localhost:9200/']).options(
        request_timeout=20,
        retry_on_timeout=True,
        ignore_status=[400, 404]
    )
    result = es.indices.create(index="news")
    print(result) # {'acknowledged': True, 'shards_acknowledged': True, 'index': 'news'}
    
    在这里插入图片描述
2、删除索引
  • 删除索引news:es.indices.delete(index="news")
    # 删除索引
    result = es.indices.delete(index="news")
    print(result)  # {'acknowledged': True}
    
3、新增数据
  • 插入数据有两种es.createes.index , create方法需要指定id,index自动生成id,返回结果部分内容:‘_version’: 1, ‘result’: ‘created’
    result = es.create(index='news', id='1', document={
          
          "title": "你好周六"})   # 需指定id
    print(result)  # {'_index': 'news', '_id': '1', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 0, '_primary_term': 1}
    result = es.index(index='news', document={
          
          "title": "你好周日"})  # 自动生成id
    print(result)  # {'_index': 'news', '_id': 'zT_HwIABRhdG867DnYdw', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
    
4、更新数据
  • 需指定更新的id,es.index既可以插入数据也可以更新数据,其中返回的结果里面有个_version字段,代表版本号每次更新都会加1,返回结果部分内容:‘_version’: 2, ‘result’: ‘updated’
    result = es.index(index='news', id='1', document={
          
          "title": "你好周日", "en": "hello zhou liu"})
    print(result)  # {'_index': 'news', '_id': '1', '_version': 2, 'result': 'updated', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}
    
5、删除数据
  • 需指定删除的id,es.delete删除数据,返回结果部分内容:‘_version’: 3, ‘result’: ‘deleted’,
    result = es.delete(index='news', id='1')
    print(result) # {'_index': 'news', '_id': '1', '_version': 3, 'result': 'deleted', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1}
    
6、查询数据
  • es.search查询数据 ,更多查询使用
    from elasticsearch import Elasticsearch
    es = Elasticsearch(hosts=['http://localhost:9200/']).options(
        request_timeout=20,
        retry_on_timeout=True,
        ignore_status=[400, 404]
    )
    properties = {
          
          
        "title": {
          
          'type': 'text'}
    }
    # es.indices.delete(index="news")
    result = es.indices.put_mapping(index='news', properties=properties)
    print(result)
    # 插入数据
    datas = [
        {
          
          'title': '美国留给伊拉克的是个烂摊子吗', 'url': 'http://view.news.qq.com/zt2011/usa_iraq/index.htm', 'date': '2011-12-16'},
        {
          
          'title': '公安部:各地校车将享最高路权', 'url': 'http://www.chinanews.com/gn/2011/12-16/3536077.shtml', 'date': '2011-12-16'},
        {
          
          'title': '中韩渔警冲突调查:韩警平均每天扣1艘中国渔船', 'url': 'https://news.qq.com/a/20111216/001044.htm', 'date': '2011-12-17'},
        {
          
          'title': '中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首', 'url': 'http://news.ifeng.com/world/detail_2011_12/16/11372558_0.shtml', 'date': '2011-12-18'}
    ]
    for data in datas:
        es.index(index='news', document=data)
    # 查询1
    result = es.search(index='news')
    print(result)
    # 查询2
    query = {
          
          
        'match': {
          
          
            'title': '平均'
        }
    }
    result = es.search(index='news', query=query)
    print(result)
    
    

猜你喜欢

转载自blog.csdn.net/weixin_43411585/article/details/124763838