# Cannot found inserted document after delete index

**URL:** <https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225>\
**Category:** Elasticsearch\
**Created:** [May 12, 2020, 12:38pm UTC](https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225 "2020-05-12T12:38:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [May 12, 2020, 12:38pm UTC](https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225/1 "2020-05-12T12:38:50Z")

</div>

I write a simple test validate duplicates are not exist:

```
@Test
public void testSameDataNotPushedTwice() throws Exception {
	// Do some logic
	// index contains es index name
	
	// adding this line fail the test
	// deleteOldData(esPersistence.getESClient(), index);
	esPersistence.insert(cdrData);
	esPersistence.insert(cdrData);

	SearchResponse searchResponse = getDataFromElastic(esPersistence.getESClient(), index);
	assertThat(searchResponse.getHits().getHits().length).isEqualTo(1);
}

```

As you can see I push data to ES and check hits length equals 1.

Test is passed when the delete line is in commnet.

Now, I want to make sure there is no data from old / others tests, therefore I want to delete the index before the insert. The problem: delete and then insert return 0 hits in Search Response.

The delete index method:

```
public static void deleteOldData(RestHighLevelClient client, String index) throws IOException {
	GetIndexRequest request = new GetIndexRequest(index);
	boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
	if (exists) {
		DeleteIndexRequest deleteRequest = new DeleteIndexRequest(index);
		client.indices().delete(deleteRequest, RequestOptions.DEFAULT);
	}
}

```

- ES 7.6.2
- The data is exist in ES.
- Adding sleep not solve the problem (even for 10 seconds).
- The search is working (document is found) while debbuging.
- `SettingsResponse` not return `refresh_interval` (so the default is 1s)

**Bottom line:** How can I perform delete index --\> insert --\> search and found the documents?

**More Info:**

Insert methods:

```
public boolean insert(List<ProjectData> projDataList) {
	// Relevant Lines
	BulkRequest bulkRequest = prepareBulkRequests(projDataList, esConfiguration.getCdrDataIndexName());
	insertBulk(bulkRequest)
}

private BulkRequest prepareBulkRequests(List<ProjectData> data, String indexName) {
	BulkRequest bulkRequest = new BulkRequest();
	for (ProjectData ProjectData : data) {
		String json = jsonParser.parsePojo(ProjectData);

		bulkRequest.add(new IndexRequest(indexName)
				.id(ProjectData.getId())
				.source(json, XContentType.JSON));
	}

	return bulkRequest;
}

private boolean insertBulk(BulkRequest bulkRequest) {
	try {
		BulkResponse bulkResponse = rhlClient.bulk(bulkRequest, RequestOptions.DEFAULT);

		if (bulkResponse.hasFailures()) {
			logger.error(buildCustomBulkFailedMessage(bulkResponse));
			return false;
		}

	} catch (IOException e) {
		logger.warn("Failed to insert csv fields. Error: {}", e.getMessage());
		return false;
	}

	return true;
}
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [May 12, 2020, 1:45pm UTC](https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225/2 "2020-05-12T13:45:11Z")

</div>

I think you need to make sure that you are refreshing the index after the bulk execution and before the search operation.

Have a look at:

> <https://github.com/dadoonet/elasticsearch-java-client-demo/blob/master/src/test/java/fr/pilato/test/elasticsearch/hlclient/EsClientTest.java#L151-L165>

Might help...

---

<div class="post-metadata">

**Author:** ![AsfK](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/asfk/32/58995_2.png) [@AsfK](https://discuss.elastic.co/u/AsfK)\
**Post date:** [May 12, 2020, 2:24pm UTC](https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225/3 "2020-05-12T14:24:18Z")

</div>

> [@dadoonet](#):
>
> IndexRequest

Thank you very much!! actually refresh is enough (not need the IndexRequest line in the code)  
Thanks again,  
Assaf

---

<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 9, 2020, 2:24pm UTC](https://discuss.elastic.co/t/cannot-found-inserted-document-after-delete-index/232225/4 "2020-06-09T14:24:52Z")

</div>

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