确认已经装好了Elasticsearch【Elasticsearch安装教程 - 博客园】
参考教程参考自 关于在Java中简单的将Mysql中的数据添加到es中并且定时同步更新 - CSDN
引入依赖参考教程 Java中ElasticSearch的各种查询(普通,模糊,前缀,高亮,聚合,范围) - CSDN
pom依赖代码注意!!!
elasticsearch.version 要和自己安装的版本协同
可能遇到的问题7.15.2 com.alibaba fastjson 1.2.36 org.springframework.boot spring-boot-starter-data-elasticsearch org.elasticsearch.client elasticsearch-rest-high-level-client 7.15.2 org.elasticsearch elasticsearch org.elasticsearch.client elasticsearch-rest-client org.elasticsearch.client elasticsearch-rest-client 7.15.2 org.elasticsearch elasticsearch 7.15.2 org.elasticsearch.client elasticsearch-rest-client 7.15.2 commons-logging commons-logging
-
fastjson 的版本报红,不下载。
可能是这个 fastjson 版本和 pom 文件的别的设置冲突了,改一下 fastjson 版本就行
-
pom 文件更新的时候报错 Connot connect
谁知道这是什么问题啊【应该是我手贱又下载了个JDK导致的自动更新】可能是 jdk 环境被自动改了。File – Setting – Maven – Importing – 查看 JDK for Inporting 的设置是否正确
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// Elasticsearch的配置类【配置es链接】
@Configuration
public class ElasticSearchClientConfig
{
// 这一段感觉应该是初始化了一个 RequestOptions 变量
public static final RequestOptions COMMON_OPTIONS;
static
{
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public static RestHighLevelClient restHighLevelClient()
{
// 集群配置法
RestHighLevelClient client = new RestHighLevelClient
(RestClient.builder(new HttpHost("127.0.0.1", 9200, "http")));
return client;
}
}
将数据库的数据同步到ES
import com.alibaba.fastjson.JSONObject;
import com.placename.general.config.ElasticSearchClientConfig;
import com.placename.general.mapper.PogOfficialInfoMapper;
import com.placename.general.model.domain.PogOfficialInfo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@EnableScheduling // 定时器
@Component
public class ElasticsearchImpl
{
@Autowired
@Resource
// 这个是要被同步的数据表所对应的Mapper
private PogOfficialInfoMapper pogOfficialInfoMapper;
// 定时器注解,每过去5秒执行这个方法
@Scheduled(cron = "0/5 * * * * ?")
public void Escalculating()
{
// 查询 OfficialInfo 表中的所有数据,待会这些数据全部放在 es 里
List list = pogOfficialInfoMapper.getall();
// 调用高层对象
// ElasticSearchClientConfig 为之前写的 Elasticsearch 配置类,restHighLevelClient 是其中的静态方法
RestHighLevelClient client = ElasticSearchClientConfig.restHighLevelClient();
// 存储刚刚 list 里获得的对象
for(PogOfficialInfo temp:list)
{
// 创建构造器指定index索引
IndexRequest indexRequest = new IndexRequest("officialinfo"); // 索引的名字
indexRequest.id(temp.getId().toString());
// 创建批量操作的对象
BulkRequest request = new BulkRequest();
// 将查询到的数据转化为Json
indexRequest.source(JSONObject.toJSONString(temp), XContentType.JSON);
// Json数据放入批量操作对象中
request.add(indexRequest);
// 执行操作
try
{
client.bulk(request,ElasticSearchClientConfig.COMMON_OPTIONS);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(temp);
}
}
}
Elasticsearch查询示例
GET officialinfo/_search
{
"query":{
"match": {
"content": {
"query": "疫情 核酸",
"operator": "or"
}
}
}
}
使用Elasticsearch进行查询
测试类测试
import com.placename.general.model.domain.PogOfficialInfo;
import org.apache.http.HttpHost;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.Operator;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SpringBootTest
public class ElasticsearchApplicationTest
{
// 连接客户端
@Autowired
RestHighLevelClient client;
@Test
public void testClient() throws IOException
{
// 初始化客户端
// 正常操作应该是把这个方法加上 @Before 注解实现比较好
System.out.println(client);
HttpHost serverHost = new HttpHost("localhost",9200);
client = new RestHighLevelClient(RestClient.builder(serverHost));
// 模糊查询
// 创建查询request
// officialinfo 为索引名称
SearchRequest request = new SearchRequest("officialinfo");
// 这个request.type() 可能因为 Elasticsearch 版本的问题,已经不用了,如果用的话,会报错
// 【[types removal] Specifying types in search requests is deprecated."]】
// request.types("text");
// 指定查询条件
SearchSourceBuilder builder = new SearchSourceBuilder();
// 选择模糊查询匹配的模式是 and 还是 or
// 也可以不加后面的 .operator(Operator.OR) ,如果不加,就是直接匹配
builder.query(QueryBuilders.matchQuery("content","防疫 核酸").operator(Operator.OR));
request.source(builder);
// 执行查询
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
// 结果打印【为什么用 Hit ,需要去看 Elasticsearch 查询的结果,看结果很容易就明白了】
for(SearchHit hit:response.getHits().getHits())
{
System.out.println(hit.getSourceAsMap());
}
// 销毁客户端 【同上,理论上应该用 @After 注解实现】
if(client == null)
{
try
{
client.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
详细的Elasticsearch的使用我将会再写一个教程,如果有需要,请到我的博客中去寻找阿巴阿巴,这个可以当作初版,或者说代码版



