Search code examples
springamazon-web-servicesspring-bootamazon-kendra

Aws Kendra and Spring boot


I want to integrate spring boot with the aws kendra query index.I want to leverage kendra as elastic search where i make api for search query and then get results for the same via that api.

Documentations dont clearly mention the connection procedure/steps. Not sure if thats possible or not.

P.s created index and dataSource as webcrawler. Now whats the step forward.


Solution

  • We can follow the official AWS documentation to query an index. The tricky part is to provide the credentials while building the KendraClient object. The code is taken mostly from the link mentioned with the addition of creating Kendra Client.

    A sample code would look like as below -

    import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
    import software.amazon.awssdk.auth.credentials.AwsCredentials;
    import software.amazon.awssdk.regions.Region;
    import software.amazon.awssdk.services.kendra.KendraClient;
    import software.amazon.awssdk.services.kendra.model.QueryRequest;
    import software.amazon.awssdk.services.kendra.model.QueryResponse;
    import software.amazon.awssdk.services.kendra.model.QueryResultItem;
    
    public class SearchKendraIndex {
    
        public static void main(String[] args) {
    
            String accessKey = "your-access-key";
            String secretKey = "your-secret-key";
    
            AwsCredentials awsCredentials = AwsBasicCredentials.create(accessKey, secretKey);
    
            KendraClient kendra = KendraClient
                    .builder()
                    .region(Region.AP_SOUTHEAST_1) // Change it with your region
                    .credentialsProvider(() -> awsCredentials)
                    .build();
    
            String query = "your-search-term";
            String indexId = "your-index-id";
    
            QueryRequest queryRequest = QueryRequest
                    .builder()
                    .queryText(query)
                    .indexId(indexId)
                    .build();
    
            QueryResponse queryResponse = kendra.query(queryRequest);
    
            System.out.printf("\nSearch results for query: %s%n", query);
            for (QueryResultItem item : queryResponse.resultItems()) {
                System.out.println("----------------------");
                System.out.printf("Type: %s%n", item.type());
                switch (item.type()) {
                    case QUESTION_ANSWER, ANSWER -> {
                        String answerText = item.documentExcerpt().text();
                        System.out.println(answerText);
                    }
                    case DOCUMENT -> {
                        String documentTitle = item.documentTitle().text();
                        System.out.printf("Title: %s%n", documentTitle);
                        String documentExcerpt = item.documentExcerpt().text();
                        System.out.printf("Excerpt: %s%n", documentExcerpt);
                    }
                    default -> System.out.printf("Unknown query result type: %s%n", item.type());
                }
                System.out.println("-----------------------\n");
            }
        }
    }