|
@@ -0,0 +1,198 @@
|
|
|
|
+package com.sw;
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
|
+import com.sw.domain.SysTest;
|
|
|
|
+import com.sw.domain.po.EsProduct;
|
|
|
|
+import com.sw.util.ParseHtml;
|
|
|
|
+import org.elasticsearch.action.bulk.BulkRequest;
|
|
|
|
+import org.elasticsearch.action.delete.DeleteRequest;
|
|
|
|
+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.update.UpdateRequest;
|
|
|
|
+import org.elasticsearch.client.RequestOptions;
|
|
|
|
+import org.elasticsearch.client.RestHighLevelClient;
|
|
|
|
+import org.elasticsearch.common.document.DocumentField;
|
|
|
|
+import org.elasticsearch.common.text.Text;
|
|
|
|
+import org.elasticsearch.common.xcontent.XContentType;
|
|
|
|
+import org.elasticsearch.core.TimeValue;
|
|
|
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
|
+import org.elasticsearch.search.SearchHit;
|
|
|
|
+import org.elasticsearch.search.SearchHits;
|
|
|
|
+import org.elasticsearch.search.builder.SearchSourceBuilder;
|
|
|
|
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
|
|
|
+import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
|
|
|
|
+import org.elasticsearch.search.sort.SortOrder;
|
|
|
|
+import org.junit.Test;
|
|
|
|
+import org.junit.runner.RunWith;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
+import org.springframework.boot.test.context.SpringBootTest;
|
|
|
|
+import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
|
|
|
|
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|
|
|
+
|
|
|
|
+import java.io.IOException;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author yegang
|
|
|
|
+ * @create 2022-03-09 14:21
|
|
|
|
+ **/
|
|
|
|
+@SpringBootTest
|
|
|
|
+@RunWith(SpringJUnit4ClassRunner.class)
|
|
|
|
+public class EsDocTests {
|
|
|
|
+ @Autowired
|
|
|
|
+ private ElasticsearchRestTemplate elasticsearchRestTemplate;
|
|
|
|
+ @Autowired
|
|
|
|
+ @Qualifier("restHighLevelClient")
|
|
|
|
+ private RestHighLevelClient client;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加文档
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void putTest() throws IOException {
|
|
|
|
+ //创建数据对象
|
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","99.8");
|
|
|
|
+ IndexRequest indexRequest = new IndexRequest("yg");
|
|
|
|
+ //设置请求规则,设置请求id
|
|
|
|
+ indexRequest.id("1");
|
|
|
|
+ //设置请求规则,设置请求超时时间
|
|
|
|
+ indexRequest.timeout(TimeValue.timeValueSeconds(1));
|
|
|
|
+ indexRequest.timeout("1s");
|
|
|
|
+ //请求绑定数据 以json格式
|
|
|
|
+ indexRequest.source(JSON.toJSONString(esProduct), XContentType.JSON);
|
|
|
|
+ //执行请求
|
|
|
|
+ IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
|
|
|
|
+ System.out.println(index.toString());
|
|
|
|
+ System.out.println(index.status());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ /**
|
|
|
|
+ * 批量添加
|
|
|
|
+ */
|
|
|
|
+ public void putbatchTest() throws IOException {
|
|
|
|
+ //创建数据对象
|
|
|
|
+ List<EsProduct> list = ParseHtml.parse("c语言");
|
|
|
|
+ BulkRequest bulkRequest = new BulkRequest();
|
|
|
|
+ bulkRequest.timeout("2m");
|
|
|
|
+ for(EsProduct esProduct :list){
|
|
|
|
+ bulkRequest.add(new IndexRequest("jd_list").source(JSON.toJSONString(esProduct), XContentType.JSON));
|
|
|
|
+ }
|
|
|
|
+ client.bulk(bulkRequest,RequestOptions.DEFAULT);
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 获取文档
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void getTest() throws IOException {
|
|
|
|
+ GetRequest getRequest = new GetRequest("yg","1");
|
|
|
|
+ boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
|
|
|
|
+ if(exists){
|
|
|
|
+ GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT);
|
|
|
|
+ Map<String, Object> source = documentFields.getSource();
|
|
|
|
+ System.out.println(source);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新文档
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void updateTest() throws IOException {
|
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8");
|
|
|
|
+ UpdateRequest updateRequest = new UpdateRequest("yg","1");
|
|
|
|
+ updateRequest.timeout("2s");
|
|
|
|
+ updateRequest.doc(JSON.toJSONString(esProduct),XContentType.JSON);
|
|
|
|
+ client.update(updateRequest,RequestOptions.DEFAULT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 更新文档
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void delTest() throws IOException {
|
|
|
|
+ EsProduct esProduct = new EsProduct(1,"水浒传","https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimgservice.suning.cn%2Fuimg1%2Fb2c%2Fimage%2Fxqvo6GQUdf3Qi65VvhzkrQ.png_800w_800h_4e&refer=http%3A%2F%2Fimgservice.suning.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1649469488&t=95064a70d6145bbf6f1615b1806709ad","100.8");
|
|
|
|
+ DeleteRequest deleteRequest = new DeleteRequest("yg","1");
|
|
|
|
+ deleteRequest.timeout("2s");
|
|
|
|
+ client.delete(deleteRequest,RequestOptions.DEFAULT);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查询
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void searchTest() throws IOException {
|
|
|
|
+ SearchRequest searchRequest = new SearchRequest("jd_list");
|
|
|
|
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
|
+ QueryBuilder queryBuilder = QueryBuilders.termQuery("name","JavaScript高级程序设计 第4版(图灵出品) web前端开发教程,JS'红宝书'升级,入门+实战,涵盖ECMAScript,2019,提供教学视频+配套编程环境,可直接在线运行随书代码");
|
|
|
|
+ searchSourceBuilder.query(queryBuilder);
|
|
|
|
+ searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
|
|
|
+ searchSourceBuilder.from(1);
|
|
|
|
+ SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
+ SearchHits hits = search.getHits();
|
|
|
|
+ System.out.println(JSON.toJSONString(hits));
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ /**
|
|
|
|
+ * 查询
|
|
|
|
+ */
|
|
|
|
+ @Test
|
|
|
|
+ public void searchTest2() throws IOException {
|
|
|
|
+ SearchRequest searchRequest = new SearchRequest("jd_list");
|
|
|
|
+ SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
|
|
|
|
+
|
|
|
|
+// searchSourceBuilder.fetchSource("name", null);//过滤为null数据
|
|
|
|
+// searchSourceBuilder.sort("price", SortOrder.ASC);
|
|
|
|
+ //模糊查询
|
|
|
|
+ QueryBuilder queryBuilder = QueryBuilders.termQuery("name","java");
|
|
|
|
+ searchSourceBuilder.query(queryBuilder);
|
|
|
|
+ //设置高亮
|
|
|
|
+ HighlightBuilder highlightBuilder = new HighlightBuilder();
|
|
|
|
+ highlightBuilder.field("name");
|
|
|
|
+ highlightBuilder.postTags("</span>");
|
|
|
|
+ highlightBuilder.preTags("<span style='color:red'>");
|
|
|
|
+ highlightBuilder.requireFieldMatch(false);
|
|
|
|
+ searchSourceBuilder.highlighter(highlightBuilder);
|
|
|
|
+ //设置超时
|
|
|
|
+ searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
|
|
|
|
+ //设置起始位置
|
|
|
|
+ searchSourceBuilder.from(20);
|
|
|
|
+ //设置页大小
|
|
|
|
+ searchSourceBuilder.size(15);
|
|
|
|
+ //绑定build
|
|
|
|
+ searchRequest.source(searchSourceBuilder);
|
|
|
|
+ //执行搜索
|
|
|
|
+ SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
|
|
|
|
+ //遍历hits
|
|
|
|
+ for (SearchHit hit : searchResponse.getHits().getHits()) {
|
|
|
|
+ Map<String, Object> sourceAsMap = hit.getSourceAsMap();
|
|
|
|
+ //获取高亮字段
|
|
|
|
+ Map<String, HighlightField> highlightFields = hit.getHighlightFields();
|
|
|
|
+ //获取name字段的高亮信息
|
|
|
|
+ HighlightField highlightField = highlightFields.get("name");
|
|
|
|
+
|
|
|
|
+ if(highlightField !=null){
|
|
|
|
+ Text[] fragments = highlightField.getFragments();
|
|
|
|
+ String n_name ="";
|
|
|
|
+ for (Text fragment : fragments) {
|
|
|
|
+ n_name+=fragment;
|
|
|
|
+ }
|
|
|
|
+ //置换
|
|
|
|
+ sourceAsMap.put("name",n_name);
|
|
|
|
+ }
|
|
|
|
+ System.out.println(sourceAsMap);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|