今天写了一段ES的测试代码,如下:
package elasticSearch;
import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.List; import java.util.Map;
import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; 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.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits;
import entity.Person; import util.JsonUtils;
public class ESTest {
private Client client; public ESTest() { this("localhost"); } public ESTest(String ip) { Settings settings = Settings.settingsBuilder().put("cluster.name", "huiTest") .build(); client = TransportClient.builder().settings(settings).build(); client = ((TransportClient) client).addTransportAddress( new InetSocketTransportAddress(new InetSocketAddress(ip, 9300))); } public IndexResponse createIndex(String indexName, String type, List<String> jsonArray) { IndexRequestBuilder builder = client.prepareIndex(indexName, type); for (String jsonTemp : jsonArray) { System.out.println(jsonTemp); builder = builder.setSource(jsonTemp); } IndexResponse response = builder.execute().actionGet(); return response; } public List<Person> search(QueryBuilder queryBuilder, String indexName, String type) { List<Person> persons = new ArrayList<>(); SearchResponse sResponse = client.prepareSearch(indexName).setTypes(type) .setQuery(queryBuilder).execute().actionGet(); SearchHits hits = sResponse.getHits(); System.out.println(hits.getTotalHits()); Double salary = 0.0; SearchHit[] hitArray = hits.hits(); for (SearchHit hit : hitArray) { Map<String, Object> map = hit.getSource(); Integer age = (Integer) map.get("age"); String name = (String) map.get("name"); Object salaryO = map.get("salary"); if (salaryO instanceof Integer) { salary = (Integer) salaryO * 1.0; } else { salary = (Double) salaryO; } Person p = new Person(name, age, salary); persons.add(p); } return persons; } public static void main(String[] args) { ESTest esTest = new ESTest(); List<Person> persons = JsonUtils.getData(10); List<String> jsonArray = JsonUtils.covertList2String(persons); String indexName = "liuhui'sindex";// 要小写 String type = "liuhui'sType"; esTest.createIndex(indexName, type, jsonArray); // QueryBuilder builder = QueryBuilders.matchAllQuery(); // QueryBuilder builder = QueryBuilders.termQuery("name", "hui10"); QueryBuilder builder = QueryBuilders.termQuery("age", "20"); List<Person> list = esTest.search(builder, indexName, type); System.out.println(list); }
}
运行时报错如下:
Exception in thread "main" NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{127.0.0.1}{localhost/127.0.0.1:9300}]]
at org.elasticsearch.client.transport.TransportClientNodesService.ensureNodesAreAvailable(TransportClientNodesService.java:290)
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:207)
at org.elasticsearch.client.transport.support.TransportProxyClient.execute(TransportProxyClient.java:55)
at org.elasticsearch.client.transport.TransportClient.doExecute(TransportClient.java:288)
at org.elasticsearch.client.support.AbstractClient.execute(AbstractClient.java:359)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:86)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:56)
at elasticSearch.ESTest.search( ESTest.java:60)
at elasticSearch.ESTest.main( ESTest.java:99)
造成这一异常的原因,仅我所知的,有以下几个:
运行该客户端时 elasticsearch.bat未启动;
cluster.name:huiTest未在ES的安装目录config下的elasticsearch.yml中配置,即如果代码里使用了put("cluster.name","huiTest"),则一定要在elasticsearch.yml中添加一行配置:cluster.name:huiTest,因为cluster.name默认是elasticsearch。当然也可以不使用put("cluster.name","huiTest"),或者使用put("cluster.name","elasticsearch");这里要注意如果修改了elasticsearch.yml文件,一定要重启elasticsearch.bat;
客户端端口不是用的9300,而是9200.9200是为http协议准备的,9300才是客户端使用的;
以上3条逐个排查,多半问题就解决了;