文章目录
- 1.新建SpringBoot项目
- 依赖
- 2.实现
- 配置模块 config
- 控制层 controller
- 模型层 model
- 服务层 service
- 工具 util
- 主类
- 单元测试
1.新建SpringBoot项目
依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.lagou</groupId><artifactId>es-project</artifactId><version>0.0.1-SNAPSHOT</version><name>es-project</name><description>Demo project for Spring Boot</description><properties><elasticsearch.version>7.3.0</elasticsearch.version></properties><dependencies><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency><!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch --><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.3.0</version><exclusions><exclusion><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.3.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!-- HttpClient --><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.3</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.58</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!--devtools热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional><scope>true</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
配置文件:application.yml
spring:devtools:restart:enabled: true #设置开启热部署additional-paths: src/main/java #重启目录exclude: WEB-INF/**freemarker:cache: false #页面不加载缓存,修改即时生效elasticsearch:rest:uris: 127.0.0.1:9200cert:username: rootpassword: rootserver:port: 8083logging:level:root: infocom.xdclass.search: debug
2.实现
配置模块 config
EsConfig.java
package com.learn.es.config;import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class EsConfig {@Value("${spring.elasticsearch.rest.uris}")private String hostlist;@Value("${spring.elasticsearch.cert.username}")private String certUsername;@Value("${spring.elasticsearch.cert.password}")private String certPassword;@Beanpublic RestHighLevelClient client() {//解析hostlist配置信息String[] split = hostlist.split(",");//创建HttpHost数组,其中存放es主机和端口的配置信息HttpHost[] httpHostArray = new HttpHost[split.length];for(int i=0;i<split.length;i++){String item = split[i];System.out.println(item);httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");}//账户密码//用户认证对象BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();//设置账户密码credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(certUsername, certPassword));//创建rest client对象RestClientBuilder builder = RestClient.builder(httpHostArray).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {@Overridepublic HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);}});;//创建RestHighLevelClient客户端return new RestHighLevelClient(builder);}
}
控制层 controller
package com.learn.es.controller;import com.learn.es.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;import java.io.IOException;
import java.util.List;
import java.util.Map;@Controller
public class PositionController {@Autowiredprivate PositionService service;// 测试页面@GetMapping({"/","/index"})public String indexPage(){return "index";}@GetMapping("/search/{keyword}/{pageNo}/{pageSize}")@ResponseBodypublic List<Map<String,Object>> searchPosition(@PathVariable("keyword") String keyword,@PathVariable("pageNo")int pageNo,@PathVariable("pageSize")int pageSize)throws IOException{List<Map<String,Object>> list = service.searchPos(keyword, pageNo, pageSize);return list;}@RequestMapping("/importAll")@ResponseBodypublic String importAll(){try {service.importAll();} catch (IOException e) {e.printStackTrace();}return "success";}
}
模型层 model
package com.learn.es.model;import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.Date;@Data
@NoArgsConstructor
@AllArgsConstructor
public class Position {//主键private String id;//公司名称private String companyName;//职位名称private String positionName;//职位诱惑private String positionAdvantage;//薪资private String salary;//薪资下限private int salaryMin;//薪资上限private int salaryMax;//学历private String education;//工作年限private String workYear;//发布时间private String publishTime;//工作城市private String city;//工作地点private String workAddress;//创建时间@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")private Date createTime;//工作模式private String jobNature;
}
服务层 service
PositionService.java
package com.learn.es.service;import java.io.IOException;
import java.util.List;
import java.util.Map;public interface PositionService {/* 分页查询 */public List<Map<String,Object>> searchPos(String keyword,int pageNo,int pageSize)throws IOException;/**导入数据 */void importAll()throws IOException;
}
PositionServiceImpl.java
package com.learn.es.service.impl;import com.learn.es.service.PositionService;
import com.learn.es.util.DBHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.bulk.BackoffPolicy;
import org.elasticsearch.action.bulk.BulkProcessor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
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.common.unit.ByteSizeUnit;
import org.elasticsearch.common.unit.ByteSizeValue;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.Serializable;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.BiConsumer;@Service
public class PositionServiceImpl implements PositionService {private static final Logger logger = LogManager.getLogger(PositionServiceImpl.class);@Autowiredprivate RestHighLevelClient client;private static final String POSITION_INDEX = "position";public List<Map<String, Object>> search(String keyword, String matchField, int pageNo, int pageSize) throws IOException {if (pageNo <= 1){pageNo = 1;}// 搜索SearchRequest searchRequest = new SearchRequest(POSITION_INDEX);SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 分页设置searchSourceBuilder.from((pageNo-1)*pageSize);searchSourceBuilder.size(pageSize);QueryBuilder builder = QueryBuilders.matchQuery(matchField, keyword);searchSourceBuilder.query(builder);searchSourceBuilder.timeout(new TimeValue(60,TimeUnit.SECONDS));// 执行搜索searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);ArrayList<Map<String,Object>> list = new ArrayList<>();SearchHit[] hits = searchResponse.getHits().getHits();for (SearchHit hit:hits){list.add(hit.getSourceAsMap());}return list;}@Overridepublic List<Map<String, Object>> searchPos(String keyword, int pageNo, int pageSize) throws IOException {List<Map<String, Object>> list = search(keyword, "positionName", pageNo, pageSize);if (list.size() < 5){List<Map<String, Object>> list2 = search(keyword, "positionAdvantage", pageNo, pageSize);list.addAll(list2);}return list;}@Overridepublic void importAll() throws IOException {writeMySQLDataToES("position");}private void writeMySQLDataToES(String tableName){BulkProcessor bulkProcessor = getBulkProcessor(client);Connection connection = null;PreparedStatement ps = null;ResultSet rs = null;try {connection = DBHelper.getConn();logger.info("start handle data :" + tableName);String sql = "select * from " + tableName;ps = connection.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);// 根据自己需要设置 fetchSizeps.setFetchSize(20);rs = ps.executeQuery();ResultSetMetaData colData = rs.getMetaData();ArrayList<HashMap<String,String>> dataList = new ArrayList<>();HashMap<String,String> map = null;int count = 0;// c 就是列的名字 v 就是列对应的值String c = null;String v = null;while(rs.next()){count ++;map = new HashMap<String,String>(128);for (int i=1;i< colData.getColumnCount();i++){c = colData.getColumnName(i);v = rs.getString(c);map.put(c,v);}dataList.add(map);// 每1万条 写一次 不足的批次的数据 最后一次提交处理if (count % 10000 == 0){logger.info("mysql handle data number:"+count);// 将数据添加到 bulkProcessorfor (HashMap<String,String> hashMap2 : dataList){bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));}// 每提交一次 清空 map 和 dataListmap.clear();dataList.clear();}}// 处理 未提交的数据for (HashMap<String,String> hashMap2 : dataList){bulkProcessor.add(new IndexRequest(POSITION_INDEX).source(hashMap2));}bulkProcessor.flush();} catch (SQLException e) {e.printStackTrace();}finally {try {rs.close();ps.close();connection.close();boolean terinaFlag = bulkProcessor.awaitClose(150L,TimeUnit.SECONDS);logger.info(terinaFlag);} catch (Exception e) {e.printStackTrace();}}}private BulkProcessor getBulkProcessor(RestHighLevelClient client) {BulkProcessor bulkProcessor = null;try {BulkProcessor.Listener listener = new BulkProcessor.Listener() {@Overridepublic void beforeBulk(long executionId, BulkRequest request) {logger.info("Try to insert data number : "+ request.numberOfActions());}@Overridepublic void afterBulk(long executionId, BulkRequest request,BulkResponse response) {logger.info("************** Success insert data number : "+ request.numberOfActions() + " , id: " + executionId);}@Overridepublic void afterBulk(long executionId, BulkRequest request, Throwable failure) {logger.error("Bulk is unsuccess : " + failure + ", executionId: " + executionId);}};BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer = (request, bulkListener) -> client.bulkAsync(request, RequestOptions.DEFAULT, bulkListener);BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer, listener);builder.setBulkActions(5000);builder.setBulkSize(new ByteSizeValue(100L, ByteSizeUnit.MB));builder.setConcurrentRequests(10);builder.setFlushInterval(TimeValue.timeValueSeconds(100L));builder.setBackoffPolicy(BackoffPolicy.constantBackoff(TimeValue.timeValueSeconds(1L), 3));// 注意点:让参数设置生效bulkProcessor = builder.build();} catch (Exception e) {e.printStackTrace();try {bulkProcessor.awaitClose(100L, TimeUnit.SECONDS);} catch (Exception e1) {logger.error(e1.getMessage());}}return bulkProcessor;}
}
工具 util
package com.learn.es.util;import java.sql.Connection;
import java.sql.DriverManager;public class DBHelper {public static final String url = "jdbc:mysql://127.0.0.1:3306/lagou_position?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";public static final String name = "com.mysql.cj.jdbc.Driver";public static final String user = "root";public static final String password = "";private static Connection connection = null;public static Connection getConn(){try {Class.forName(name);connection = DriverManager.getConnection(url,user,password);}catch (Exception e){e.printStackTrace();}return connection;}
}
主类
package com.learn.es;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class EsBootApplication {public static void main(String[] args) {SpringApplication.run(EsBootApplication.class,args);}
}
单元测试
package com.learn.es;import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.io.IOException;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;@SpringBootTest
@RunWith(SpringRunner.class)
public class EsBootApplicationTest implements Serializable {@AutowiredRestHighLevelClient client;@Testpublic void testCreateIndex() throws Exception {//创建索引对象CreateIndexRequest createIndexRequest = newCreateIndexRequest("elasticsearch_test_wj");//设置参数createIndexRequest.settings(Settings.builder().put("number_of_shards", "1").put("number_of_replicas", "0"));// 指定映射XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("properties").startObject().field("studymodel").startObject().field("index","true").field("type", "keyword").endObject().field("name").startObject().field("index","true").field("type", "integer").endObject().field("description").startObject().field("index","true").field("type", "text").field("analyzer", "ik_max_word").endObject().field("pic").startObject().field("index","false").field("type", "text").endObject().endObject().endObject();createIndexRequest.mapping("doc", builder);/*指定映射createIndexRequest.mapping("doc"," {\n" +" \t\"properties\": {\n" +" \"studymodel\":{\n" +" \"type\":\"keyword\"\n" +" },\n" +" \"name\":{\n" +" \"type\":\"keyword\"\n" +" },\n" +" \"description\": {\n" +" \"type\": \"text\",\n" +" \"analyzer\":\"ik_max_word\" \n" +" },\n" +" \"pic\":{\n" +" \"type\":\"text\",\n" +" \"index\":false\n" +" }\n" +" \t}\n" +"}", XContentType.JSON);*///操作索引的客户端IndicesClient indices = client.indices();//执行创建索引库//CreateIndexResponse createIndexResponse =indices.create(createIndexRequest, RequestOptions.DEFAULT);CreateIndexResponse createIndexResponse =indices.create(createIndexRequest, RequestOptions.DEFAULT);//得到响应boolean acknowledged = createIndexResponse.isAcknowledged();System.out.println(acknowledged);}//删除索引库@Testpublic void testDeleteIndex() throws IOException {//删除索引的请求对象DeleteIndexRequest deleteIndexRequest = newDeleteIndexRequest("elasticsearch_test_wj");//操作索引的客户端IndicesClient indices = client.indices();//执行删除索引AcknowledgedResponse delete = indices.delete(deleteIndexRequest,RequestOptions.DEFAULT);//得到响应boolean acknowledged = delete.isAcknowledged();System.out.println(acknowledged);}//添加文档/*POST /elasticsearch_test/_doc/1{"name": "spring cloud实战",6) 其它搜索操作"description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。","studymodel":"201001","timestamp": "2020-08-22 20:09:18","price": 5.6}*/@Testpublic void testAddDoc() throws IOException {//创建索引请求对象IndexRequest indexRequest = newIndexRequest("elasticsearch_test", "doc");indexRequest.id("1");//文档内容 准备json数据Map<String, Object> jsonMap = new HashMap<>();jsonMap.put("name", "spring cloud实战");jsonMap.put("description", "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。");jsonMap.put("studymodel", "201001");SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");jsonMap.put("timestamp", dateFormat.format(new Date()));jsonMap.put("price", 5.6f);indexRequest.source(jsonMap);//通过client进行http的请求IndexResponse indexResponse =client.index(indexRequest, RequestOptions.DEFAULT);DocWriteResponse.Result result = indexResponse.getResult();System.out.println(result);}//查询文档@Testpublic void testGetDoc() throws IOException {//查询请求对象GetRequest getRequest = new GetRequest("elasticsearch_test", "1");GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);//得到文档的内容Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();System.out.println(sourceAsMap);}//搜索全部记录/*GET /elasticsearch_test/_search{"query":{"match_all":{}}}*/@Testpublic void testSearchAll() throws IOException, ParseException {//搜索请求对象SearchRequest searchRequest = new SearchRequest("elasticsearch_test");//搜索源构建对象SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//搜索方式//matchAllQuery搜索全部searchSourceBuilder.query(QueryBuilders.matchAllQuery());//设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});//向搜索请求对象中设置搜索源searchRequest.source(searchSourceBuilder);//执行搜索,向ES发起http请求SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);//搜索结果SearchHits hits = searchResponse.getHits();//匹配到的总记录数TotalHits totalHits = hits.getTotalHits();//得到匹配度高的文档SearchHit[] searchHits = hits.getHits();//日期格式化对象SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");for (SearchHit hit : searchHits) {//文档的主键String id = hit.getId();//源文档内容Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");//由于前边设置了源文档字段过虑,这时description是取不到的String description = (String) sourceAsMap.get("description");//学习模式String studymodel = (String) sourceAsMap.get("studymodel");//价格Double price = (Double) sourceAsMap.get("price");//日期Date timestamp = dateFormat.parse((String)sourceAsMap.get("timestamp"));System.out.println(name);System.out.println(studymodel);System.out.println(description);}}//分页查询@Testpublic void testSearchPage() throws IOException, ParseException {//搜索请求对象SearchRequest searchRequest = new SearchRequest("elasticsearch_test");//指定类型searchRequest.types("doc");//搜索源构建对象SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//设置分页参数//页码int page = 1;//每页记录数int size = 2;//计算出记录起始下标int from = (page - 1) * size;searchSourceBuilder.from(from);//起始记录下标,从0开始searchSourceBuilder.size(size);//每页显示的记录数//搜索方式//matchAllQuery搜索全部searchSourceBuilder.query(QueryBuilders.matchAllQuery());//设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});//向搜索请求对象中设置搜索源searchRequest.source(searchSourceBuilder);//执行搜索,向ES发起http请求SearchResponse searchResponse =client.search(searchRequest, RequestOptions.DEFAULT);//搜索结果SearchHits hits = searchResponse.getHits();//匹配到的总记录数TotalHits totalHits = hits.getTotalHits();//得到匹配度高的文档SearchHit[] searchHits = hits.getHits();//日期格式化对象SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");for (SearchHit hit : searchHits) {//文档的主键String id = hit.getId();//源文档内容Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");//由于前边设置了源文档字段过虑,这时description是取不到的String description = (String) sourceAsMap.get("description");//学习模式String studymodel = (String) sourceAsMap.get("studymodel");//价格Double price = (Double) sourceAsMap.get("price");//日期Date timestamp = dateFormat.parse((String)sourceAsMap.get("timestamp"));System.out.println(name);System.out.println(studymodel);System.out.println(description);}}//TermQuery@Testpublic void testTermQuery() throws IOException, ParseException {//搜索请求对象SearchRequest searchRequest = new SearchRequest("elasticsearch_test");//搜索源构建对象SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//搜索方式//termQuerysearchSourceBuilder.query(QueryBuilders.termQuery("name", "spring"));//设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});//向搜索请求对象中设置搜索源searchRequest.source(searchSourceBuilder);//执行搜索,向ES发起http请求SearchResponse searchResponse =client.search(searchRequest, RequestOptions.DEFAULT);//搜索结果SearchHits hits = searchResponse.getHits();//匹配到的总记录数TotalHits totalHits = hits.getTotalHits();//得到匹配度高的文档SearchHit[] searchHits = hits.getHits();//日期格式化对象SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");for (SearchHit hit : searchHits) {//文档的主键String id = hit.getId();//源文档内容Map<String, Object> sourceAsMap = hit.getSourceAsMap();String name = (String) sourceAsMap.get("name");//由于前边设置了源文档字段过虑,这时description是取不到的String description = (String) sourceAsMap.get("description");//学习模式String studymodel = (String) sourceAsMap.get("studymodel");//价格Double price = (Double) sourceAsMap.get("price");//日期Date timestamp = dateFormat.parse((String)sourceAsMap.get("timestamp"));System.out.println(name);System.out.println(studymodel);System.out.println(description);}}}