我正在尝试将一个对象索引到elasticsearch,但除了找到很多死路之外我没有运气......
我的Application类看起来像这样:
import com.google.gson.Gson;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import static org.elasticsearch.common.xcontent.XContentFactory.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.elasticsearch.client.transport.TransportClient;
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private FilRepo repository;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9200));
// repository.deleteAll();
List<Fil> listFiler = new ArrayList();
// save a couple of customers
// repository.save(new Fil("Steffen-1", 5, new Date(), new Date()));
// repository.save(new Fil("Steffen-2", 10, new Date(), new Date()));
// fetch all customers
System.out.println("Filer funder med findAll():");
System.out.println("-------------------------------");
for (Fil f : repository.findAll()) {
listFiler.add(f);
IndexResponse response = client.prepareIndex("elasticsearch", "fil")
.setSource(jsonBuilder()
.startObject()
.field("fil", f.filNavn)
.field("size", f.filStr)
.field("created", f.created)
.field("modified", f.modified)
.endObject()
)
.get();
System.out.println("RESPONSE ER HER: " + response.toString());
}
System.out.println(listFiler);
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFilNavn('Steffen-1'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFilNavn("Steffen-1"));
client.close();
}
}
我的弹性搜索正在运行,我已经通过访问localhost来检查:9200它看起来像这样:
name "WpUHj5-"
cluster_name "elasticsearch"
cluster_uuid "nEgvRKklRveOr1ltZSPbeA"
version
number "5.5.0"
build_hash "260387d"
build_date "2017-06-30T23:16:05.735Z"
build_snapshot false
lucene_version "6.6.0"
tagline "You Know, for Search"
我从mvn spring-boot:run获取的错误日志是:
和我的Elasticsearch窗口上的错误: 基本上说&#34;现有连接由外部主机关闭&#34;
答案 0 :(得分:0)
首先,将配置部分与业务逻辑分开是件好事。因此,弹性搜索的配置bean应该遵循(注意correct port for communication via TransportClient
9300 ,端口9200用于REST,我认为这是主要问题,但可能不是唯一的问题):
@Configuration
public class ElasticsearchConfig {
private static final Logger LOG = getLogger(ElasticsearchConfig.class);
private Client client;
@SuppressWarnings("resource")
@Bean
public Client getClient() throws UnknownHostException {
Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch")
.build();
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
LOG.info("--ElasticSearch--");
Map<String, String> asMap = client.settings().getAsMap();
asMap.forEach((k, v) -> LOG.info(k + " = " + v));
LOG.info("--ElasticSearch--");
return client;
}
@PreDestroy
public void closeClient() {
client.close();
}
}
初始化client
对象后,出于测试目的,您可以检查您的逻辑(此处打印客户端的设置)但如果您可以将逻辑移动到单独的服务并使用它将更加清晰注入之后client
:
@Autowired
private Client client;
Main Spring Boot类:
@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "pl.jma.es.demo.esrepository")
public class EsDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EsDemoApplication.class, args);
}
}