ElasticSearch—通过Elasticsearch RestFul搜索查询

时间:2022-11-17 13:35:00
ElasticSearch—通过Elasticsearch RestFul搜索查询

       ElasticSearch在执行CRUD操作时,可以通过调用上层的java api,也可以通过 Jest Restful的方式进行CRUD操作。通过Jest Restful的方式获得的结果一般为com.google.gson.JsonArray,需要对该Json数组进行解析,获取所需的内容,下面给出一个分页查询的demo。


import java.io.IOException;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import edu.fzu.cmp.common.exception.MetaManagerException;
import edu.fzu.cmp.common.exception.QueryException;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import junit.framework.TestCase;

public class EsQueryByRestful extends TestCase {

private static String connUrl = "http://belasticsearch1:9200";
private static String index = "contentmanagerdata_index";
private static String indexType = "contentmanagerdata_type";
private static SearchResult result = null;

@Test
public void testEsRestful() throws QueryException, MetaManagerException, IOException {
// 1. connect
JestClient client = getClient();
// 2. prepare query param
String queryParamJson = buildQueryParamByAPI();
// 3. search
result = search(client, queryParamJson);
// 4. get response
getContent();

}

public static JestClient getClient() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig.Builder(connUrl).multiThreaded(true).build());
JestClient client = factory.getObject();
return client;
}

/**利用from-size的方式进行分页查询, 可以通过searchSourceBuilder.query(queryBuilder)的方式设置查询条件*/
public static String buildQueryParamByAPI() throws QueryException, MetaManagerException {
int pageNumber = 1;
int pageSize = 11;
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.from((pageNumber - 1) * pageSize);// 设置起始页
searchSourceBuilder.size(pageSize);// 设置页大小
return searchSourceBuilder.toString();
}

public static String buildQueryParamByStr() {
String queryParamJson = "{\"query\" : {\"match\" : {\"empname\" : \"emp2\"}}}";
return queryParamJson;
}

public static SearchResult search(JestClient client, String queryParamJson) throws IOException {
Search search = new Search.Builder(queryParamJson)
// multiple index or types can be added.
.addIndex(index).addType(indexType).build();
// JestResult result = client.execute(search);
SearchResult result = client.execute(search);
return result;
}

public static void getContent() {

// 自动解析
JsonObject jsonObject = result.getJsonObject();
JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
long took = jsonObject.get("took").getAsLong(); //统计耗时
long total = hitsobject.get("total").getAsLong();
System.out.println("took:" + took + " " + "total:" + total);;
JsonArray recordObject = hitsobject.getAsJsonArray("hits");
/**通过下面的代码解析JsonArray的内容*/
System.out.println(recordObject.size());
System.out.println(recordObject.get(0).toString().split(",")[2]);
}

// public static Map<String, String> generateQueryCondtion() {
// Map<String, String> queryCondition = new HashMap<>();
// int ra1 = (int) (Math.random() * 10);
// int ra2 = (int) (Math.random() * 3);
// queryCondition.put("MAINRISKNAME", ra2 + "");
// queryCondition.put("INSURANCENO", "O" + String.valueOf((ra1 + 1) * 3));
// queryCondition.put("POLICYHOLDERNAME", ra2 + "");
// return queryCondition;
// }

}

运行结果:

ElasticSearch—通过Elasticsearch RestFul搜索查询

ES索引库中的已有记录数:

ElasticSearch—通过Elasticsearch RestFul搜索查询