简体   繁体   中英

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.

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

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");
        }
    }
}

There are examples of how to INVOKE AWS Services from a Spring BOOT app. Here are a few that show use of the AWS SDK for Java V2 :

  1. Creating your first AWS Java web application
  2. Creating a dynamic web application that analyzes photos using the AWS SDK for Java .
  3. Creating the Amazon DynamoDB web application item tracker

There are more Spring BOOT/AWS Service examples in Github .

While there are no Spring BOOT examples for Kendra client (yet), the pattern is the same. You can build a MVC app and build a service that uses https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/kendra/KendraClient.html .

You can use the AWS SDK for Java to make API calls to AWS services. You can find code examples in the link above.

Setting up your Kendra client will look something like this:

String accessKey = "YOUR_ACCESS_KEY";
String secretKey = "YOUR_SECRET_KEY";
BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
AWSkendra kendraClient = AWSkendraClientBuilder
                .standard()
                .withCredentials(new AWSStaticCredentialsProvider(awsCredentials))
                .build();

After you set up your Kendra client, you can then use the query method to query your Kendra index.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM