Browse Source

es 基本操作1.0

yegang 2 years ago
parent
commit
9edb7efd55

+ 15 - 0
pom.xml

@@ -17,6 +17,7 @@
     <description>口罩秒杀</description>
     <properties>
         <java.version>1.8</java.version>
+<!--        <elasticsearch.version>8.0.1</elasticsearch.version>-->
     </properties>
     <dependencies>
         <dependency>
@@ -93,6 +94,20 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-mongodb</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.75</version>
+        </dependency>
+        <dependency>
+            <groupId>org.jsoup</groupId>
+            <artifactId>jsoup</artifactId>
+            <version>1.11.2</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>

+ 27 - 0
src/main/java/com/sw/config/EsConfig.java

@@ -0,0 +1,27 @@
+package com.sw.config;
+
+import org.apache.http.HttpHost;
+import org.elasticsearch.client.Node;
+import org.elasticsearch.client.RestClient;
+import org.elasticsearch.client.RestClientBuilder;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import java.net.InetAddress;
+
+/**
+ * @author yegang
+ * @create 2022-03-09 15:09
+ * es 配置
+ **/
+@Configuration
+public class EsConfig {
+    @Bean
+    RestHighLevelClient restHighLevelClient (){
+        HttpHost httpHost = new HttpHost("192.168.20.85", 9200, "http");
+        RestClientBuilder restClientBuilder = RestClient.builder(new Node(httpHost));
+        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
+        return restHighLevelClient;
+    }
+}

+ 22 - 0
src/main/java/com/sw/domain/po/EsProduct.java

@@ -0,0 +1,22 @@
+package com.sw.domain.po;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * 商品
+ *
+ * @author yegang
+ * @create 2022-03-10 9:55
+ **/
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class EsProduct {
+    private Integer id;
+    private String name;
+    private String url;
+    private String price;
+}

+ 38 - 0
src/main/java/com/sw/util/ParseHtml.java

@@ -0,0 +1,38 @@
+package com.sw.util;
+
+import com.sw.domain.po.EsProduct;
+import org.jsoup.Connection;
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.nodes.Element;
+import org.jsoup.select.Elements;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * @author yegang
+ * @create 2022-03-10 10:03
+ **/
+public class ParseHtml {
+    public static List<EsProduct> parse(String name) throws IOException {
+        String url ="https://search.jd.com/Search?keyword="+name;
+        Document document = Jsoup.connect(url).get();
+        Element j_goodsList = document.body().getElementById("J_goodsList");
+        Elements elements = j_goodsList.getElementsByTag("li");
+        List<EsProduct> list = new ArrayList<>();
+        for (Element e :elements){
+            String img =e.getElementsByTag("img").eq(0).attr("data-lazy-img");
+            String price =e.getElementsByClass("p-price").text();
+            String pname =e.getElementsByClass("p-name").text();
+            EsProduct esProduct = new EsProduct();
+            esProduct.setName(pname);
+            esProduct.setPrice(price);
+            esProduct.setUrl(img);
+            list.add(esProduct);
+        }
+        return  list;
+    }
+}

+ 198 - 0
src/test/java/com/sw/EsDocTests.java

@@ -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);
+        }
+
+    }
+}
+

+ 53 - 0
src/test/java/com/sw/EsIndexTests.java

@@ -0,0 +1,53 @@
+package com.sw;
+
+import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
+import org.elasticsearch.action.support.master.AcknowledgedResponse;
+import org.elasticsearch.client.RequestOptions;
+import org.elasticsearch.client.RestHighLevelClient;
+import org.elasticsearch.client.indices.CreateIndexRequest;
+import org.elasticsearch.client.indices.CreateIndexResponse;
+import org.elasticsearch.client.indices.GetIndexRequest;
+import org.elasticsearch.client.indices.GetIndexResponse;
+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.data.elasticsearch.core.ElasticsearchRestTemplate;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+import java.io.IOException;
+
+/**
+ * @author yegang
+ * @create 2022-03-09 14:21
+ **/
+@SpringBootTest
+@RunWith(SpringJUnit4ClassRunner.class)
+public class EsIndexTests {
+    @Autowired
+    private ElasticsearchRestTemplate elasticsearchRestTemplate;
+    @Autowired
+    private RestHighLevelClient restHighLevelClient;
+    @Test
+    public void createIndex() throws IOException {
+        CreateIndexRequest indexRequest = new CreateIndexRequest("yg");
+        CreateIndexResponse index = restHighLevelClient.indices().create(indexRequest,RequestOptions.DEFAULT);
+        System.out.println(index);
+    }
+
+    @Test
+    public void getIndex() throws IOException {
+        GetIndexRequest getIndexRequest = new GetIndexRequest("yg");
+        boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
+        if(exists){
+            GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);
+            System.out.println(getIndexRequest);
+        }
+    }
+    @Test
+    public void delIndex() throws IOException {
+        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("yg");
+        AcknowledgedResponse delete = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
+        System.out.println(delete.isAcknowledged());
+    }
+}

+ 1 - 1
src/test/java/com/sw/ResourceCommentTests.java

@@ -87,7 +87,7 @@ public class ResourceCommentTests {
 		Example<ResourceComment> example = Example.of(rc);
 		Optional<ResourceComment> optional = resourceCommentRepository.findOne(example);
 		Assert.assertTrue(optional.isPresent());
-		Assert.assertEquals("user1", optional.get().getCommentUserName());
+		Assert.assertEquals("用户1", optional.get().getCommentUserName());
 	}