pom.xml 增加依赖, 包含坐标转换api
<!-- elasticsearch的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.9.0</version>
</dependency>
<!-- elasticsearch依赖2.x的log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.11.1</version>
</dependency>
<!--坐标转换 -->
<dependency>
<groupId>com.vividsolutions</groupId>
<artifactId>jts</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>org.osgeo</groupId>
<artifactId>proj4j</artifactId>
<version>0.1.0</version>
</dependency>
java 关键字查询
private List<JSONObject> ESMtchSingleAddress(String address, int maxLocations) throws IOException {
// 获取客户端
RestHighLevelClient client = getClient();
SearchTemplateRequest request = new SearchTemplateRequest();
// 指定索引
request.setRequest(new SearchRequest("mpoi"));
// 设置为内联
request.setScriptType(ScriptType.INLINE);
// 设置脚本
request.setScript(
"{\"query\": " + "{\"multi_match\": {\r\n" + "\"query\": \"{{address}}\",\r\n"
+ " \"analyzer\": \"ik_smart\"\r\n" + " }" + " },\r\n"
+ " \"from\": 0,\r\n" + " \"size\": {{maxLocations}}\r\n" + "}");
Map<String, Object> scriptParams = new HashMap<>();
scriptParams.put("address", address);
scriptParams.put("maxLocations", maxLocations);
request.setScriptParams(scriptParams);
List<JSONObject> searchResult = new ArrayList();
SearchTemplateResponse searchTemplateResponse = null;
try {
// 进行查询
searchTemplateResponse = client.searchTemplate(request, RequestOptions.DEFAULT);
SearchResponse searchResponse = searchTemplateResponse.getResponse();
for (SearchHit hit : searchResponse.getHits()) {
if (hit != null) {
searchResult.add(new JSONObject(hit.getSourceAsString()).put("score", hit.getScore()));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭客户端
close(client);
}
return searchResult;
}
/**
* 获取ES客户端
*
* @return
*/
private static RestHighLevelClient getClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("192.168.1.111", 9200, "http")));
return client;
}