# Search query is very slow while using spring-data-elasticsearch

**URL:** <https://discuss.elastic.co/t/search-query-is-very-slow-while-using-spring-data-elasticsearch/319803>\
**Category:** Elasticsearch\
**Created:** [November 25, 2022, 9:26am UTC](https://discuss.elastic.co/t/search-query-is-very-slow-while-using-spring-data-elasticsearch/319803 "2022-11-25T09:26:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farhan\_Nazmul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/farhan_nazmul/32/78041_2.png) [@Farhan\_Nazmul](https://discuss.elastic.co/u/Farhan_Nazmul)\
**Post date:** [November 25, 2022, 9:26am UTC](https://discuss.elastic.co/t/search-query-is-very-slow-while-using-spring-data-elasticsearch/319803/1 "2022-11-25T09:26:33Z")

</div>

I've an index which have:

- 6 shards
- 6 replica shards
- more than 1crore doc (row) in that single index
- 27 fields
- fields types are text, long and date
- index.max\_return\_result = 50000

Now I run the following query from postman:

```auto
curl --location --request POST 'http://es_host/my-index/_search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "from": 0,
    "size": 10000,
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": {
                            "query": "huda",
                            "operator": "OR",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": true,
                            "lenient": false,
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": true,
                            "boost": 1.0
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
    }
}'

```

The above query returns the result in less than 3 secs.

Now if I run the above query from e spring boot service using spring-data-Elasticsearch then it returns the result in 10+ secs.  
Here is the code:

```auto
BoolQueryBuilder boolQueryBuilder = boolQueryBuilder.must(QueryBuilders.matchQuery(std.getSearchField().getFieldName(), std.getSearchValue1()));

Query searchQuery = new NativeSearchQueryBuilder()
				.withSourceFilter(
						new FetchSourceFilter(new String[] {fields name.....}, new String[] {}))
				.withQuery(queryBuilder).build();

		SearchRequest searchRequest = new SearchRequest();
		searchRequest.indices(index);
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		searchSourceBuilder.from(0);
		searchSourceBuilder.size(10000);
		searchSourceBuilder.query(queryBuilder);
		searchRequest.source(searchSourceBuilder);
		try {
			long start = System.currentTimeMillis();
			log.info("starting with query in: " + start);
			SearchResponse search = rClient.search(searchRequest, RequestOptions.DEFAULT);
			long end = System.currentTimeMillis();
			log.info("query returned with in " + ((end - start) / 1000f));

		} catch (Exception e) {
			e.printStackTrace();
		}

```

I'm using:

- spring boot version 2.7.5
- my elasticsearch version is 7.17.7 (3 nodes are running)
- Update or insert in the index will happen very rarely few times in a year so the index a read heavy index.

So why the same query is the same query taking more time in spring boot?  
And how can I improve the search performance using spring boot?

The first raw query(without spring boot) is taking more than 2 secs, is it too much also?

---

<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:** [December 23, 2022, 9:26am UTC](https://discuss.elastic.co/t/search-query-is-very-slow-while-using-spring-data-elasticsearch/319803/2 "2022-12-23T09:26:47Z")

</div>

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