https://github.com/plotlabs/esgetfamily-python
Get parent and children documents from Elasticsearch using a single query.
https://github.com/plotlabs/esgetfamily-python
elasticsearch python
Last synced: 4 months ago
JSON representation
Get parent and children documents from Elasticsearch using a single query.
- Host: GitHub
- URL: https://github.com/plotlabs/esgetfamily-python
- Owner: plotlabs
- License: mit
- Created: 2018-04-25T08:30:18.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T01:00:16.000Z (about 3 years ago)
- Last Synced: 2025-09-01T11:56:28.479Z (5 months ago)
- Topics: elasticsearch, python
- Language: Python
- Homepage: https://www.plotlabs.io
- Size: 17.6 KB
- Stars: 7
- Watchers: 2
- Forks: 2
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Elasticsearch: Get Family - Python
[](https://github.com/plotlabs/esgetfamily-python/blob/master/LICENSE.txt) [](https://travis-ci.org/plotlabs/esgetfamily-python) [](https://www.codefactor.io/repository/github/plotlabs/esgetfamily-python) [](https://www.plotlabs.io/) [](https://pypi.org/project/esgetfamily/)
_This package returns the parent and its children together. This is compatible with elasticsearch version >=5.0.0,<6.0.0._
```python
from elasticsearch import Elasticsearch
import esgetfamily
def get_es_object():
es = Elasticsearch(elastic_cred)
return es
es_db = get_es_object()
```
> **For single parent:**
```python
parents = es_db.get(index=parent_index, doc_type=parent_type, id=parent_id)
final = esgetfamily.parent_child(es_db, parents, child_type)
```
> **For single parent with query for child:**
```python
query = {
"match": {
"id": 1
}
}
parents = es_db.get(index=parent_index, doc_type=parent_type, id=parent_id)
final = esgetfamily.parent_child(es_db, parents, child_type, query)
```
> **For multiple parents:**
```python
parents = [es_db.get(index=parent_index, doc_type=parent_type, id=parent_id),
es_db.get(index=parent_index, doc_type=parent_type, id=parent_id)]
final = esgetfamily.parent_child(es_db, parents, child_type)
```
> **For multiple parents with query for child:**
```python
query = {
"match": {
"id": 1
}
}
parents = [es_db.get(index=parent_index, doc_type=parent_type, id=parent_id),
es_db.get(index=parent_index, doc_type=parent_type, id=parent_id)]
final = esgetfamily.parent_child(es_db, parents, child_type, query)
```
> **Sample Example:**
#### Code:
```python
from elasticsearch import Elasticsearch
def get_es_object():
es = Elasticsearch(elastic_cred)
return es
es_db = get_es_object()
import esgetfamily, json
parents = es_db.get(index="parent", doc_type="par", id=3)
final = esgetfamily.parent_child(es_db, parents, "child")
print json.dumps(final, indent=3)
```
#### Result:
```json
{
"3": {
"parent": {
"_type": "par",
"_source": {
"age": 23,
"id": 3,
"name": "ABC"
},
"_index": "parent",
"_version": 1,
"found": true,
"_id": "3"
},
"child": [
{
"_type": "child",
"_routing": "3",
"_index": "parent",
"_score": 1.0,
"_source": {
"id": 1,
"name": "Child1"
},
"_parent": "3",
"_id": "1"
},
{
"_type": "child",
"_routing": "3",
"_index": "parent",
"_score": 1.0,
"_source": {
"id": 2,
"name": "Child2"
},
"_parent": "3",
"_id": "2"
}
]
}
}
```