Need example Java code for OpenSearch CRUD API

Joined
Feb 2, 2023
Messages
2
Reaction score
0
I am looking for example Java code so that I can write index & search query in OpenSearch. I didn't use OpenSearch or ElasticSearch before. Any help would be much appreciated. Thank you.
 
Joined
Jan 30, 2023
Messages
107
Reaction score
13
I am looking for example Java code so that I can write index & search query in OpenSearch. I didn't use OpenSearch or ElasticSearch before. Any help would be much appreciated. Thank you.
Here's an example of indexing a document in OpenSearch using Java:
Java:
import java.util.HashMap;
import java.util.Map;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.document.DocumentClient;
import com.aliyun.opensearch.sdk.generated.document.Field;
import com.aliyun.opensearch.sdk.generated.document.FieldTypeEnum;
import com.aliyun.opensearch.sdk.generated.document.update.UpdateParams;

public class OpenSearchExample {
    public static void main(String[] args) {
        String accessKeyId = "YOUR_ACCESS_KEY_ID";
        String secret = "YOUR_SECRET";
        String baseUrl = "YOUR_BASE_URL";
        OpenSearchClient client = new OpenSearchClient(accessKeyId, secret, baseUrl);
        DocumentClient documentClient = client.getDocumentClient();

        Map<String, Field> fields = new HashMap<>();
        fields.put("id", new Field("1", FieldTypeEnum.LONG));
        fields.put("title", new Field("OpenSearch", FieldTypeEnum.TEXT));
        fields.put("description", new Field("OpenSearch is a search engine service provided by Alibaba Cloud.", FieldTypeEnum.TEXT));

        UpdateParams updateParams = new UpdateParams("YOUR_APP_NAME", "YOUR_DOCUMENT_TABLE_NAME");
        updateParams.setFields(fields);

        OpenSearchResult result = documentClient.update(updateParams);
        System.out.println(result.getResult());
    }
}


And here's an example of a search query in OpenSearch using Java:

Java:
import java.util.HashMap;
import java.util.Map;
import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.SearcherClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;
import com.aliyun.opensearch.sdk.generated.search.SearchParams;

public class OpenSearchExample {
    public static void main(String[] args) {
        String accessKeyId = "YOUR_ACCESS_KEY_ID";
        String secret = "YOUR_SECRET";
        String baseUrl = "YOUR_BASE_URL";
        OpenSearchClient client = new OpenSearchClient(accessKeyId, secret, baseUrl);
        SearcherClient searcherClient = client.getSearcherClient();

        SearchParams searchParams = new SearchParams("YOUR_APP_NAME");
        searchParams.setQuery("default:'OpenSearch'");
        searchParams.setFormat("json");

        OpenSearchResult result = sear
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,516
Members
44,991
Latest member
Josephnag

Latest Threads

Top