通过Java api 的方式创建索引,想着是对name字段设置对应的分词器,但是执行代码一直出错,也是第一次接触es,不知道怎么处理,什么地方的格式有问题
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.junit.jupiter.api.Test;
import java.io.IOException;
//创建索引
public class creat_es_index {
private static final String IP = "IP地址";
private static final Integer Dk = 9200;
private static final String FWFS = "http";
@Test
public void esindex() throws IOException {
//创建es客户端
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(
RestClient.builder(new HttpHost(IP,Dk,FWFS))
);
CreateIndexRequest createIndexRequest = new CreateIndexRequest("ceshi");
createIndexRequest.settings(Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 0));
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();{
builder.startObject("properties");
{
// 流水号
builder.startObject("name");
{
builder.field("type", "text").field("analyzer","ik_max_word");
}
builder.endObject();
// 手机号
builder.startObject("age");
{
builder.field("type", "keyword");
}
builder.endObject();
// 发送时间
builder.startObject("sex");
{
builder.field("type", "keyword");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
createIndexRequest.mapping(builder);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
if (createIndexResponse.isAcknowledged()==true){
System.out.println("索引创建成功");
}else {
System.out.println("索引创建失败");
}
//关闭es客户端
restHighLevelClient.close();
}
}
运行结果及报错内容
希望有会的能指导一下