Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-data-elasticsearch:4.4.10 IndicesBoost error #2598

Closed
freedally opened this issue Jun 20, 2023 · 5 comments · Fixed by #2606
Closed

spring-data-elasticsearch:4.4.10 IndicesBoost error #2598

freedally opened this issue Jun 20, 2023 · 5 comments · Fixed by #2606
Labels
type: bug A general bug

Comments

@freedally
Copy link

freedally commented Jun 20, 2023

spring-data-elasticsearch:4.4.10

    List<IndexBoost> indexBoosts = new ArrayList<>();
    indexBoosts.add(new IndexBoost(IndexConst.INDEX_NAME_1, 2));
    indexBoosts.add(new IndexBoost(IndexConst.INDEX_NAME_2, 1));


    Pageable pageable = PageRequest.of(0, 10);

    NativeQuery nativeQuery = NativeQuery.builder()
            .withIndicesBoost(indexBoosts)
            .withQuery(query)
            .withPageable(pageable).build();

    SearchHits<EntityAsMap> searchHits = elasticsearchTemplate.search(nativeQuery, EntityAsMap.class, IndexCoordinates.of(IndexConst.INDEX_NAME_1, IndexConst.INDEX_NAME_2));

Received exception

Caused by: co.elastic.clients.elasticsearch._types.ElasticsearchException: [es/search] failed: [parsing_exception] Expected [END_OBJECT] in [indices_boost] but found [FIELD_NAME]
	at co.elastic.clients.transport.rest_client.RestClientTransport.getHighLevelResponse(RestClientTransport.java:313)
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:153)
	at co.elastic.clients.elasticsearch.ElasticsearchClient.search(ElasticsearchClient.java:1754)
	at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.lambda$search$14(ElasticsearchTemplate.java:312)
	at org.springframework.data.elasticsearch.client.elc.ElasticsearchTemplate.execute(ElasticsearchTemplate.java:514)
	... 71 more

Is my method incorrect?

There seems to be an error here:org.springframework.data.elasticsearch.client.elc.RequestConverter

// line 1206
if (!isEmpty(query.getIndicesBoost())) {
    Map<String, Double> boosts = new LinkedHashMap<>();
    query.getIndicesBoost().forEach(indexBoost -> {
        boosts.put(indexBoost.getIndexName(), (double) indexBoost.getBoost());
    });
    // noinspection unchecked
    builder.indicesBoost(boosts);
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 20, 2023
@sothawo
Copy link
Collaborator

sothawo commented Jun 21, 2023

You are using the new client, what version is the server? If it is an Elasticsearch 8 server, do you set the compatibility headers?

4.4.x is out of maintenance now, can you try this with the current version (5.1)

@sothawo sothawo added status: waiting-for-feedback We need additional information before we can continue and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 21, 2023
@freedally
Copy link
Author

elasticsearch version:7.17.8

I upgraded my dependencies:
spring-boot-starter-data-elasticsearch:3.1.1
spring-data-elasticsearch:5.1.1

I got the same result.
I set boosts to an array during debugging and got the correct result.

if (!isEmpty(query.getIndicesBoost())) {
  Map<String, Double> boosts = new LinkedHashMap<>();
  query.getIndicesBoost()
	  .forEach(indexBoost -> boosts.put(indexBoost.getIndexName(), (double) indexBoost.getBoost()));
  // noinspection unchecked
  builder.indicesBoost(boosts);
}

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 25, 2023
@sothawo
Copy link
Collaborator

sothawo commented Jun 25, 2023

I set boosts to an array during debugging and got the correct result.

what do you mean with that, where do you change?

@sothawo sothawo added status: waiting-for-feedback We need additional information before we can continue and removed status: feedback-provided Feedback has been provided labels Jun 25, 2023
@freedally
Copy link
Author

I got the correct result after making the modifications like this.
org.springframework.data.elasticsearch.client.elc.RequestConverter

  // line 1414
  if (!isEmpty(query.getIndicesBoost())) {
	  List<Map<String, Double>> boosts = query.getIndicesBoost().stream()
			  .map(indexBoost -> Map.of(indexBoost.getIndexName(), (double) indexBoost.getBoost()))
			  .collect(Collectors.toList());
  
	  // noinspection unchecked
	  builder.indicesBoost(boosts);
  }

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 26, 2023
@sothawo sothawo added type: bug A general bug and removed status: feedback-provided Feedback has been provided labels Jun 26, 2023
@sothawo
Copy link
Collaborator

sothawo commented Jun 26, 2023

Just had a closer look at the Elasticsearch client and the API definition. So it seems that the we need to send an array of objects that just have one property (index name) and value.

I thought that the client would send the entries from the map as an array of such objects, but here the Map seems only to be used to represent an object with a property whose name is not known beforehand.

So switching from one Map with many values to a list of maps with one value each is correct. Thanks for finding that.

I'll fix that later today tomorrow.

sothawo added a commit to sothawo/spring-data-elasticsearch that referenced this issue Jun 27, 2023
sothawo added a commit that referenced this issue Jun 27, 2023
Original Pull Request #2606
Closes #2598
@sothawo sothawo added this to the 5.2 M1 (2023.1.0) milestone Jun 27, 2023
sothawo added a commit that referenced this issue Jun 27, 2023
Original Pull Request #2606
Closes #2598

(cherry picked from commit d9fd722)
sothawo added a commit that referenced this issue Jun 27, 2023
Original Pull Request #2606
Closes #2598

(cherry picked from commit d9fd722)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants