浏览代码

文件库

yp 3 年之前
父节点
当前提交
112c4b83c2

+ 51 - 3
nngkxxdp/src/main/java/com/example/nngkxxdp/elk/ElkController.java

@@ -1,11 +1,12 @@
 package com.example.nngkxxdp.elk;
 
 import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import org.elasticsearch.action.search.SearchRequest;
+import org.elasticsearch.action.search.SearchRequestBuilder;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms;
+import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.client.RequestOptions;
 import org.elasticsearch.client.RestHighLevelClient;
@@ -14,7 +15,10 @@ import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.script.Script;
 import org.elasticsearch.search.SearchHit;
+import org.elasticsearch.search.aggregations.*;
+import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
 import org.elasticsearch.search.builder.SearchSourceBuilder;
+import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
 import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
 import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
 import org.elasticsearch.search.sort.ScriptSortBuilder;
@@ -416,6 +420,7 @@ public class ElkController {
             type = 0;
         }
         BoolQueryBuilder boolQueryBuilder3 = new BoolQueryBuilder();
+        // 默认全部文件 查询所有
         if (type == 0) {
             List<QueryBuilder> should1 = boolQueryBuilder3.should();
             should1.add(QueryBuilders.matchQuery(articleType, 1));
@@ -455,20 +460,38 @@ public class ElkController {
             }
             searchSourceBuilder.highlighter(hb);
         }
+        // 分组查询 根据主题类型分类
+        TermsAggregationBuilder aggregation = AggregationBuilders.terms("childtype")
+                //聚合字段名
+                .field("childtype.keyword")
+                .size(300)
+                // 降序
+                .order(BucketOrder.count(false));
         // 排序
         searchSourceBuilder.sort("_score", SortOrder.DESC);
         searchSourceBuilder.sort(pubTimeStr, SortOrder.DESC);
+
         // 开始位置
         searchSourceBuilder.from((page - 1) * limit);
         // 查询条数
         searchSourceBuilder.size(limit);
+        searchSourceBuilder.aggregation(aggregation);
         searchRequest.source(searchSourceBuilder);
         System.out.println(searchSourceBuilder);
         // 执行搜索
         SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
         SearchHit[] s = searchResponse.getHits().getHits();
+        // 查询分组信息
+        Terms terms = searchResponse.getAggregations().get("childtype");
+        List<? extends Bucket> buckets = terms.getBuckets();
+        Map<Object, Object> groupInfo = new HashMap<>();
+        for(Bucket bt : buckets) {
+            groupInfo.put(bt.getKey(), bt.getDocCount());
+            System.err.println(bt.getKey() + " " + bt.getDocCount());
+        }
         JSONArray array = new JSONArray();
         JSONArray highlightArr = new JSONArray();
