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

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:

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:

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?

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