Suppressed: org.elasticsearch.client.ResponseException: method [PUT]
时间: 2024-06-13 16:09:10 浏览: 154
根据提供的引用内容,可以得知该错误是由于使用了不被识别的参数导致的。而在引用中提供了解决方法,即使用正确的CreateIndexRequest参数来调用create方法。下面是一个演示如何使用CreateIndexRequest来创建索引的例子:
```java
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.ObjectMapper;
import org.elasticsearch.index.mapper.ObjectMapper.Builder;
import org.elasticsearch.index.mapper.RootObjectMapper;
import java.io.IOException;
public class CreateIndexExample {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
CreateIndexRequest request = new CreateIndexRequest("books");
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
String mapping = "{\n" +
" \"properties\": {\n" +
" \"title\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"author\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"float\"\n" +
" }\n" +
" }\n" +
"}";
request.mapping(mapping, XContentType.JSON);
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
System.out.println("Acknowledged: " + acknowledged);
System.out.println("Shards Acknowledged: " + shardsAcknowledged);
client.close();
}
}
```
阅读全文