package com.pgy.controller;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;
import java.net.InetAddress;
import java.util.List;

public class ElasticHandler {
    private TransportClient  client;
    public ElasticHandler() throws Exception {
        Settings settings = Settings.builder().build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.2.80"), 9300));
        prepareData(client);
        client.close();
    }

    private void prepareData(TransportClient  client) throws IOException {
        client.prepareIndex("company","employee","1")
                .setSource(XContentFactory.jsonBuilder().startObject()
                        .field("name","jack") .field("age",28)
                        .endObject()).get();
    }


}
经测试 可以使用
查询结果:

elasticsearch 在 java中应用demo

其他方法后续更新

相关文章:

  • 2021-11-25
  • 2021-11-15
  • 2021-06-10
  • 2021-05-27
  • 2022-12-23
  • 2021-04-30
猜你喜欢
  • 2021-10-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2021-09-30
  • 2022-12-23
相关资源
相似解决方案