例如这串json数据
public static final String JSON_TEMPLATE = "{\n" +
" \"address\" : \"静安交通路40号\",\n" +
" \"brand\" : \"7天酒店\",\n" +
" \"business\" : \"四川北路商业区\",\n" +
" \"city\" : \"上海\",\n" +
" \"id\" : 36934,\n" +
" \"location\" : \"31.251433, 121.47522\",\n" +
" \"name\" : \"7天连锁酒店(上海宝山路地铁站店)\",\n" +
" \"pic\" : \"https://m.tuniucdn.com/fb2/t1/G1/M00/3E/40/Cii9EVkyLrKIXo1vAAHgrxo_pUcAALcKQLD688AAeDH564_w200_h200_c1_t0.jpg\",\n" +
" \"price\" : 336,\n" +
" \"score\" : 37,\n" +
" \"starName\" : \"二钻\"\n" +
"}";
代码的实现:
public class ElasticSearchIndexer2 {
private RestHighLevelClient client;
//连接虚拟机,
public ElasticSearchIndexer2() {
this.client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.200.128", 9200, "http")));
}
public void indexDocument(String indexName,String typeName,String json) throws IOException {
try {
IndexRequest request = new IndexRequest(indexName, typeName);
request.source(HotelIndexConstants.JSON_TEMPLATE, json);
client.index(request, RequestOptions.DEFAULT);
System.out.println("Index created successfully");
} catch (IndexNotFoundException e) {
System.out.println("Index does not exist");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
} finally {
if (client != null) {
client.close();
}
}
}
}