# How to access Records from Elasticsearch using python dynamically

**URL:** <https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248>\
**Category:** Elasticsearch\
**Created:** [July 25, 2019, 12:57pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248 "2019-07-25T12:57:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [July 25, 2019, 12:57pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/1 "2019-07-25T12:57:47Z")

</div>

Dear all.  
i have below in python but that one static value passed.

but need access the same code from services in dynamic way.

# Import Elasticsearch package

from elasticsearch import Elasticsearch

# Connect to the elastic cluster

es=Elasticsearch([{'host':'localhost','port':9200}]) #[http://127.0.0.1:9200](http://127.0.0.1:9200)  
print("connection has been done here")  
res=es.get(index='megacorp',doc\_type='employee',id='4')

print("you read data is:",res)  
print("-----------------------------------------------------------")  
print(res['\_source'])

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [July 25, 2019, 4:14pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/2 "2019-07-25T16:14:06Z")

</div>

Hi,

I am sure what exactly you're after but this link might help you (it did for me in past)

> **[Elasticsearch tutorial for beginners using Python](https://towardsdatascience.com/elasticsearch-tutorial-for-beginners-using-python-b9cb48edcedc)**
>
> This tutorial is for the beginners who want to learn Elasticsearch from the scratch. In this tutorial i am going to cover all the basic…

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [July 26, 2019, 2:50pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/3 "2019-07-26T14:50:48Z")

</div>

Hi @pjanzen  
Thanks but in this page ,they are proving value in static manner.means not in dynamic way:-  
For Example:-  
res= es.search(index='megacorp',body={  
'query':{  
'bool':{  
'must':{  
'match':{  
'first\_name':'nitin'  
}  
},  
"filter":{  
"range":{  
"age":{  
"gt":25  
}  
}  
}  
}  
}  
})  
print res['hits']['hits']  
HERE:- provided first\_name':'nitin' this is static way...but want to access any name: matching with Elasticsearch.

like:-  
name:any name?

---

<div class="post-metadata">

**Author:** ![pjanzen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pjanzen/32/13756_2.png) [@pjanzen](https://discuss.elastic.co/u/pjanzen)\
**Post date:** [July 26, 2019, 3:28pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/4 "2019-07-26T15:28:09Z")

</div>

I use python elasticsearch in 2 use cases.

1. I have a query which I execute and I do something with result.

- if I have more the 1000 records I need to use the scroll function to collect all information.

1. I have a prepared query (like what you have) and I update the the field with the data I am searching for. In your example that would be first\_name. Once the query is updated you can execute it and retrieve the result.

So you need to think about your use case and if at possible share that here. If you already have developed any code please share that as well, I might be useful to help your case.

Paul.

---

<div class="post-metadata">

**Author:** ![rameshkr1994](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rameshkr1994/32/59029_2.png) [@rameshkr1994](https://discuss.elastic.co/u/rameshkr1994)\
**Post date:** [August 16, 2019, 3:00pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/5 "2019-08-16T15:00:53Z")

</div>

Hi @pjanzen.

we can use JDBC connection for that.to access based on condition same as sql query.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 13, 2019, 3:01pm UTC](https://discuss.elastic.co/t/how-to-access-records-from-elasticsearch-using-python-dynamically/192248/6 "2019-09-13T15:01:14Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
