Browse Source

批次搜索统计品牌、类目、货源等信息时,统计各条目下批次的数量

sunyj 8 years ago
parent
commit
42b2cd469a
1 changed files with 51 additions and 48 deletions
  1. 51 48
      src/main/java/com/uas/search/grouping/GoodsGroupCollector.java

+ 51 - 48
src/main/java/com/uas/search/grouping/GoodsGroupCollector.java

@@ -1,74 +1,77 @@
 package com.uas.search.grouping;
 package com.uas.search.grouping;
 
 
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-
+import com.alibaba.druid.util.StringUtils;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.index.LeafReader;
 import org.apache.lucene.index.LeafReader;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.index.LeafReaderContext;
 import org.apache.lucene.search.SimpleCollector;
 import org.apache.lucene.search.SimpleCollector;
 
 
-import com.alibaba.druid.util.StringUtils;
+import java.io.IOException;
+import java.util.*;
 
 
 /**
 /**
  * 批次统计
  * 批次统计
- * 
+ *
  * @author sunyj
  * @author sunyj
  * @since 2017年7月12日 下午2:12:56
  * @since 2017年7月12日 下午2:12:56
  */
  */
 public class GoodsGroupCollector extends SimpleCollector {
 public class GoodsGroupCollector extends SimpleCollector {
 
 
-	private Set<String> fieldsToLoad = new HashSet<>();
+    private Set<String> fieldsToLoad = new HashSet<>();
 
 
-	private Set<String> uniqueValues = new HashSet<>();
+    private List<String> uniqueValues = new ArrayList<>();
 
 
-	private String uniqueField;
+    private String uniqueField;
 
 
-	private LeafReader reader;
+    private LeafReader reader;
 
 
-	private List<Map<String, Object>> values = new ArrayList<>();
+    private List<Map<String, Object>> values = new ArrayList<>();
 
 
-	public GoodsGroupCollector(String uniqueField, Set<String> fieldsToLoad) {
-		this.uniqueField = uniqueField;
-		this.fieldsToLoad.addAll(fieldsToLoad);
-	}
+    public GoodsGroupCollector(String uniqueField, Set<String> fieldsToLoad) {
+        this.uniqueField = uniqueField;
+        this.fieldsToLoad.addAll(fieldsToLoad);
+    }
 
 
-	@Override
-	public boolean needsScores() {
-		return false;
-	}
+    @Override
+    public boolean needsScores() {
+        return false;
+    }
 
 
-	@Override
-	protected void doSetNextReader(LeafReaderContext context) throws IOException {
-		super.doSetNextReader(context);
-		reader = context.reader();
-	}
+    @Override
+    protected void doSetNextReader(LeafReaderContext context) throws IOException {
+        super.doSetNextReader(context);
+        reader = context.reader();
+    }
 
 
-	@Override
-	public void collect(int doc) throws IOException {
-		if (null != reader) {
-			Map<String, Object> map = new HashMap<>();
-			Document document = reader.document(doc, fieldsToLoad);
-			String uniqueValue = document.get(uniqueField);
-			// 排除重复值
-			if (!StringUtils.isEmpty(uniqueValue) && !uniqueValues.contains(uniqueValue)) {
-				uniqueValues.add(uniqueValue);
-				for (String field : fieldsToLoad) {
-					String value = document.get(field);
-					map.put(field, value);
-				}
-				values.add(map);
-			}
-		}
-	}
+    @Override
+    public void collect(int doc) throws IOException {
+        if (null != reader) {
+            Document document = reader.document(doc);
+            String uniqueValue = document.get(uniqueField);
+            // 排除重复值
+            if (!StringUtils.isEmpty(uniqueValue)) {
+                if (!uniqueValues.contains(uniqueValue)) {
+                    Map<String, Object> map = new HashMap<>();
+                    uniqueValues.add(uniqueValue);
+                    for (String field : fieldsToLoad) {
+                        String value = document.get(field);
+                        map.put(field, value);
+                    }
+                    map.put("count", 1L);
+                    values.add(map);
+                } else {
+                    // 统计数目
+                    int index = uniqueValues.indexOf(uniqueValue);
+                    Map<String, Object> map = values.get(index);
+                    Long count = (Long) map.get("count");
+                    map.put("count", count + 1);
+                }
+            }
+        }
+    }
 
 
-	public List<Map<String, Object>> getValues() {
-		return values;
-	}
+    public List<Map<String, Object>> getValues() {
+        return values;
+    }
 
 
 }
 }