使用前端发送的json数据,创建Elasticsearch索引

 例如这串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();
            }
        }
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值