当前位置:   article > 正文

Java使用elasticsearch-rest-high-level-client操作EleasticSearch_elasticsearch-rest-high-level-client配置

elasticsearch-rest-high-level-client配置

1、创建maven项目,pom.xml文件中增加如下配置

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.elasticsearch</groupId>
  4. <artifactId>elasticsearch</artifactId>
  5. <version>6.2.1</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.elasticsearch.client</groupId>
  9. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  10. <version>6.2.1</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.12</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.alibaba</groupId>
  19. <artifactId>fastjson</artifactId>
  20. <version>1.2.44</version>
  21. </dependency>
  22. </dependencies>

2、测试代码如下:

  1. package com.xuan.test;
  2. import com.alibaba.fastjson.JSON;
  3. import org.apache.http.HttpHost;
  4. import org.elasticsearch.action.DocWriteResponse;
  5. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  6. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  7. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  8. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  9. import org.elasticsearch.action.delete.DeleteRequest;
  10. import org.elasticsearch.action.delete.DeleteResponse;
  11. import org.elasticsearch.action.get.GetRequest;
  12. import org.elasticsearch.action.get.GetResponse;
  13. import org.elasticsearch.action.index.IndexRequest;
  14. import org.elasticsearch.action.index.IndexResponse;
  15. import org.elasticsearch.action.search.SearchRequest;
  16. import org.elasticsearch.action.search.SearchResponse;
  17. import org.elasticsearch.action.update.UpdateRequest;
  18. import org.elasticsearch.action.update.UpdateResponse;
  19. import org.elasticsearch.client.RestClient;
  20. import org.elasticsearch.client.RestClientBuilder;
  21. import org.elasticsearch.client.RestHighLevelClient;
  22. import org.elasticsearch.common.text.Text;
  23. import org.elasticsearch.common.xcontent.XContentType;
  24. import org.elasticsearch.index.query.*;
  25. import org.elasticsearch.rest.RestStatus;
  26. import org.elasticsearch.search.SearchHit;
  27. import org.elasticsearch.search.SearchHits;
  28. import org.elasticsearch.search.builder.SearchSourceBuilder;
  29. import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
  30. import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
  31. import org.elasticsearch.search.sort.FieldSortBuilder;
  32. import org.elasticsearch.search.sort.SortOrder;
  33. import org.junit.Before;
  34. import org.junit.Test;
  35. import java.io.IOException;
  36. import java.util.Arrays;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. /**
  41. * FileName: TestES
  42. * Author: xuan zongjun
  43. * Date: 2020/5/11 10:20
  44. * Description:
  45. */
  46. public class TestES {
  47. private RestHighLevelClient client = null;
  48. @Before
  49. public void prepare() {
  50. // 创建Client连接对象
  51. String[] ips = {"127.0.0.1:9200"};
  52. HttpHost[] httpHosts = new HttpHost[ips.length];
  53. for (int i = 0; i < ips.length; i++) {
  54. httpHosts[i] = HttpHost.create( ips[i] );
  55. }
  56. RestClientBuilder builder = RestClient.builder( httpHosts );
  57. client = new RestHighLevelClient( builder );
  58. }
  59. /**
  60. * 创建索引 和 映射
  61. *
  62. * @throws IOException
  63. */
  64. @Test
  65. public void test1() throws IOException {
  66. //创建名称为blog2的索
  67. CreateIndexRequest request = new CreateIndexRequest( "blog1" );
  68. //设置映射 doc type名称
  69. request.mapping( "doc", " {\n" +
  70. " \t\"properties\": {\n" +
  71. " \"name\": {\n" +
  72. " \"type\": \"text\",\n" +
  73. " \"analyzer\":\"ik_max_word\",\n" +
  74. " \"search_analyzer\":\"ik_smart\"\n" +
  75. " },\n" +
  76. " \"description\": {\n" +
  77. " \"type\": \"text\",\n" +
  78. " \"analyzer\":\"ik_max_word\",\n" +
  79. " \"search_analyzer\":\"ik_smart\"\n" +
  80. " },\n" +
  81. " \"studymodel\": {\n" +
  82. " \"type\": \"keyword\"\n" +
  83. " },\n" +
  84. " \"price\": {\n" +
  85. " \"type\": \"float\"\n" +
  86. " }\n" +
  87. " }\n" +
  88. "}", XContentType.JSON );
  89. CreateIndexResponse createIndexResponse = client.indices().create( request );
  90. System.out.println( JSON.toJSONString( createIndexResponse ) );
  91. //释放资源
  92. client.close();
  93. }
  94. /**
  95. * 删除索引
  96. */
  97. //删除索引库
  98. @Test
  99. public void testDeleteIndex() throws IOException {
  100. //删除索引请求对象
  101. DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest( "blog2" );
  102. //删除索引
  103. DeleteIndexResponse deleteIndexResponse = client.indices().delete( deleteIndexRequest );
  104. //删除索引响应结果
  105. boolean acknowledged = deleteIndexResponse.isAcknowledged();
  106. System.out.println( acknowledged );
  107. }
  108. /**
  109. * 插入文档
  110. */
  111. @Test
  112. public void testAddDocument() throws IOException {
  113. //准备json数据
  114. Map<String, Object> jsonMap = new HashMap<>();
  115. jsonMap.put( "name", "11spring cloud实战" );
  116. jsonMap.put( "description", "11本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud基础入门 3.实战Spring Boot 4.注册中心eureka。" );
  117. jsonMap.put( "studymodel", "201001" );
  118. jsonMap.put( "price", 5.6f );
  119. //索引请求对象
  120. IndexRequest indexRequest = new IndexRequest( "blog1", "doc" ).id( "11" );
  121. //指定索引文档内容
  122. indexRequest.source( jsonMap );
  123. //索引响应对象
  124. IndexResponse index = client.index( indexRequest );
  125. //获取响应结果
  126. DocWriteResponse.Result result = index.getResult();
  127. System.out.println( result );
  128. }
  129. /**
  130. * 查询文档 根据ID查询
  131. */
  132. @Test
  133. public void getDocumentById() throws IOException {
  134. GetRequest getRequest = new GetRequest( "blog1", "doc", "11" );
  135. GetResponse response = client.get( getRequest );
  136. boolean exists = response.isExists();
  137. Map<String, Object> sourceAsMap = response.getSourceAsMap();
  138. System.out.println( sourceAsMap );
  139. }
  140. /**
  141. * 更新文档
  142. */
  143. @Test
  144. public void updateDocument() throws IOException {
  145. UpdateRequest updateRequest = new UpdateRequest( "blog1", "doc", "11" );
  146. Map<String, String> map = new HashMap<>();
  147. map.put( "name", "spring cloud实战222" );
  148. updateRequest.doc( map );
  149. UpdateResponse update = client.update( updateRequest );
  150. RestStatus status = update.status();
  151. System.out.println( status );
  152. }
  153. /**
  154. * 删除文档
  155. */
  156. @Test
  157. public void deleteDocument() throws IOException {
  158. String id = "AXIDNJoC19Z06cvw7_Gv";
  159. DeleteRequest deleteRequest = new DeleteRequest( "blog2", "doc", id );
  160. DeleteResponse delete = client.delete( deleteRequest );
  161. System.out.println( delete.status() );
  162. }
  163. /**
  164. * 搜索管理 查询所有文档
  165. */
  166. @Test
  167. public void testSearchAll() throws IOException {
  168. SearchRequest searchRequest = new SearchRequest( "blog1" );
  169. searchRequest.types( "doc" );
  170. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  171. searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
  172. //source源字段过虑
  173. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  174. searchRequest.source( searchSourceBuilder );
  175. SearchResponse searchResponse = client.search( searchRequest );
  176. SearchHits hits = searchResponse.getHits();
  177. long totalHits = hits.getTotalHits();
  178. System.out.println( "total=" + totalHits );
  179. SearchHit[] searchHits = hits.getHits();
  180. for (SearchHit searchHit : searchHits) {
  181. String index = searchHit.getIndex();
  182. System.out.println( "index=" + index );
  183. String id = searchHit.getId();
  184. System.out.println( "id=" + id );
  185. String sourceAsString = searchHit.getSourceAsString();
  186. System.out.println( sourceAsString );
  187. Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
  188. //String articleId = String.valueOf( sourceAsMap.get( "id" ) );
  189. String title = (String) sourceAsMap.get( "name" );
  190. String content = (String) sourceAsMap.get( "description" );
  191. //System.out.println("articleId="+articleId);
  192. System.out.println( "title=" + title );
  193. System.out.println( "content=" + content );
  194. }
  195. }
  196. /**
  197. * 搜索管理 分页查询
  198. */
  199. @Test
  200. public void testSearchByPage() throws IOException {
  201. //设置要查询的索引 和 type
  202. SearchRequest searchRequest = new SearchRequest( "blog1" );
  203. searchRequest.types( "doc" );
  204. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  205. searchSourceBuilder.query( QueryBuilders.matchAllQuery() );
  206. //分页查询,设置起始下标,从0开始
  207. searchSourceBuilder.from( 0 );
  208. //每页显示个数
  209. searchSourceBuilder.size( 2 );
  210. //source源字段过虑
  211. //searchSourceBuilder.fetchSource(new String[]{"title","id","content"},new String[]{} );
  212. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  213. searchRequest.source( searchSourceBuilder );
  214. SearchResponse search = client.search( searchRequest );
  215. SearchHits hits = search.getHits();
  216. long totalHits = hits.getTotalHits();
  217. System.out.println( totalHits );
  218. for (SearchHit searchHit : hits.getHits()) {
  219. String sourceAsString = searchHit.getSourceAsString();
  220. System.out.println( sourceAsString );
  221. }
  222. }
  223. /**
  224. * 搜索管理 Term Query精确查找 ,在搜索时会整体匹配关键字,不再将关键字分词 ,
  225. * 下面的语句:查询title 包含 spring 的文档
  226. */
  227. @Test
  228. public void testSearchTerm() throws IOException {
  229. //创建查询,设置索引
  230. SearchRequest searchRequest = new SearchRequest( "blog1" );
  231. //设置type
  232. searchRequest.types( "doc" );
  233. //设置查询条件
  234. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  235. searchSourceBuilder.query( QueryBuilders.termQuery( "name", "spring" ) );
  236. //source源字段过虑
  237. //searchSourceBuilder.fetchSource(new String[]{"title","id","content"},new String[]{});
  238. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  239. searchRequest.source( searchSourceBuilder );
  240. SearchResponse search = client.search( searchRequest );
  241. SearchHits hits = search.getHits();
  242. long totalHits = hits.getTotalHits();
  243. System.out.println( "总条数:" + totalHits );
  244. for (SearchHit searchHit : hits.getHits()) {
  245. String sourceAsString = searchHit.getSourceAsString();
  246. System.out.println( sourceAsString );
  247. }
  248. }
  249. /**
  250. * 搜索管理 根据ID查询
  251. */
  252. @Test
  253. public void testSearchByID() throws IOException {
  254. //创建查询,设置索引
  255. SearchRequest searchRequest = new SearchRequest( "blog1" );
  256. //设置type
  257. searchRequest.types( "doc" );
  258. //设置查询条件
  259. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  260. String[] ids = {"11", "12"};
  261. List<String> stringList = Arrays.asList( ids );
  262. searchSourceBuilder.query( QueryBuilders.termsQuery( "_id", stringList ) );
  263. //source源字段过虑
  264. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  265. searchRequest.source( searchSourceBuilder );
  266. SearchResponse search = client.search( searchRequest );
  267. SearchHits hits = search.getHits();
  268. long totalHits = hits.getTotalHits();
  269. System.out.println( "总条数:" + totalHits );
  270. for (SearchHit searchHit : hits.getHits()) {
  271. String sourceAsString = searchHit.getSourceAsString();
  272. System.out.println( sourceAsString );
  273. }
  274. }
  275. /**
  276. * 搜索管理 match query 先分词后查找
  277. */
  278. @Test
  279. public void testSearchMatch() throws IOException {
  280. //创建查询,设置索引
  281. SearchRequest searchRequest = new SearchRequest( "blog1" );
  282. //设置type
  283. searchRequest.types( "doc" );
  284. //设置查询条件
  285. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  286. //匹配关键字
  287. searchSourceBuilder.query( QueryBuilders.matchQuery( "name", "spring开发" ).operator( Operator.OR ) );
  288. //source源字段过虑
  289. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  290. searchRequest.source( searchSourceBuilder );
  291. SearchResponse search = client.search( searchRequest );
  292. SearchHits hits = search.getHits();
  293. long totalHits = hits.getTotalHits();
  294. System.out.println( "总条数:" + totalHits );
  295. for (SearchHit searchHit : hits.getHits()) {
  296. String sourceAsString = searchHit.getSourceAsString();
  297. System.out.println( sourceAsString );
  298. }
  299. }
  300. /**
  301. * 搜索管理 match query 先分词后查找 minimum_should_match (如果实现三个词至少有两个词匹配如何实现)
  302. */
  303. @Test
  304. public void testSearchMatchMinimnum() throws IOException {
  305. //创建查询,设置索引
  306. SearchRequest searchRequest = new SearchRequest( "blog1" );
  307. //设置type
  308. searchRequest.types( "doc" );
  309. //设置查询条件
  310. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  311. //匹配关键字
  312. searchSourceBuilder.query( QueryBuilders.matchQuery( "name", "spring开发" ).minimumShouldMatch( "60%" ) );
  313. //source源字段过虑
  314. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  315. searchRequest.source( searchSourceBuilder );
  316. SearchResponse search = client.search( searchRequest );
  317. SearchHits hits = search.getHits();
  318. long totalHits = hits.getTotalHits();
  319. System.out.println( "总条数:" + totalHits );
  320. for (SearchHit searchHit : hits.getHits()) {
  321. String sourceAsString = searchHit.getSourceAsString();
  322. System.out.println( sourceAsString );
  323. }
  324. }
  325. /**
  326. * 搜索管理 同时搜索多个Field
  327. */
  328. @Test
  329. public void testSearchMultiMatch() throws IOException {
  330. //创建查询,设置索引
  331. SearchRequest searchRequest = new SearchRequest( "blog1" );
  332. //设置type
  333. searchRequest.types( "doc" );
  334. //设置查询条件
  335. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  336. //匹配关键字
  337. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery( "Boot 开发", "name", "description" )
  338. .minimumShouldMatch( "50%" );
  339. multiMatchQueryBuilder.field( "name",10 );//提升boost 表示权重提升10
  340. searchSourceBuilder.query( multiMatchQueryBuilder );
  341. //source源字段过虑
  342. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  343. searchRequest.source( searchSourceBuilder );
  344. SearchResponse search = client.search( searchRequest );
  345. SearchHits hits = search.getHits();
  346. long totalHits = hits.getTotalHits();
  347. System.out.println( "总条数:" + totalHits );
  348. for (SearchHit searchHit : hits.getHits()) {
  349. String sourceAsString = searchHit.getSourceAsString();
  350. System.out.println( sourceAsString );
  351. }
  352. }
  353. /**
  354. * 搜索管理 布尔查询
  355. */
  356. @Test
  357. public void testSearchBoolean() throws IOException {
  358. //创建搜索请求对象
  359. SearchRequest searchRequest = new SearchRequest( "blog1" );
  360. //设置type
  361. searchRequest.types( "doc" );
  362. 创建搜索源配置对象
  363. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  364. //source源字段过虑
  365. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  366. //multiQuery
  367. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery( "Boot 开发", "name", "description" )
  368. .minimumShouldMatch( "50%" );
  369. multiMatchQueryBuilder.field( "name",10 );//提升boost 表示权重提升10
  370. //TermQuery
  371. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
  372. // 布尔查询
  373. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  374. boolQueryBuilder.must(multiMatchQueryBuilder);
  375. boolQueryBuilder.must(termQueryBuilder);
  376. //设置布尔查询对象
  377. searchSourceBuilder.query( boolQueryBuilder );
  378. //设置搜索源配置
  379. searchRequest.source( searchSourceBuilder );
  380. //搜索
  381. SearchResponse search = client.search( searchRequest );
  382. SearchHits hits = search.getHits();
  383. long totalHits = hits.getTotalHits();
  384. System.out.println( "总条数:" + totalHits );
  385. for (SearchHit searchHit : hits.getHits()) {
  386. String sourceAsString = searchHit.getSourceAsString();
  387. System.out.println( sourceAsString );
  388. }
  389. }
  390. /**
  391. * 搜索管理 过滤器
  392. */
  393. @Test
  394. public void testSearchFilter() throws IOException {
  395. //创建搜索请求对象
  396. SearchRequest searchRequest = new SearchRequest( "blog1" );
  397. //设置type
  398. searchRequest.types( "doc" );
  399. 创建搜索源配置对象
  400. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  401. //source源字段过虑
  402. searchSourceBuilder.fetchSource( new String[]{"name", "studymodel", "description"}, new String[]{} );
  403. //multiQuery 多field查询
  404. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery( "Boot 开发", "name", "description" )
  405. .minimumShouldMatch( "50%" );
  406. multiMatchQueryBuilder.field( "name",10 );//提升boost 表示权重提升10
  407. //多Field查询,添加到搜索源配置对象中
  408. searchSourceBuilder.query(multiMatchQueryBuilder);
  409. //TermQuery
  410. TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
  411. //Term查询,添加到搜索源配置对象中
  412. searchSourceBuilder.query( termQueryBuilder );
  413. // 布尔查询
  414. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  415. boolQueryBuilder.must(searchSourceBuilder.query());
  416. //过虑
  417. boolQueryBuilder.filter(QueryBuilders.termQuery( "studymodel","201001" ));
  418. boolQueryBuilder.filter(QueryBuilders.rangeQuery( "price" ).gte( 5 ).lte( 100 ));
  419. //设置布尔查询对象
  420. searchSourceBuilder.query( boolQueryBuilder );
  421. //设置搜索源配置
  422. searchRequest.source( searchSourceBuilder );
  423. //搜索
  424. SearchResponse search = client.search( searchRequest );
  425. SearchHits hits = search.getHits();
  426. long totalHits = hits.getTotalHits();
  427. System.out.println( "总条数:" + totalHits );
  428. for (SearchHit searchHit : hits.getHits()) {
  429. String sourceAsString = searchHit.getSourceAsString();
  430. System.out.println( sourceAsString );
  431. }
  432. }
  433. /**
  434. * 排序
  435. * @throws IOException
  436. */
  437. @Test
  438. public void testSort() throws IOException {
  439. SearchRequest searchRequest = new SearchRequest("blog1");
  440. searchRequest.types("doc");
  441. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  442. //source源字段过虑
  443. searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","description"},new String[]{});
  444. searchRequest.source(searchSourceBuilder);
  445. //布尔查询
  446. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  447. //过虑
  448. boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
  449. // 排序
  450. searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order( SortOrder.DESC));
  451. searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
  452. //搜索
  453. SearchResponse searchResponse = client.search(searchRequest);
  454. SearchHits hits = searchResponse.getHits();
  455. SearchHit[] searchHits = hits.getHits();
  456. for (SearchHit hit : searchHits) {
  457. String sourceAsString = hit.getSourceAsString();
  458. System.out.println(sourceAsString);
  459. }
  460. }
  461. /**
  462. * 高亮显示
  463. * @throws IOException
  464. */
  465. @Test
  466. public void testHighlight() throws IOException {
  467. SearchRequest searchRequest = new SearchRequest("blog1");
  468. searchRequest.types("doc");
  469. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  470. //source源字段过虑
  471. searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","description"},new String[]{});
  472. searchRequest.source(searchSourceBuilder);
  473. //匹配关键字
  474. MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("实战","name", "description");
  475. searchSourceBuilder.query(multiMatchQueryBuilder);
  476. //布尔查询
  477. BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
  478. boolQueryBuilder.must(searchSourceBuilder.query());
  479. //过虑
  480. boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
  481. //排序
  482. searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));
  483. searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
  484. //高亮设置
  485. HighlightBuilder highlightBuilder = new HighlightBuilder();
  486. highlightBuilder.preTags("<tag>");//设置前缀
  487. highlightBuilder.postTags("</tag>");//设置后缀
  488. // 设置高亮字段
  489. highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
  490. //highlightBuilder.fields().add(new HighlightBuilder.Field("description"));
  491. searchSourceBuilder.highlighter(highlightBuilder);
  492. SearchResponse searchResponse = client.search(searchRequest);
  493. SearchHits hits = searchResponse.getHits();
  494. SearchHit[] searchHits = hits.getHits();
  495. for (SearchHit hit : searchHits) {
  496. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  497. //名称
  498. String name = (String) sourceAsMap.get("name");
  499. //取出高亮字段内容
  500. Map<String, HighlightField> highlightFields = hit.getHighlightFields();
  501. if(highlightFields!=null){
  502. HighlightField nameField = highlightFields.get("name");
  503. if(nameField!=null){
  504. Text[] fragments = nameField.getFragments();
  505. StringBuffer stringBuffer = new StringBuffer();
  506. for (Text str : fragments) {
  507. stringBuffer.append(str.string());
  508. }
  509. name = stringBuffer.toString();
  510. }
  511. }
  512. System.out.println("高亮=="+name);
  513. String sourceAsString = hit.getSourceAsString();
  514. System.out.println(sourceAsString);
  515. }
  516. }
  517. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/199732
推荐阅读
相关标签
  

闽ICP备14008679号