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 Templates via Annotation #2997

Open
gquintillan opened this issue Nov 5, 2024 · 2 comments
Open

[FEATURE] Support Search Templates via Annotation #2997

gquintillan opened this issue Nov 5, 2024 · 2 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@gquintillan
Copy link

gquintillan commented Nov 5, 2024

Elastic Search provides the Search template API which is a very clean and efficient way to run queries in ElasticSearch.

A search template is a stored search you can run with different variables.
If you use Elasticsearch as a search backend, you can pass user input from a search bar as parameters for a search template. This lets you run searches without exposing Elasticsearch’s query syntax to your users.
If you use Elasticsearch for a custom application, search templates let you change your searches without modifying your app’s code.

I checked both in the documentation and the code and I couldn't find an annotation that supports it.

They idea would be to have something like:

@Repository
public interface SampleDataRepository extends Repository<SampleData, String> {

    @SearchTemplateQuery(id="my-search-template")
    List<SampleData> findSamples(String queryString, int from, int size);

}

Which translates into this (example taken from the Elasticsearch documentation):

GET my-index/_search/template
{
  "id": "my-search-template",
  "params": {
    "queryString": "hello world",
    "from": 0,
    "size": 10
  }
}

In case it helps, without annotations we have to do it like this:

@Repository
public class SampleDataRepositoryImpl implements SampleDataRepository {

    private final ElasticsearchOperations elasticsearchOperations;

    public SampleDataRepositoryImpl(ElasticsearchOperations elasticsearchOperations) {
        this.elasticsearchOperations = elasticsearchOperations;
    }

    @Override
    public SearchHits<SampleData> findSamples(String queryString, int from, int size) {
        final Map<String, Object> templateParams = Map.of(
                "queryString", queryString,
                "from", from,
                "size", size);
        SearchTemplateQuery searchTemplateQuery = SearchTemplateQuery.builder()
                .withId("my-search-template")
                .withParams(templateParams)
                .build();
        return elasticsearchOperations.search(searchTemplateQuery, SampleData.class);
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 5, 2024
@sothawo sothawo added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 5, 2024
@sothawo
Copy link
Collaborator

sothawo commented Nov 5, 2024

Interesting idea. Might even find some time this week to have a deeper look.

@rishiraj88
Copy link

Very impressive and nice idea, @gquintillan ! Thanks a ton for thinking through and sharing.

@sothawo sothawo self-assigned this Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

4 participants