Here's an example of indexing a document in OpenSearch using Java: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.
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());
}
}
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
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.