# Can not query when inserting data

**URL:** <https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532>\
**Category:** Elasticsearch\
**Created:** [April 16, 2024, 4:52pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532 "2024-04-16T16:52:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![D\_Tu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d_tu/32/133568_2.png) [@D\_Tu](https://discuss.elastic.co/u/D_Tu)\
**Post date:** [April 16, 2024, 4:52pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/1 "2024-04-16T16:52:45Z")

</div>

I am a newbie to elastic and use spring boots to interact with elastic.  
I have a scenario as below:

1. inserting a document like A
2. inserting a document like B ( and this action takes 5m to complete) and I also insert other documents like C and D after but quickly  
then I performed searching but I got only A because the B was not completed  
after 5m I queried again and got all A B C and D.  
it seems that I can't query new data C and D when performing an action insert or update B  
so, could you tell me why and how can I fix this case by config or something else?  
Thanks.

---

<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:** [April 16, 2024, 5:23pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/2 "2024-04-16T17:23:38Z")

</div>

Welcome!

Most likely you need to call the [\_refresh API](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html).

But please read the warning on that page:

> Refreshes are resource-intensive. To ensure good cluster performance, we recommend waiting for Elasticsearch’s periodic refresh rather than performing an explicit refresh when possible.
> 
> If your application workflow indexes documents and then runs a search to retrieve the indexed document, we recommend using the [index API](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html)'s `refresh=wait_for` query parameter option. This option ensures the indexing operation waits for a periodic refresh before running the search.

---

<div class="post-metadata">

**Author:** ![D\_Tu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d_tu/32/133568_2.png) [@D\_Tu](https://discuss.elastic.co/u/D_Tu)\
**Post date:** [April 16, 2024, 5:50pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/3 "2024-04-16T17:50:49Z")

</div>

Thank you @dadoonet  
But when I am inserting perhaps there is a transaction to lock the index, I called \_refresh but it didn't work immediately and took a long time and it waited until the insert action finished then it worked

this is my insert code

```auto
  elasticsearchClient.index(i ->
                        i.index(elasticSearchConfig.fileIndex)
                                .id(finalInput.getId().toString())
                                .document(finalInput)

                );

```

query code

```auto

        SearchHits<FileDocumentContentDto> searchResponse = elasticsearchOperations.search(
                searchQuery,
                FileDocumentContentDto.class,
                IndexCoordinates.of(elasticSearchConfig.fileIndex));

```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 16, 2024, 6:08pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/4 "2024-04-16T18:08:23Z")

</div>

> [@D\_Tu](#):
>
> inserting a document like B ( and this action takes 5m to complete)

This sounds incredibly long. What is the size and configuration of your cluster? Where is it running?

What is the size of your document? What type of data does it contain?

---

<div class="post-metadata">

**Author:** ![D\_Tu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/d_tu/32/133568_2.png) [@D\_Tu](https://discuss.elastic.co/u/D_Tu)\
**Post date:** [April 16, 2024, 6:14pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/5 "2024-04-16T18:14:49Z")

</div>

I parsed a PDF 180M to a text and then stored it in the index. is that ok?

I used a default configuration like that

```auto
#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically
# generated to configure Elasticsearch security features on 16-04-2024 14:12:48
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: true
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: true
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

```

---

<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:** [May 14, 2024, 6:15pm UTC](https://discuss.elastic.co/t/can-not-query-when-inserting-data/357532/6 "2024-05-14T18:15:27Z")

</div>

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