+
         for (int i = 0; i < s.length; i++) {
             Map<String, HighlightField> hig = s[i].getHighlightFields();
             JSONObject jsonObject = JSONObject.parseObject(s[i].toString());
@@ -483,6 +506,7 @@ public class ElkController {
         Map<String, Object> map = new HashMap<>();
         map.put("data", array);
         map.put("highlightArr", highlightArr);
+        map.put("groupInfo", groupInfo);
         return SendUtil.send(true, null, map);
     }
 
@@ -535,4 +559,28 @@ public class ElkController {
     public Map<String, Object> getChildTypeCount(String typeName) {
         return SendUtil.send(true, null, elkService.getChildTypeCount(typeName));
     }
+
+//    public Map<String, Long> getTypeGroupBy() throws IOException {
+//        Map<String, Long> map = new LinkedHashMap<>();
+//        SearchRequest searchRequest = new SearchRequest();
+//        searchRequest.indices("record_traffic*");
+//        //指定分组字段,terms指定别名,field指定字段名
+//        TermsAggregationBuilder aggregation = AggregationBuilders.terms("childtype")
+//                //聚合字段名
+//                .field("childtype.keyword")
+//                .size(100)
+//                // 降序
+//                .order(BucketOrder.count(false));
+//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
+//        searchSourceBuilder.aggregation(aggregation);
+//        //执行查询
+//        searchRequest.source(searchSourceBuilder);
+//        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
+//        Terms byAgeAggregation = response.getAggregations().get("content");
+//        List<? extends Terms.Bucket> buckets = byAgeAggregation.getBuckets();
+//        for (Terms.Bucket buck: buckets) {
+//            map.put(buck.getKeyAsString(), buck.getDocCount());
+//        }
+//        return map;
+//    }
 }

+ 8 - 8
nngkxxdp/src/main/java/com/example/nngkxxdp/service/impl/ElkServiceImpl.java

@@ -84,14 +84,14 @@ public class ElkServiceImpl implements ElkService {
         List<Map<String, Object>> firstChildType = elkDao.getFirstChildType();
         if (Blank.isNotEmpty(firstChildType)) {
             for (Map<String, Object> first : firstChildType) {
-                int count = 0;
-                String title = (String) first.get("title");
-                if (title.indexOf("\\") != -1) {
-                    title = title.replace("\\", "\\");
-                }
-                count = elkDao.getChildTypeCount1(title);
-                title += "("+count+")";
-                first.put("title", title);
+//                int count = 0;
+//                String title = (String) first.get("title");
+//                if (title.indexOf("\\") != -1) {
+//                    title = title.replace("\\", "\\");
+//                }
+//                count = elkDao.getChildTypeCount1(title);
+//                title += "("+count+")";
+//                first.put("title", title);
                 List children = new ArrayList();
                 List<Map<String, Object>> secondChildType = elkDao.getSecondChildType((Integer) first.get("id"));
                 if (Blank.isNotEmpty(secondChildType)) {

+ 1 - 1
nngkxxdp/src/main/resources/static/zdjc/sywj11.html

@@ -390,7 +390,6 @@
 
 				$.ajax({
 					type:'get',
-
 					url: '/elk/getPubs',
 					success:function(obj) {
 						console.log(obj)
@@ -420,6 +419,7 @@
 				}
 				$('.all').val(org);
 				page = 1;
+				sessionStorage.ES_BIAOSHI = "biaoshi"
 				window.location = 'wj2.html';
 			}
 

+ 91 - 28
nngkxxdp/src/main/resources/static/zdjc/wj2.html

@@ -78,7 +78,7 @@
 			<div class="text_four">
 				<div class="title_three">
 					<div class="treeti" >
-						<div class="ti1"></div>
+						<div class="ti1">按主题分类</div>
 						<div id="wjtree"></div>
 					</div>
 					<div class="layui-tab layui-tab-brief tright" lay-filter="docDemoTabBrief">
@@ -147,11 +147,11 @@
 			layui.use('tree', function(){
 				var tree = layui.tree;
 				//渲染
+
 				$.get("/elk/getChildType", function (data) {//请求数据
 					var inst1 = tree.render({
 						elem: '#wjtree',  //绑定元素
 						data: data.data,
-
 						click: function(obj){
 							page = 1;
 							if (!$('#search-input').val()) {
@@ -159,20 +159,25 @@
 							}
 							// $('.layui-tree-entry').find('.layui-tree-txt').removeClass('ly-color');
 							$('.layui-tree-set').find('.layui-tree-txt').removeClass('ly-color');
-							console.log($(obj.elem))
+							// console.log($(obj.elem))
 							if ($(obj.elem).find('.layui-tree-icon').length > 0) {
 								$(obj.elem).children(":first").find('.layui-tree-txt').addClass('ly-color')
 							} else {
 								$(obj.elem).find('.layui-tree-txt').addClass('ly-color')
 							}
-
+							var flag = false;
+							if (obj.data.children && obj.data.children.length > 0) {
+								flag = true;
+							}
+							// console.log(data.data)
 							// console.log(obj.data); //得到当前点击的节点数据
 							var articleType = $($("#articleType").find(".layui-this")[0]).attr("data-type");
 							// console.log($($("#articleType").find(".layui-this")[0]).attr("data-type"))
-							initPage(articleType, obj.data.title);
+							initPage(articleType, obj.data.title, 1);
 						},
 						accordion: true,//手风琴模式
 					});
+					initPage("","","",data.data);
 				});
 			});
 			layui.use(['form', 'jquery'], function() {
@@ -199,14 +204,15 @@
 					$(".tangchu").css("display","none");
 				});
 				$('.ss-icon').on('click', function() {
-					 if (!$('#search-input').val()) {
-						layer.msg('请输入检索信息');
-						return;
+					if (!$('#search-input').val()) {
+						// layer.msg('请输入检索信息');
+						$('.ly-color').text($('.ly-color').text())
+						// return;
 					}
 					page = 1;
 					sessionStorage.ES_SEARCH = $('#search-input').val();
 					var articleType = $($("#articleType").find(".layui-this")[0]).attr("data-type")
-					initPage(articleType, $('.ly-color').text());
+					initPage(articleType, $('.ly-color').text(),2);
 				});
 				$("#articleType li").on('click', function (){
 					page = 1;
@@ -258,18 +264,22 @@
 					}
 				})
 
+				// initPage();
 				initDetail();
-				initPage();
-				getChildTypeCount();
-				// hotSearch();
+
 			});
 
 			var page = 1;
 			var dataMap = {};
-			function initPage(type, childType) {
-
-
+			function initPage(type, childType, changeCount, treeData) {
 				var index = layer.load(2);
+				if (childType && childType.indexOf("(") != -1) {
+					childType = childType.split("(")[0];
+				}
+				if (changeCount === 2){
+					childType = '';
+				}
+				console.log(treeData)
 				$.ajax({
 					type:'get',
 					url: '/elk/retrieval',
@@ -283,6 +293,7 @@
 					},
 					dataType: 'json',
 					success:function(obj) {
+
 					  if (page == 1) {
 						 $('.more').removeClass('layui-hide');
 						 $('.wrap_text').empty();
@@ -294,7 +305,50 @@
 			        	  }
 			        	  page++;
 			        	  var data = datas.data;
+						  var groupInfo = datas.groupInfo;
+
+						  var total = 0;
+						  for(var key in groupInfo) {
+							  var value1 = groupInfo[key];
+							  if (key === '') {
+								  value1 = 0;
+							  }else {
+								  total += value1
+							  }
+						  }
+						  // if (treeData) {
+							//   for (let i = 0; i < treeData.length; i++) {
+							// 	  if () {
+							//
+							// 	  }
+							//   }
+						  // }
+						  $("#wjtree").find(".layui-tree-txt").each(function (){
+						  	// 判断如果是点击主题分类栏查询就不改变原有数量统计
+						  	if (changeCount === 1) {
+								$(this).text($(this).text())
+								$(".ti1").text($(".ti1").text())
+							}else {
+								// console.log("===>"+$(this).text())
+								if ($(this).text().indexOf("(") != -1) {
+									if ($(this).children)
+									console.log($(this).data)
+									// $(this).text(($(this).text()).split["("][0])
+									var aa = $(this).text()
+									var cc = aa.split("(")[0]
+									$(this).text(cc)
+								}
+								var value = groupInfo[$(this).text()]
+								if (!value) {
+									value = 0;
+								}
+								$(this).text($(this).text()+"("+value+")")
+								$(".ti1").text("主题分类("+total+")")
+							}
+						  })
+
 			        	  var html = '';
+
 			        	  for (var i = 0; i < data.length; i++) {
 			        	  	// console.log(data[i])
 							  var content = data[i]['_source']['content'];
@@ -335,19 +389,28 @@
 					}
 				})
 			}
-			function getChildTypeCount(){
-				$.ajax({
-					type:'get',
-					url: '/elk/getChildTypeCount',
-					success:function(obj) {
-						console.log(obj)
-						$(".ti1").text("按主题分类("+obj.data+")")
-					},
-					error: function(msg) {
-						console.log(msg)
-					}
-				})
-			}
+			// var groupArr;
+			// function getChildTypeCount(){
+			// 	$.ajax({
+			// 		type:'get',
+			// 		url: '/elk/getChildType',
+			// 		success:function(obj) {
+			// 			if (obj.data) {
+			// 				var data = obj.data;
+			// 				for (let i = 0; i < data.length; i++) {
+			// 					groupArr.push(data[i].title);
+			// 					if (data[i].children) {
+			// 						groupArr.push(data[i].children[i].title);
+			// 					}
+			// 				}
+			// 			}
+			// 		},
+			// 		error: function(msg) {
+			// 			console.log(msg)
+			// 		}
+			// 	})
+			// 	console.log(groupArr)
+			// }
 			// 初始化详情页面
 			function initDetail() {