python环境下ElasticSearch使用

本文详细介绍了如何在Elasticsearch中进行环境配置、安装依赖、创建客户端,展示了索引操作如创建、查看、修改、删除以及搜索的实战步骤,包括使用get_alias、cat_indices和各种查询方法。
摘要由CSDN通过智能技术生成

环境配置

安装依赖包

pip install elasticsearch

使用

创建客户端

from ssl import create_default_context
from elasticsearch import Elasticsearch
from typing import Optional, Dict, Any
from elastic_transport import NodeConfig



# context = create_default_context(cafile="XXX.pem")
context = create_default_context(cafile="http_ca.crt")
context.check_hostname = False
es= Elasticsearch(['https://192.168.122.8:9200'],
                  basic_auth=('elastic', '19vZ*=B5UhWgF_BQeU-2'),
                  ssl_context=context
                 )




服务器信息查看

es.info().raw
{'name': 'node-1',
 'cluster_name': 'elasticsearch',
 'cluster_uuid': 'err8U3ufRS-EiKUqWTh4wQ',
 'version': {'number': '8.0.0',
  'build_flavor': 'default',
  'build_type': 'tar',
  'build_hash': '1b6a7ece17463df5ff54a3e1302d825889aa1161',
  'build_date': '2022-02-03T16:47:57.507843096Z',
  'build_snapshot': False,
  'lucene_version': '9.0.0',
  'minimum_wire_compatibility_version': '7.17.0',
  'minimum_index_compatibility_version': '7.0.0'},
 'tagline': 'You Know, for Search'}

查询所有索引

直接查询(会有警告信息)

indices=es.indices.get_alias().keys()
print(indices)
dict_keys(['.apm-agent-configuration', 'blog', 'bolg', 'bolg1', '.transform-notifications-000002', '.fleet-policies-7', '.kibana_task_manager_8.0.0_001', '.async-search', '.fleet-enrollment-api-keys-7', '.kibana-event-log-8.0.0-000001', '.tasks', '.kibana_8.0.0_001', '.metrics-endpoint.metadata_united_default', 'metrics-endpoint.metadata_current_default', '.security-7', '.apm-custom-link', '.kibana_security_session_1', '.transform-internal-007', '.lists-default-000001', '.items-default-000001'])


C:\Users\24404\AppData\Local\Temp/ipykernel_2940/2155219832.py:1: ElasticsearchWarning: this request accesses system indices: [.apm-agent-configuration, .fleet-policies-7, .kibana_task_manager_8.0.0_001, .async-search, .fleet-enrollment-api-keys-7, .tasks, .kibana_8.0.0_001, .security-7, .apm-custom-link, .kibana_security_session_1, .transform-internal-007], but in a future major version, direct access to system indices will be prevented by default
  indices=es.indices.get_alias().keys()

利用cat查询

print(es.cat.indices().raw) 
yellow open bolg1                                     VKEDY50WTO-Ab0NhWj8W8w 1 1 0 0   225b   225b
green  open metrics-endpoint.metadata_current_default JQ3DZ31kRp6fWxuIgQtTyg 1 0 0 0   225b   225b
yellow open .lists-default-000001                     31iZgKfASnW4aGYgSBW7mA 1 1 0 0   225b   225b
yellow open bolg                                      z84ToWTZTM-J80G1tKshLA 1 1 0 0   225b   225b
yellow open .items-default-000001                     INOvz6zaThSsDZ9NS0NZ5Q 1 1 0 0   225b   225b
yellow open blog                                      5ovQ7NOUSOiqbzNHVrEAsg 5 1 6 0 24.4kb 24.4kb

创建索引

方法1

#index创建索引
mappings={
    'properties':{
            "id":{
                "type":"long",
                "store":True
            },
            "title":{
                "type":"text",
                "store":True,
                "index":True,
                "analyzer":"ik_smart"
            },
            "content":{
                "type":"text",
                "store":True,
                "index":True,
                "analyzer":"ik_smart"
            }
    }
}
settings = {
    "index": {
        "number_of_shards": "5",
        "number_of_replicas": "1"
    }
}
res = es.indices.create(index='post-index',mappings=mappings,settings=settings)

