总结一下elasticsearch java api开发过程中遇到的一些问题。
elasticsearch版本:5.1.2
当我们按elasticsearch老版本2.x一样引用maven文件时,如下:
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>5.1.2version>
dependency>
在我们创建elasticsearch的TransportClient时候
TransportClient client = new PreBuiltTransportClient(),提示无法找到PreBuiltTransportClient类。
这是由于elasticsearch从5.x开始客户端放在了新的jar包里,我们需要引用如下:
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>transportartifactId>
<version>5.1.2version>
dependency>
执行java查询时候,报错如下:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/Logger
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:105)
at org.elasticsearch.common.logging.Loggers.getLogger(Loggers.java:72)
at org.elasticsearch.common.component.AbstractComponent.(AbstractComponent.java:37)
at org.elasticsearch.plugins.PluginsService.(PluginsService.java:98)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:94)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:119)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:247)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:125)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:111)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:101)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.Logger
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
只是由于elasticsearch 5.x中需要log4j的一些依赖,需要添加,在pom.xml中添加如下maven配置:
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.7version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.7version>
dependency>
按照2.x的方式构造Setting,如下:
Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.put("cluster.transport.sniff", true)
.put("index.refresh_interval", "60s")
.build();
在执行客户端查询时,报错如下:
java.lang.IllegalArgumentException: node settings must not contain any index level settings
at org.elasticsearch.common.settings.SettingsModule.(SettingsModule.java:131)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:133)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:247)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:125)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:111)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:101)
即5.x不支持索引级别的设置,即5.x不支持index.refresh_interval配置,且名字也变更了,5.x只支持的Settings配置如下:
配置 | 说明 |
---|---|
cluster.name | 集群name |
client.transport.sniff | 是否支持自动嗅探 |
client.transport.ignore_cluster_name | Set to true to ignore cluster name validation of connected nodes. (since 0.19.4) |
client.transport.ping_timeout | The time to wait for a ping response from a node. Defaults to 5s. |
client.transport.nodes_sampler_interval | How often to sample / ping the nodes listed and connected. Defaults to 5s. |
因此,我们配置如下即可:
Settings settings = Settings.builder()
.put("cluster.name", clusterName)
.put("client.transport.sniff", true)
.build();
在elasticsearch 2.x版本中,如果我们想指定查询返回的字段,可以使用fields字段设置,代码如下:
String index = xxx;
String type = xxx;
// 指定要返回的字段
String[] fields = new String[2];
fields[0] = "field1"; // 字段1名称
fields[1] = "filed2"; // 字段2名称
// 构造query
SearchRequestBuilder requestBuilder = client.prepareSearch(index);
requestBuilder .setFrom(0).setSize(100)
.setTimeout(TimeValue.timeValueMillis(300))
.setTypes(type)
.addFields(fields);
// bool 条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
TermQueryBuilder tqb = QueryBuilders.termQuery("field1", "val1");
boolQueryBuilder.must(tqb);
// set query
requestBuilder.setQuery(boolQueryBuilder);
// get response
SearchResponse response = requestBuilder.execute().actionGet();
// 遍历返回的字段
SearchHits searchHits = response.getHits();
for (SearchHit hit : searchHits) {
System.out.println(hit.getFields().get("filed1").getValue().toString());
}
但是在5.x中,setFileds函数已经不存在了,那在5.x中通过什么来指定返回的字段呢,答案是Source字段,通过setFetchSource函数来指定要返回的字段,可以指定单个filed或者一个filed数组,通过getSource来获取返回的字段值,其中setFetchSource有三个版本,说明如下:
1. setFetchSource(boolean fetch)
用于指定是否返回字段,默认为true,如果为false,则getSource返回null。
2. setFetchSource(String include, String exclude)
用于指定包含的一个字段和排除的一个字段,实际会返回include字段及其对应的值。
3. setFetchSource(String[] includes, String[] excludes)
用于指定包含的多个字段和排除的多个字段。
下面以具体代码来说明如何获取:
String index = xxx;
String type = xxx;
// 指定要返回的字段
String[] fields = new String[2];
fields[0] = "field1"; // 字段1名称
fields[1] = "filed2"; // 字段2名称
// 构造query
SearchRequestBuilder requestBuilder = client.prepareSearch(index);
requestBuilder .setFrom(0).setSize(100)
.setTimeout(TimeValue.timeValueMillis(300))
.setTypes(type)
// 指定返回的字段,排除字段设为null
.setFetchSource(fields, null);
// bool 条件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
TermQueryBuilder tqb = QueryBuilders.termQuery("field1", "val1");
boolQueryBuilder.must(tqb);
// set query
requestBuilder.setQuery(boolQueryBuilder);
// get response
SearchResponse response = requestBuilder.execute().actionGet();
// 遍历返回的字段
SearchHits searchHits = response.getHits();
for (SearchHit hit : searchHits) {
// 注意这里和2.x不同的是使用getSource函数
System.out.println(hit.getSource().get("filed1"));
}