es文档整理1 indexapi

本文深入探讨了Elasticsearch的Index API,包括如何创建、更新和删除文档,以及设置索引元数据的关键细节。通过实例演示,阐述了如何使用Index API与Elasticsearch集群进行交互,确保数据的正确存储和检索。
摘要由CSDN通过智能技术生成
package sss.first;


import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


import org.apache.http.HttpHost;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse; 
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.VersionType;

import org.elasticsearch.rest.RestStatus;


//https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/5.6.9/index.html.
//high-level-client
public class IndexApi {


//创建连接
public static RestHighLevelClient getClient(String hostname) {
//low-level-client
RestClient restClient = RestClient.builder(
        new HttpHost(hostname, 9200, "http")).build();
RestHighLevelClient client =
    new RestHighLevelClient(restClient);
return client;
}

//创建index
public static IndexRequest createIndex(int optional) throws IOException{
if (optional == 1) {
IndexRequest request = new IndexRequest(
        "posts", 
        "doc",  
        "1");   
String jsonString = "{" +
        "\"user\":\"kimchy\"," +
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值