# ES Python API behaves strangely when used for search right after ingestion

**URL:** <https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586>\
**Category:** Elasticsearch\
**Created:** [May 10, 2019, 4:01pm UTC](https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586 "2019-05-10T16:01:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mehdi-gital](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mehdi-gital](https://discuss.elastic.co/u/mehdi-gital)\
**Post date:** [May 10, 2019, 4:01pm UTC](https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586/1 "2019-05-10T16:01:41Z")

</div>

I run a search right after ingesting documents through my Python client and get an empty response even though in Kibana I can see that everything is ingested correctly. Here's a simple demo code:

```
from elasticsearch import Elasticsearch, helpers

es_host = 'localhost'
es_port = '9200'

es = Elasticsearch([{'host': es_host, 'port': es_port}])

bulk_data = []

for i in range(5):
    obj = {}

    obj['_index'] = 'test_ind'
    obj['_type'] = 'my_type'
    obj['a'] = i

    bulk_data.append(obj)

bulk_data = iter(bulk_data)
helpers.bulk(es, bulk_data)

body = {
  "query": {
    "match_all": {}
  }
}

print es.search('test_ind', body)

```

The above code does the ingestion but the search returns nothing. However, when I run my search in a separate code, it works as expected.

```
from elasticsearch import Elasticsearch

es_host = 'localhost'
es_port = '9200'

es = Elasticsearch([{'host': es_host, 'port': es_port}])

body = {
  "query": {
    "match_all": {}
  }
}

print es.search('test_ind', body=body)

```

Is there a conceptual reason as to why the first approach won't work or is this just a newbie goof on my part?

PS. Not sure if this matters but I'm using Python 2.7 and Elastic 5.5 (I know!!).

---

<div class="post-metadata">

**Author:** ![loren](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/loren/32/44942_2.png) [@loren](https://discuss.elastic.co/u/loren)\
**Post date:** [May 10, 2019, 7:47pm UTC](https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586/2 "2019-05-10T19:47:25Z")

</div>

After you do `helpers.bulk(es, bulk_data)`, you either need to wait a full second (`settings.refresh_interval`) [docs](https://www.elastic.co/guide/en/elasticsearch/reference/master/index-modules.html#dynamic-index-settings), send in an [explicit `refresh`](https://www.elastic.co/guide/en/elasticsearch/reference/master/docs-refresh.html) with the bulk request, or use the [Refresh API](https://www.elastic.co/guide/en/elasticsearch/reference/master/indices-refresh.html) to have those docs returned from a search so soon after you index them.

---

<div class="post-metadata">

**Author:** ![mehdi-gital](https://avatars.discourse-cdn.com/v4/letter/m/65b543/32.png) [@mehdi-gital](https://discuss.elastic.co/u/mehdi-gital)\
**Post date:** [May 13, 2019, 2:18pm UTC](https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586/3 "2019-05-13T14:18:23Z")

</div>

Hey thanks for your reply!!!  
I had never looked into this but now it makes a lot of sense.  
I tried to follow your pointers and added

```
time.sleep(2)
es.indices.refresh('test_ind')
time.sleep(2)

```

after the ingestion step but it's not making a difference. I'll figure it out with try and error though.  
I remember I had similar issues with Rollover too ...

---

<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:** [June 10, 2019, 2:18pm UTC](https://discuss.elastic.co/t/es-python-api-behaves-strangely-when-used-for-search-right-after-ingestion/180586/4 "2019-06-10T14:18:27Z")

</div>

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