P_Elascticsearch_Easy

Description
P_Elascticsearch_Easy is a Python utility that provides a simplified class for interacting with elasticsearch and elasticsearch_dsl.
Features
- Create a connection with Elasticsearch
- Retrieve a document given an ID
- Perform a delete_by_query
- Update a document
- Perform an update_by_query
- Index a document
- Index a document given an ID
- Delete a document given an ID
- Perform a bulk operation using the helpers library
- Perform a native bulk operation without using the helpers library
- Perform searches in indices using queries
- Retrieve all documents matching a query
- Perform searches using msearch
- Perform searches using mget
- Get a count of documents matching a query
Installation
You can install the package directly from PyPi using pip:
pip install p-elasticsearch-easy
Usage
Example of use ElasticsearchNative
from p_elasticsearch_easy import ElasticsearchNative
es_native: ElasticsearchNative = ElasticsearchNative("https://elastic.com:9243")
es_native.connect()
resp = es_native.search(id="id", index="name_index")
query = {"query":{"match_all":{}}}
es_native.delete_by_query(information=query, index="name_index")
doc = {
"field1": "value",
"field_2": "value"
}
es_native.update(document=doc, id="id", index="name_index")
query = {"query":{"match_all":{}}}
es_native.update_by_query(body=query, index="name_index", wait_for="true")
doc = {
"field1": "value",
"field_2": "value"
}
es_native.index(document=doc, index="name_index")
query = {
"size": 10,
"query": {"bool": {"must": [{"term": {"name_field": {"value": ""}}}]}},
}
resp = es_native.search(index="name_index", body=query)
actions = [
{
"_op_type": "index",
"_index": "mi_indice",
"_source": {
"title": "Documento 1",
"contenido": "Este es el primer documento"
}
},
{
"_op_type": "index",
"_index": "mi_indice",
"_source": {
"title": "Documento 2",
"contenido": "Este es el segundo documento"
}
}
]
success_count, errors = es_native.bulk(actions=actions)
operations = [
json.dumps({ "index": { "_index": "mi_indice", "_id": "1" } }),
json.dumps({ "title": "Documento 1", "contenido": "Este es el primer documento" }),
json.dumps({ "index": { "_index": "mi_indice", "_id": "2" } }),
json.dumps({ "title": "Documento 2", "contenido": "Este es el segundo documento" })
]
es_native.bulk_without_helper(operations=operations, wait_for="wait_for")
query = {
"query": {"bool": {"must": [{"term": {"name_field": {"value": ""}}}]}},
}
es_native.search_all(body=query, index="name_index")
searches = [
{"index": "mi_indice"},
{
"query": {
"match": {
"contenido": "primer"
}
}
},
{"index": "mi_indice"},
{
"query": {
"match": {
"contenido": "segundo"
}
}
}
]
es_native.msearch(searches=searches)
doc_ids = ["1", "2", "3"]
response = es_native.mget(index="name_index", doc_ids=doc_ids)
query = {
"query": {"bool": {"must": [{"term": {"name_field": {"value": ""}}}]}},
}
resp = es_native.count(index="name_index", body=query)
es_native.close()
License
This project is licensed under the GNU GPLv3 license. See the LICENSE file for more details.