Kaynağa Gözat

es 基本操作1.0 排序问题

yegang 2 yıl önce
ebeveyn
işleme
3db2d937a2

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

@@ -4,6 +4,10 @@ import lombok.AllArgsConstructor;
 import lombok.Data;
 import lombok.NoArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.annotation.Id;
+import org.springframework.data.elasticsearch.annotations.Document;
+import org.springframework.data.elasticsearch.annotations.Field;
+import org.springframework.data.elasticsearch.annotations.FieldType;
 
 /**
  * 商品
@@ -14,9 +18,19 @@ import org.springframework.beans.factory.annotation.Autowired;
 @Data
 @AllArgsConstructor
 @NoArgsConstructor
+@Document(indexName = "jd_list")
 public class EsProduct {
+    @Id  //主键
     private Integer id;
+    //FieldType.Auto根据字段类型自动注入,例如这里是String
+    @Field(type = FieldType.Auto)
     private String name;
+    //FieldType.Auto根据字段类型自动注入,例如这里是String
+    @Field(type = FieldType.Auto)
     private String url;
+    @Field(type = FieldType.Auto)
     private String price;
+
+    @Field(type = FieldType.Integer,index = false)
+    private Integer num;
 }

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

@@ -11,6 +11,7 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.Random;
 
 /**
  * @author yegang
@@ -31,6 +32,7 @@ public class ParseHtml {
             esProduct.setName(pname);
             esProduct.setPrice(price);
             esProduct.setUrl(img);
+            esProduct.setNum(new Random().nextInt(1000));
             list.add(esProduct);
         }
         return  list;

+ 18 - 9
src/test/java/com/sw/EsDocTests.java

@@ -11,6 +11,7 @@ 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.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.action.update.UpdateRequest;
 import org.elasticsearch.client.RequestOptions;
@@ -19,6 +20,7 @@ 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.BoolQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.search.SearchHit;
@@ -26,6 +28,8 @@ 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.SortBuilder;
+import org.elasticsearch.search.sort.SortBuilders;
 import org.elasticsearch.search.sort.SortOrder;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -60,7 +64,7 @@ public class EsDocTests {
     @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");
+        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",100);
         IndexRequest indexRequest = new IndexRequest("yg");
         //设置请求规则,设置请求id
         indexRequest.id("1");
@@ -81,7 +85,7 @@ public class EsDocTests {
      */
     public  void putbatchTest() throws IOException {
         //创建数据对象
-        List<EsProduct> list = ParseHtml.parse("c语言");
+        List<EsProduct> list = ParseHtml.parse("大数据");
         BulkRequest bulkRequest = new BulkRequest();
         bulkRequest.timeout("2m");
         for(EsProduct esProduct :list){
@@ -108,7 +112,7 @@ public class EsDocTests {
      */
     @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");
+        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",100);
         UpdateRequest updateRequest = new UpdateRequest("yg","1");
         updateRequest.timeout("2s");
         updateRequest.doc(JSON.toJSONString(esProduct),XContentType.JSON);
@@ -120,7 +124,7 @@ public class EsDocTests {
      */
     @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");
+        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",200);
         DeleteRequest deleteRequest = new DeleteRequest("yg","1");
         deleteRequest.timeout("2s");
         client.delete(deleteRequest,RequestOptions.DEFAULT);
@@ -150,9 +154,13 @@ public class EsDocTests {
     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);
+        String[] includes  =new String[4];
+        includes[0]="name";
+        includes[1]="price";
+        includes[2]="url";
+        includes[3]="num";
+        searchSourceBuilder.fetchSource(includes, null);//过滤为null数据
+        searchSourceBuilder.sort("num",SortOrder.DESC);
         //模糊查询
         QueryBuilder queryBuilder = QueryBuilders.termQuery("name","java");
         searchSourceBuilder.query(queryBuilder);
@@ -166,9 +174,9 @@ public class EsDocTests {
         //设置超时
         searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
         //设置起始位置
-        searchSourceBuilder.from(20);
+        searchSourceBuilder.from(1);
         //设置页大小
-        searchSourceBuilder.size(15);
+        searchSourceBuilder.size(30);
         //绑定build
         searchRequest.source(searchSourceBuilder);
         //执行搜索
@@ -194,5 +202,6 @@ public class EsDocTests {
         }
 
     }
+
 }