����λ�ã���ҳ > �����̳� > �̳� > ����Elasticsearch��������Python����ʾ��

����Elasticsearch��������Python����ʾ��

��Դ������������|��ʱ�䣺2024-10-25 09:46:34 |���Ķ���161��|�� ��ǩ�� a T last El S C AR ���� ���� ������ EA cs Python �� |����������

����Elasticsearch��ES���������ǽ������������ͷ����ij��ò�����Elasticsearch��һ������Lucene���������棬�ṩ��RESTful API�����������������͹������ݡ�

����Elasticsearch��ES���������ǽ������������ͷ����ij��ò�����Elasticsearch��һ������Lucene���������棬�ṩ��RESTful API�����������������͹������ݡ�

������һ����ϸ��Python����ʾ����չʾ������ӵ�Elasticsearch��������ִ��һЩ�������������ʾ��ʹ���˹ٷ��� elasticsearch-py �ͻ��˿⡣

1. ��װElasticsearch�ͻ��˿�

���ȣ�����Ҫ��װ elasticsearch �⡣����㻹û�а�װ������ʹ��pip���а�װ��

bash
pip install elasticsearch

2. ���ӵ�Elasticsearch������

������һ��������Python�ű���չʾ��������ӵ�Elasticsearch�����������������������ĵ���������������

from elasticsearch import Elasticsearch, helpers  
  
# ����Elasticsearch����  
es = Elasticsearch(  
    ['http://localhost:9200'],  # Elasticsearch��������ַ�Ͷ˿�  
    http_auth=('username', 'password'),  # �����Ҫ��֤����д�û���������  
    use_ssl=False,  # ���ʹ��HTTPS������ΪTrue  
    verify_certs=False  # ���ʹ��HTTPS����ǩ��֤�飬����ΪFalse  
)  
  
# ��������Ƿ�ɹ�  
if es.ping():  
    print("Successfully connected to Elasticsearch!")  
else:  
    print("Could not connect to Elasticsearch")  
    exit()  
  
# ��������  
index_name = 'my_index'  
if not es.indices.exists(index=index_name):  
    # ����������ӳ�䣨Schema��  
    mappings = {  
        'properties': {  
            'title': {'type': 'text'},  
            'content': {'type': 'text'},  
            'author': {'type': 'keyword'}  
        }  
    }  
    # ��������  
    es.indices.create(index=index_name, body={'mappings': mappings})  
    print(f"Index '{index_name}' created successfully.")  
else:  
    print(f"Index '{index_name}' already exists.")  
  
# �����ĵ�  
documents = [  
    {"_id": 1, "title": "Elasticsearch Basics", "content": "Learn the basics of Elasticsearch.", "author": "John Doe"},  
    {"_id": 2, "title": "Advanced Elasticsearch", "content": "Go deeper into Elasticsearch features.", "author": "Jane Smith"},  
    {"_id": 3, "title": "Elasticsearch Performance", "content": "Optimize Elasticsearch for performance.", "author": "Alice Johnson"}  
]  
  
# ʹ��bulk API���������ĵ�  
actions = [  
    {  
        "_index": index_name,  
        "_id": doc['_id'],  
        "_source": doc  
    }  
    for doc in documents  
]  
  
helpers.bulk(es, actions)  
print("Documents added successfully.")  
  
# �����ĵ�  
search_body = {  
    "query": {  
        "match": {  
            "content": "Elasticsearch"  
        }  
    }  
}  
  
response = es.search(index=index_name, body=search_body)  
print("Search results:")  
for hit in response['hits']['hits']:  
    print(hit['_source'])  
  
# ��������ѡ����ɾ������  
# es.indices.delete(index=index_name)  
# print(f"Index '{index_name}' deleted successfully.")

3. �������

  1. �������ã�
    • Elasticsearch(['http://localhost:9200']) �����ӵ������ڱ��������ϵ�Elasticsearch��������Ĭ�϶˿�Ϊ9200��
    • http_auth=('username', 'password') �����Elasticsearch��������Ҫ��֤����д�û��������롣
    • use_ssl �� verify_certs ���������ʹ��HTTPS������������Щѡ�
  2. ������ӣ�
    • ʹ�� es.ping() ������������Ƿ�ɹ���
  3. ����������
    • ʹ�� es.indices.exists(index=index_name) ��������Ƿ���ڡ�
    • ʹ�� es.indices.create(index=index_name, body={'mappings': mappings}) �����������������ĵ���ӳ�䡣
  4. �����ĵ���
    • ʹ�� helpers.bulk(es, actions) ���������ĵ��������С�
  5. �����ĵ���
    • ʹ�� es.search(index=index_name, body=search_body) ��������������ӡ���������
  6. ��������ѡ����
    • ʹ�� es.indices.delete(index=index_name) ɾ��������

4. ע������

  • ��������ַ ��ȷ��Elasticsearch�������������У����ҵ�ַ�Ͷ˿�������ȷ��
  • ��֤ �����Elasticsearch��������Ҫ��֤��ȷ���ṩ��ȷ���û��������롣
  • SSL ���������ʹ��HTTPS������ȷ���� use_ssl �� verify_certs ѡ�

С���Ƽ��Ķ�

�������������Ľ�Ϊ������Ϣ����������������ͬ���޹۵��֤ʵ��������

a 1.0
a 1.0
���ͣ���������������Ӫ״̬����ʽ��Ӫ�������ԣ����� ����

��Ϸ����

��Ϸ���

��Ϸ��Ƶ

��Ϸ����

��Ϸ�

��alittletotheleft������������һ��ܻ�ӭ����������������Ϸ����ҵ������Ƕ��ճ������еĸ���������

�����Ƶ����

����

ͬ������

����

ɨ��ά�����������ֻ��汾��

ɨ��ά����������΢�Ź��ںţ�

��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]

��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������