Search in sources :

Example 1 with Ratio

use of io.github.ihongs.dh.search.StatisGrader.Ratio in project HongsCORE by ihongs.

the class StatisHelper method amount.

/**
 * 分类计算
 *
 * <pre>
 * rd.rn 为统计结果长度
 * rd.rb 为要统计的字段
 * rd.ob 为要排序的字段
 * rd.ab 含 linked 时 rd.fn.rg 对应字段 fn 分块统计
 * rd.fn.rn 可单独指定字段统计长度
 * rd.fn.ar 可指定要统计的区间集合
 * rd.fn.nr 可指定要忽略的区间集合(只是不作统计, 并非查询约束)
 * </pre>
 *
 * @param rd
 * @return
 * @throws HongsException
 */
public Map amount(Map rd) throws HongsException {
    IndexSearcher finder = that.getFinder();
    Set<String> ab = Synt.toTerms(rd.get(Cnst.AB_KEY));
    Set<String> rb = Synt.toTerms(rd.get(Cnst.RB_KEY));
    Set<String> ob = Synt.toTerms(rd.get(Cnst.OB_KEY));
    boolean ln = ab != null && ab.contains("linked");
    int rl = rb != null ? rb.size() : 0;
    Map<String, Map<Range, Ratio>> counts = new HashMap(rl);
    // 排除
    Map<String, Set<Range>> countx = new HashMap(rl);
    Map<String, Map<Range, Ratio>> counts2 = new HashMap(rl);
    Map<String, Set<Range>> countx2 = new HashMap(rl);
    Map<String, Map<Range, Ratio>> counts3 = new HashMap(rl);
    Map<String, Set<Range>> countx3 = new HashMap(rl);
    if (rb != null && !rb.isEmpty())
        for (String k : rb) {
            Function f = getGraderFormat(k);
            if (null == f || f == asString) {
                throw new HongsException(400, "Field " + f + " is not exists or is not number");
            }
            Map vd = null;
            Set vs = null;
            Object vo = rd.get(k);
            if (vo instanceof Map) {
                Map vm = (Map) vo;
                try {
                    // 特选
                    vs = Synt.asSet(vm.get(Cnst.AR_KEY));
                    if (vs != null && !vs.isEmpty()) {
                        Map vz = new HashMap(vs.size());
                        for (Object v : vs) {
                            Range s = new Range(v);
                            Ratio r = new Ratio();
                            vz.put(s, r);
                        }
                        counts.put(k, vz);
                    }
                    // 排除
                    vs = Synt.asSet(vm.get(Cnst.NR_KEY));
                    if (vs != null && !vs.isEmpty()) {
                        Set vz = new HashSet(vs.size());
                        for (Object v : vs) {
                            Range s = new Range(v);
                            // Ratio  r = new Ratio( );
                            vz.add(s);
                        }
                        countx.put(k, vz);
                    }
                } catch (ClassCastException ex) {
                    // 区间格式不对
                    throw new HongsException(400, ex);
                }
                // 分块条件
                if (ln != true)
                    vs = null;
                else {
                    vs = Synt.asSet(vm.get(Cnst.RG_REL));
                    if (vs != null && !vs.isEmpty()) {
                        vd = new HashMap(rd);
                        vm = new HashMap(vm);
                        vm.remove(Cnst.RG_REL);
                        vd.put(k, vm);
                    }
                }
            }
            if (vs == null || vs.isEmpty()) {
                Map<Range, Ratio> vz = counts.get(k);
                if (vz != null)
                    counts2.put(k, vz);
                Set<Range> vx = countx.get(k);
                if (vz != null)
                    countx2.put(k, vx);
            } else {
                counts3.clear();
                countx3.clear();
                Map<Range, Ratio> vz = counts.get(k);
                if (vz != null)
                    counts3.put(k, vz);
                Set<Range> vx = countx.get(k);
                if (vz != null)
                    countx3.put(k, vx);
                amount(vd, finder, counts3, countx3);
            }
        }
    int n = amount(rd, finder, counts2, countx2);
    Map cnts = new HashMap();
    cnts.put("__count__", n);
    // ** 排序并截取统计数据 **/
    // Top N
    int rn = Synt.declare(rd.get(Cnst.RN_KEY), 0);
    int od = 0;
    if (ob != null) {
        if (ob.contains("!") || ob.contains("-")) {
            // 默认逆序
            od = 2;
        } else if (ob.contains("*")) {
            // 默认正序
            od = 1;
        }
    }
    for (Map.Entry<String, Map<Range, Ratio>> et : counts.entrySet()) {
        String k = et.getKey();
        Map<Range, Ratio> m = et.getValue();
        List<Object[]> a = new ArrayList(m.size());
        for (Map.Entry<Range, Ratio> e : et.getValue().entrySet()) {
            Range v = e.getKey();
            Ratio c = e.getValue();
            if (c.cnt != 0) {
                a.add(new Object[] { v, null, c.cnt, c.sum, c.min, c.max });
            }
        }
        // 排序
        if (ob != null && !ob.isEmpty()) {
            if (ob.contains(k + "!") || ob.contains("-" + k)) {
                Collections.sort(a, MountD);
            } else if (ob.contains(k)) {
                Collections.sort(a, MountA);
            } else if (od == 2) {
                Collections.sort(a, MountD);
            } else if (od == 1) {
                Collections.sort(a, MountA);
            }
        }
        // 截选 Top N
        n = Dict.getValue(rd, rn, k, Cnst.RN_KEY);
        if (n > 0 && n < a.size()) {
            a = a.subList(0, n);
        }
        cnts.put(k, a);
    }
    return cnts;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Range(io.github.ihongs.dh.search.StatisGrader.Range) Function(java.util.function.Function) HongsException(io.github.ihongs.HongsException) Ratio(io.github.ihongs.dh.search.StatisGrader.Ratio) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

HongsException (io.github.ihongs.HongsException)1 FormSet (io.github.ihongs.action.FormSet)1 Range (io.github.ihongs.dh.search.StatisGrader.Range)1 Ratio (io.github.ihongs.dh.search.StatisGrader.Ratio)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashSet (java.util.LinkedHashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1