1.实现大数据量(大于千万)环境下,具有【多字段】、【快速】、【支持模糊匹配和拆词】的**检索功能的小程序。
2.数据环境:阿里云ES、阿里云MySQL
3.项目部署:阿里云服务器
首先:强调一下,一定要关注阿里云的ES文档!
连接阿里云文档
特别注意:
ES官方已不建议通过TransportClient来访问Elasticsearch,使用TransportClient 5.5.3版在建立连接时会报 NoNodeAvailableException 问题,并且ES官方已经不再维护TransportClient。如果已经使用并且有提示报错,可用以下方法绕过:
建议换成Java Low Level REST Client来访问Elasticsearch。
在Java maven项目的POM配置文件中,使用 x-pack-transport-5.3.3 版client进行访问,并且POM配置文件中的elasticsearch版本也必须为5.3.3版,不建议通过该方案来绕着实现,不保证能完全兼容。
温馨提示:
由于阿里云Elasticsearch实例使用5.5.3版本,所以需要您的JDK至少在 1.8 及以上版本。
更多语言demo,详情请求参见 HTTP/REST Clients and Security。
我就是因为本机测试安装的是最新版本ES6.3.1版本,应用TransportClient查询,结果给自己弄坑里了,报:no node available 别找我哈
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import java.io.IOException;
import java.util.Collections;
/**
*参数说明:
*:替换为访问阿里云ES实例对应用户名。
*:替换为访问阿里云ES实例指定用户对应密码。
*:替换为阿里云ES实例基本信息界面中的内网或外网地址。
*/
public class RestClientTest {
public static void main(String[]args){
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("" , "" ));
RestClient restClient = RestClient.builder(new HttpHost("" , 9200))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
try {
//index a document
HttpEntity entity = new NStringEntity("{\n\"user\" : \"kimchy\"\n}", ContentType.APPLICATION_JSON);
Response indexResponse = restClient.performRequest(
"PUT",
"/index/type/123",
Collections.emptyMap(),
entity);
//search a document
Response response = restClient.performRequest("GET", "/index/type/123",
Collections.singletonMap("pretty", "true"));
System.out.println(EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
应用代码
代码粘过去没用,仅供参考
@RequestMapping("/query")
public ServiceResult queryPage(@RequestParam("reqParam") String reqParam, @RequestParam("page") String page,@RequestParam("pageSize") String pageSize) {
ServiceResult result = new ServiceResult();
Map<String,Object> map = new HashMap<>();//用于封装返回结果
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"multi_match\" : {\n" +
" \"query\": \""+reqParam+"\", \n" +
" \"fields\": [ \"title\", \"applicantZh\" ,\"applicantEn\",\"number\"] \n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
try {
int from = (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize);
String endpoint = "/_search?size=" + pageSize + "&from=" + from;
Response response = esClient.performRequest("GET",
endpoint,
Collections.emptyMap(),
entity);
//TODO 针对response进行你需要的操作
return result;
}
应用示例(包含其他示例)
package com.fendo.RestClient;
import java.io.IOException;
import java.util.Collections;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Before;
import org.junit.Test;
/**
* Elasticserach RestClient示例
* @author fendo
*
*/
public class Rest {
private static RestClient restClient;
public void getRestClient(){
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "changeme"));
restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
}
@Before
public void getRest(){
restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
}
/**
* 查看api信息
* @throws Exception
*/
@Test
public void CatApi() throws Exception{
String method = "GET";
String endpoint = "/_cat";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 创建索引
* @throws Exception
*/
@Test
public void CreateIndex() throws Exception{
String method = "PUT";
String endpoint = "/test-index";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 创建文档
* @throws Exception
*/
@Test
public void CreateDocument()throws Exception{
String method = "PUT";
String endpoint = "/test-index/test/1";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 获取文档
* @throws Exception
*/
@Test
public void getDocument()throws Exception{
String method = "GET";
String endpoint = "/test-index/test/1";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 查询所有数据
* @throws Exception
*/
@Test
public void QueryAll() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 根据ID获取
* @throws Exception
*/
@Test
public void QueryByField() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"user\": \"kimchy\"\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 更新数据
* @throws Exception
*/
@Test
public void UpdateByScript() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/1/_update";
HttpEntity entity = new NStringEntity("{\n" +
" \"doc\": {\n" +
" \"user\":\"大美女\"\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void GeoBoundingBox() throws IOException {
String method = "POST";
String endpoint = "/attractions/restaurant/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" },\n" +
" \"post_filter\": {\n" +
" \"geo_bounding_box\": {\n" +
" \"location\": {\n" +
" \"top_left\": {\n" +
" \"lat\": 39.990481,\n" +
" \"lon\": 116.277144\n" +
" },\n" +
" \"bottom_right\": {\n" +
" \"lat\": 39.927323,\n" +
" \"lon\": 116.405638\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
ok,暂时这样吧,共同进步^_^