print(res)
{'acknowledged': True, 'shards_acknowledged': True, 'index': 'post-index'}

索引信息查询

res=es.indices.get(index='post')
res.raw
{'post': {'aliases': {},
  'mappings': {'properties': {'content': {'type': 'text',
     'store': True,
     'analyzer': 'ik_smart'},
    'id': {'type': 'long', 'store': True},
    'title': {'type': 'text', 'store': True, 'analyzer': 'ik_smart'}}},
  'settings': {'index': {'routing': {'allocation': {'include': {'_tier_preference': 'data_content'}}},
    'number_of_shards': '5',
    'provided_name': 'post',
    'creation_date': '1646537410793',
    'number_of_replicas': '1',
    'uuid': 'XZH-1gtSSo2m_ftsm5JOfQ',
    'version': {'created': '8000099'}}}}}

为索引设置mapping

es.indices.put_mapping(index='post',properties={
    "id":{
            "type":"long",
            "store":True
        },
        "title":{
            "type":"text",
            "store":True,
            "index":True,
            "analyzer":"ik_smart"
        },
        "content":{
            "type":"text",
            "store":True,
            "index":True,
            "analyzer":"ik_smart"
        }
})
ObjectApiResponse({'acknowledged': True})

查询索引的mapping

es.indices.get_mapping(index='post').raw
{'post': {'mappings': {'properties': {'content': {'type': 'text',
     'store': True,
     'analyzer': 'ik_smart'},
    'id': {'type': 'long', 'store': True},
    'title': {'type': 'text', 'store': True, 'analyzer': 'ik_smart'}}}}}

删除索引

res = es.indices.delete(index='post')
res.raw
{'acknowledged': True}

判断索引是否存在

#索引是否存在
print(es.indices.exists(index="post"))
True

创建/修改文档


post = {
    'id': 1,
    'title': '这是一个title1',
    'content': '这是一个content',
}
resp = es.index(index="post-index", id=1, document=post)
print(resp.raw)
{'_index': 'post-index', '_id': '1', '_version': 6, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 5, '_primary_term': 1}

查询文档

根据文档id查询

resp = es.get(index="post-index", id=1)
resp.raw
{'_index': 'post-index',
 '_id': '1',
 '_version': 6,
 '_seq_no': 5,
 '_primary_term': 1,
 'found': True,
 '_source': {'id': 1, 'title': '这是一个title1', 'content': '这是一个content'}}

查询所有

res = es.search(index="blog", 
                query={"match_all": {}},)
res.raw

分页查询所有

res = es.search(index="blog", 
                query={"match_all": {}},
                size=3,
                from_=2
               )
res.raw

match 匹配

res = es.search(index="blog", 
                query={
                    "match": {
                        "title": "在"
                    }
                }
               )
res.raw

term 关键字查询

res = es.search(index="blog", 
                query={
                    "term": {
                        "content": "在"
                    }
                }
               )
res.raw

querystring查询

res = es.search(index="blog", 
                query={
                    "query_string": {
                        "default_field": "content", 
                        "query": "为人民服务"
                    }
                }
               )
res.raw

删除文档

res = es.delete(index='post-index',id=1)
res.raw
{'_index': 'post-index',
 '_id': '1',
 '_version': 5,
 'result': 'deleted',
 '_shards': {'total': 2, 'successful': 1, 'failed': 0},
 '_seq_no': 4,
 '_primary_term': 1}

分词效果查看

res =es.indices.analyze(analyzer='ik_smart',text='我爱你水杯')
res['tokens']

[{'token': '我爱你',
  'start_offset': 0,
  'end_offset': 3,
  'type': 'CN_WORD',
  'position': 0},
 {'token': '水杯',
  'start_offset': 3,
  'end_offset': 5,
  'type': 'CN_WORD',
  'position': 1}]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值