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

[FEATURE] Support Search (Query) templates #155

Open
gquintillan opened this issue Sep 13, 2023 · 1 comment
Open

[FEATURE] Support Search (Query) templates #155

gquintillan opened this issue Sep 13, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@gquintillan
Copy link

gquintillan commented Sep 13, 2023

Is your feature request related to a problem?

I would like to use the org.springframework.data.elasticsearch.core.query.SearchTemplateQuery but I get this error when I try to use it:

java.lang.IllegalArgumentException: unhandled Query implementation org.springframework.data.elasticsearch.core.query.SearchTemplateQuery
	at org.opensearch.data.client.orhlc.RequestFactory.getQuery(RequestFactory.java:1174) ~[spring-data-opensearch-1.2.0.jar:na]
	at org.opensearch.data.client.orhlc.RequestFactory.searchRequest(RequestFactory.java:752) ~[spring-data-opensearch-1.2.0.jar:na]
	at org.opensearch.data.client.orhlc.OpenSearchRestTemplate.search(OpenSearchRestTemplate.java:375) ~[spring-data-opensearch-1.2.0.jar:na]
	at org.springframework.data.elasticsearch.core.AbstractElasticsearchTemplate.search(AbstractElasticsearchTemplate.java:492) ~[spring-data-elasticsearch-5.1.2.jar:5.1.2]

What solution would you like?

Support org.springframework.data.elasticsearch.core.query.SearchTemplateQuery in

"unhandled Query implementation " + query.getClass().getName());

Do you have any additional context?

Search Templates are way to move the query logic to OpenSearch and make the code cleaner in the microservice side. Moreover this queries are more efficient since they are precompiled.

Example of use of query templates can be seen here: https://github.com/spring-projects/spring-data-elasticsearch/blob/main/src/main/antora/modules/ROOT/pages/elasticsearch/misc.adoc

public class PersonCustomRepositoryImpl implements PersonCustomRepository {

  private final ElasticsearchOperations operations;

  public PersonCustomRepositoryImpl(ElasticsearchOperations operations) {
    this.operations = operations;
  }

  @Override
  public SearchPage<Person> findByFirstNameWithSearchTemplate(String firstName, Pageable pageable) {

    var query = SearchTemplateQuery.builder()                               (1)
      .withId("person-firstname")                                           (2)
      .withParams(
        Map.of(                                                             (3)
          "firstName", firstName,
          "from", pageable.getOffset(),
          "size", pageable.getPageSize()
          )
      )
      .build();

    SearchHits<Person> searchHits = operations.search(query, Person.class); (4)

    return SearchHitSupport.searchPageFor(searchHits, pageable);
  }
}
@gquintillan gquintillan added enhancement New feature or request untriaged labels Sep 13, 2023
@gquintillan gquintillan changed the title [FEATURE] Support Search templates [FEATURE] Support Search (Query) templates Sep 14, 2023
@gquintillan
Copy link
Author

gquintillan commented Sep 14, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants