elasticsearch 在 java中应用demo

 
  
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_第1张图片

其他方法后续更新

你可能感兴趣的:(搜索引擎)