内容简介:不得不说,使用Elasticsearch的官方客户端在多个版本的时候很容易出现一些意想不到的问题,然而通过RESTful的方式可以尽可能的兼容多个版本的情况。下面我们通过Jest对Elasticsearch进行操作,这里使用的Jest版本是下面我们导入常用的包:
不得不说,使用Elasticsearch的官方客户端在多个版本的时候很容易出现一些意想不到的问题,然而通过RESTful的方式可以尽可能的兼容多个版本的情况。
下面我们通过Jest对Elasticsearch进行操作,这里使用的Jest版本是 6.3.1
,而Elasticsearch版本是 6.3.1
。对应的依赖可以 访问
进行下载。
下面我们导入常用的包:
import java.util.*; import java.io.IOException; import com.google.gson.*; import io.searchbox.client.*; import io.searchbox.core.*; import io.searchbox.core.SearchResult.Hit; import io.searchbox.client.config.*;
接着我们定义1个客户端类,用于连接对应的客户端连接:
class ESClient {
private static final String ES_HOST = "http://127.0.0.1";
private static final int ES_HTTP_PORT = 9200;
private static JestClient client;
private static JestClient build(){
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(ES_HOST+":"+ES_HTTP_PORT)
.multiThreaded(true) //设置为多线程
.defaultMaxTotalConnectionPerRoute(2)
.maxTotalConnection(2) //最大连接数
.connTimeout(10000) //设置连接超时
.readTimeout(10000) //设置读取超时
.gson(new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create()) //设置日期的格式
.build());
client = factory.getObject();
return client;
}
public static synchronized JestClient getClient(){
if(client==null){
client = build();
}
return client;
}
}
这里我们通过 JestClientFactory
的客户端工厂类的 getObject
方法得到对应的对象,并将其返回。
之后我们的调用的代码:
public class demo {
public static void main(String[] args){
JestClient client = new ESClient().getClient();
//构建JSON字符串
HashMap<String, Object> query_args = new HashMap<>();
HashMap<String, Object> match = new HashMap<>();
HashMap<String, Object> match_all = new HashMap<>();
match.put("match_all", match_all);
query_args.put("query", match);
GsonBuilder gsonBuilder = new GsonBuilder();
String query = gsonBuilder.serializeNulls().create().toJson(query_args);
//进行RESTful查询
Search.Builder searchBuilder = new Search.Builder(query).addIndex("demo").addType("person");
try{
SearchResult result = client.execute(searchBuilder.build());
List<Hit<JsonObject, Void>> hits = result.getHits(JsonObject.class);
for(Hit<JsonObject, Void> hit: hits){
JsonObject source = hit.source;
JsonPrimitive username = source.getAsJsonPrimitive("username");
JsonArray tags = source.getAsJsonArray("tags");
System.out.println(username);
System.out.println(tags);
}
} catch (IOException e){
e.printStackTrace();
e.getMessage();
}
}
}
在这里,我们通过HashMap组装JSON对象,并通过之前介绍的Gson对其进行序列化为字符串,于是我们得到类似如下的字符串:
{"query":{"match_all":{}}}
接着,我们通过 Search.Builder
中传入1个字符串,然后通过 addIndex
指定我们要查询的index名称,还有对应的 doc_type
的名称。当我们执行ESClient客户端的execute提交后得到查询的结果。
而对应的结果类似如下组成的数组:
{"username":"王五","gender":"男","age":"21","region":"杭州","tags":["吃货","技术大牛"],"es_metadata_id":"1g3l82UB8TY7aIKswO1s"}
此时,我们通过搜索结果实例的getHits方法获取到对应的查询命中结果。在这里,我们在getHits方法中传入Gson中的JsonObject类。
最后,我们进行遍历,对于基础数据类型,我们需要设置其为 JsonPrimitive
类型,而对于数组则设置为 JsonArray
,同理对象或哈希表为 JsonObject
。这样我们就得到了对应的 Java 对象,进行组合并序列化后就可以返回到前端。
在这里,如果要获取记录的总条数,可以通过如下的2种方法来获取,首先最简单的方法是:
long total = result.getTotal();
我们直接调用搜索结果的 getTotal
方法即可。而第2种方法需要自行对JSON对象进行解析后才可以获取:
JsonObject hits = result.getJsonObject().getAsJsonObject("hits");
int total = hits.get("total").getAsInt();
同样的,我们还有最大分数的获取,可以通过类似的方法来获得:
float score = result.getMaxScore();
下面我们进行手动解析的过程如下:
JsonObject hits = result.getJsonObject().getAsJsonObject("hits");
if(hits!=null){
JsonArray response = hits.getAsJsonArray("hits");
for(JsonElement resp: response){
JsonObject source = new Gson().fromJson(resp, JsonObject.class).getAsJsonObject("_source");
JsonPrimitive username = source.getAsJsonPrimitive("username");
JsonArray tags = source.getAsJsonArray("tags");
System.out.println(username);
System.out.println(tags);
}
}
可以看到这种方法,相比第1种方法需要编写更多的代码,反而显得不够简洁,不过对于理解其过程很有帮助。
以上所述就是小编给大家介绍的《使用Jest查询Elasticsearch数据》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Head First HTML5 Programming
Eric Freeman、Elisabeth Robson / O'Reilly Media / 2011-10-18 / USD 49.99
What can HTML5 do for you? If you're a web developer looking to use this new version of HTML, you might be wondering how much has really changed. Head First HTML5 Programming introduces the key featur......一起来看看 《Head First HTML5 Programming》 这本书的介绍吧!