• Elastic基本操作


    导包

    from elasticsearch import Elasticsearch
    
    • 1

    连接ES

    es = Elasticsearch(["192.168.61.129:9200"])
    
    • 1

    创建index

    body = {
      "settings": {
        "index": {
          # 切片数量
          "number_of_shards": 3,  
          # 备份数量+
          "number_of_replicas": 2 
        }
      },
        
      "mappings": {
        # 指定类型Type
        "novel": {
          "properties": {
            # 名称
            "name": {
              "type": "text",
              "analyzer": "ik_max_word",
              "index": True,
              "store": False
            },
            # 作者名称
            "author": {
              "type": "keyword"
            },
            # 字数
            "count": {
              "type": "long"
            },
            # 上架时间
            "onsale": {
              "type": "date",
              "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            },
            # 描述
            "desc": {
              "type": "text",
              "analyzer": "ik_max_word"
            }
          }
        }
      }
    }
    
    
    # 这里也是使用了 ignore 参数,来忽略 Index 不存在而删除失败导致程序中断的问题。
    result = es.indices.create(index='test-index', ignore=400, body=body)
    result
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    {'acknowledged': True, 'shards_acknowledged': True, 'index': 'test-index'}
    
    • 1

    返回有关集群中节点的信息

    es.info()
    
    • 1
    {'name': 'AvPhieW',
     'cluster_name': 'docker-cluster',
     'cluster_uuid': 'P-HHpDWzTZGvuSqNMCnr4A',
     'version': {'number': '6.5.4',
      'build_flavor': 'default',
      'build_type': 'tar',
      'build_hash': 'd2ef93d',
      'build_date': '2018-12-17T21:17:40.758843Z',
      'build_snapshot': False,
      'lucene_version': '7.5.0',
      'minimum_wire_compatibility_version': '5.6.0',
      'minimum_index_compatibility_version': '5.0.0'},
     'tagline': 'You Know, for Search'}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    插入数据

    data = [
    {
    ‘name’: ‘人间草木’,
    ‘author’: ‘汪曾祺’,
    ‘count’: 100000,
    ‘onsale’: ‘2017-04-01’,
    ‘desc’: ‘《人间草木》是汪曾祺先生的文集,他写出了草木山川、花鸟虫鱼的人味,写出了乡情民俗、凡人小事温润的乡土味;以一颗从容豁达的心写出了世间的美好与灵动。他的笔尖下总是有着一连串的惊喜:清晨薄雾里带着露珠的洁白的缅桂花,明亮、丰满而使人丰满的昆明的雨,饱涨着花骨朵的木香,自得其乐的栀子花,巷子里卖杨梅的苗族女子柔柔的声音,联大那些令人难以忘却的师友,抑或是没有大喜大忧、没有烦恼、无欲望亦无追求、天然恬淡、抱膝闲看“活庄子”般的闹市闲民。汪曾祺先生一生都对生活投入真情,那水洗般的文字有种洗涤红尘世俗的力量,赋予了作品纯真的生命力’
    },

    {
        'name': '慢煮生活',  
        'author': '汪曾祺',
        'count': 200000,
        'onsale': '2019-09-01',
        'desc': '本书为汪曾祺的散文精选集,完整收录《五味》《昆明的雨》《人间草木》《星斗其文,赤子其人》等经典名篇,同时新增《猫》《一技》《名优逸事》《和尚》《一辈古人》等罕见篇目。作者从花鸟虫鱼、乡情民俗、凡人小事、旅途见闻等多个主题出发,详尽展现了一代“生活家”汪曾祺的精神世界与生活志趣你很辛苦,很累了,那么坐下来歇一会儿,喝一杯不凉不烫的清茶,读一点我的作品。'
    },
    
    {
        'name': '心安即是归处',
        'author': '季羡林',
        'count': 500000,
        'onsale': '2020-08-01',
        'desc': '季羡林经历过人生的大苦大悲,生命的跌宕起伏。然而先生的一生,不争不辩,不怨不艾,满怀天真,执着自己的执着,安于当下。是世界上仅有的精通于吐火罗文的几位学者之一,同时又用质朴的文字向世人传达一个理念——心安即是生命的归处。本书旨在阐释先生的生命智慧,从谈人生的意义到分别谈读书、处世、行走、当下、孤独、生死等跟大家密切相关的生命话题。高山仰止,景行行止。愿我们能了悟人间万相的本真,拥有应对世事的智慧。万事安然于心,从容而行。'
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    ]for index, datum in enumerate(data):
    es.create(index=‘test-index’, doc_type=‘novel’, id=index, body=datum)

    create方法插入数据

    data = {
      "name": "人间草木-create",
      "author": "汪曾祺",
      "count": 10000,
      "onsale": "2000-01-01",
      "desc": "《人间草木》是汪曾祺先生的文集。"
    }
    
    es.create(index='test-index', doc_type='novel', id=1, body=data)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    {'_index': 'test-index',
     '_type': 'novel',
     '_id': '1',
     '_version': 1,
     'result': 'created',
     '_shards': {'total': 3, 'successful': 1, 'failed': 0},
     '_seq_no': 0,
     '_primary_term': 1}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    index方法插入数据

    data = {
      "name": "人间草木-index",
      "author": "汪曾祺",
      "count": 10000,
      "onsale": "2000-01-01",
      "desc": "《人间草木》是汪曾祺先生的文集。"
    }
    
    es.index(index='test-index', doc_type='novel', body=data)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    {'_index': 'test-index',
     '_type': 'novel',
     '_id': 'nYNtankBRd9cGn4B55kp',
     '_version': 1,
     'result': 'created',
     '_shards': {'total': 3, 'successful': 1, 'failed': 0},
     '_seq_no': 1,
     '_primary_term': 1}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    查询数据

    dsl = {
        'query': {
            'match': {
                'desc': '人间草木'
            }
        }
    }
    
    es.search(index='test-index', doc_type='novel', body=dsl)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    {'took': 11,
     'timed_out': False,
     '_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0},
     'hits': {'total': 2,
      'max_score': 0.36464313,
      'hits': [{'_index': 'test-index',
        '_type': 'novel',
        '_id': '1',
        '_score': 0.36464313,
        '_source': {'name': '人间草木-create',
         'author': '汪曾祺',
         'count': 10000,
         'onsale': '2000-01-01',
         'desc': '《人间草木》是汪曾祺先生的文集。'}},
       {'_index': 'test-index',
        '_type': 'novel',
        '_id': 'nYNtankBRd9cGn4B55kp',
        '_score': 0.36464313,
        '_source': {'name': '人间草木-index',
         'author': '汪曾祺',
         'count': 10000,
         'onsale': '2000-01-01',
         'desc': '《人间草木》是汪曾祺先生的文集。'}}]}}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    查询所有的

    es.search(index='test-index', doc_type='novel')
    
    • 1
    {'took': 1,
     'timed_out': False,
     '_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0},
     'hits': {'total': 2,
      'max_score': 1.0,
      'hits': [{'_index': 'test-index',
        '_type': 'novel',
        '_id': '1',
        '_score': 1.0,
        '_source': {'name': '人间草木-create',
         'author': '汪曾祺',
         'count': 10000,
         'onsale': '2000-01-01',
         'desc': '《人间草木》是汪曾祺先生的文集。'}},
       {'_index': 'test-index',
        '_type': 'novel',
        '_id': 'nYNtankBRd9cGn4B55kp',
        '_score': 1.0,
        '_source': {'name': '人间草木-index',
         'author': '汪曾祺',
         'count': 10000,
         'onsale': '2000-01-01',
         'desc': '《人间草木》是汪曾祺先生的文集。'}}]}}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    查询匹配的文档数

    dsl = {
        'query': {
            'match': {
                'desc': '人间草木'
            }
        }
    }
    
    es.count(index='test-index', doc_type='novel', body=dsl)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    {'count': 2,
     '_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0}}
    
    • 1
    • 2

    查询所有的文档数

    es.count(index='test-index', doc_type='novel')
    
    • 1
    {'count': 2,
     '_shards': {'total': 3, 'successful': 3, 'skipped': 0, 'failed': 0}}
    
    • 1
    • 2

    是否存在文档

    es.exists(index='test-index', doc_type='novel', id=1)
    
    • 1
    True
    
    • 1

    更新数据

    update更新

    body = {
        'doc': {
            'name': '人间草木-create-新版'
        }
    }
    
    es.update(index='test-index', doc_type='novel', id=1, body=body)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    {'_index': 'test-index',
     '_type': 'novel',
     '_id': '1',
     '_version': 2,
     'result': 'updated',
     '_shards': {'total': 3, 'successful': 1, 'failed': 0},
     '_seq_no': 2,
     '_primary_term': 1}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    index方法更新

    data = {
      "name": "人间草木-index-新版",
      "author": "汪曾祺",
      "count": 10000,
      "onsale": "2020-01-01",
      "desc": "《人间草木》是汪曾祺先生的文集。"
    }
    
    # 如果要更新的文档不存在,则为新增数据
    es.index(index='test-index', doc_type='novel', id='nYNtankBRd9cGn4B55kp', body=data)
    
    
    # es.index(index='test-index', doc_type='novel', body=data)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    {'_index': 'test-index',
     '_type': 'novel',
     '_id': 'nYNtankBRd9cGn4B55kp',
     '_version': 2,
     'result': 'updated',
     '_shards': {'total': 3, 'successful': 1, 'failed': 0},
     '_seq_no': 3,
     '_primary_term': 1}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    删除数据

    es.delete(index='test-index', doc_type='novel', id=1)
    
    • 1

    删除index

    # 这里也是使用了 ignore 参数,来忽略 Index 不存在而删除失败导致程序中断的问题。
    result = es.indices.delete(index='test-index', ignore=[400, 404])
    result
    
    • 1
    • 2
    • 3
    {'acknowledged': True}
    
    • 1
  • 相关阅读:
    JVM调优案例分析(4)
    电脑蓝屏怎么办 七大原因及解决办法来帮你
    【实现100个unity特效之12】Unity中的冲击波 ——如何使用ShaderGraph制作一个冲击波着色器
    广西建筑模板的材质类型和特点有哪些?
    java计算机毕业设计Internet快递柜管理系统MyBatis+系统+LW文档+源码+调试部署
    python之value_counts()介绍
    leetcode:45. 跳跃游戏II
    ChatGpt大模型入门
    【开发篇】二、属性绑定与校验
    【全面速懂】C#使用WSDL服务
  • 原文地址:https://blog.csdn.net/weixin_42856871/article/details/134024528