Search in sources :

Example 46 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class LuceneRecord method getAnalyser.

/**
 * 查询分析器
 * @param fc 字段配置
 * @return
 * @throws HongsException
 */
protected Analyzer getAnalyser(Map fc) throws HongsException {
    try {
        CustomAnalyzer.Builder cb = CustomAnalyzer.builder();
        String kn, an, ac;
        Map oc;
        // 分词器
        an = Synt.declare(fc.get("lucene-tokenizer"), "");
        if (!"".equals(an)) {
            int p = an.indexOf('{');
            if (p != -1) {
                ac = an.substring(p);
                an = an.substring(0, p - 1).trim();
                oc = Synt.asMap(Dawn.toObject(ac));
                cb.withTokenizer(an, oc);
            } else {
                cb.withTokenizer(an);
            }
        } else {
            cb.withTokenizer("Standard");
        }
        // 过滤器
        for (Object ot2 : fc.entrySet()) {
            Map.Entry et2 = (Map.Entry) ot2;
            kn = (String) et2.getKey();
            // 查询参数为 find,query
            if (kn.startsWith("lucene-find-filter")) {
                an = (String) et2.getValue();
                an = an.trim();
                if ("".equals(an)) {
                    continue;
                }
                int p = an.indexOf('{');
                if (p != -1) {
                    ac = an.substring(p);
                    an = an.substring(0, p - 1).trim();
                    oc = Synt.asMap(Dawn.toObject(ac));
                    cb.addCharFilter(an, oc);
                } else {
                    cb.addCharFilter(an);
                }
            } else if (kn.startsWith("lucene-query-filter")) {
                an = (String) et2.getValue();
                an = an.trim();
                if ("".equals(an)) {
                    continue;
                }
                int p = an.indexOf('{');
                if (p != -1) {
                    ac = an.substring(p);
                    an = an.substring(0, p - 1).trim();
                    oc = Synt.asMap(Dawn.toObject(ac));
                    cb.addTokenFilter(an, oc);
                } else {
                    cb.addTokenFilter(an);
                }
            }
        }
        return cb.build();
    } catch (IOException ex) {
        throw new HongsException(ex);
    } catch (IllegalArgumentException ex) {
        throw new HongsException(ex);
    }
}
Also used : CustomAnalyzer(org.apache.lucene.analysis.custom.CustomAnalyzer) HongsException(io.github.ihongs.HongsException) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 47 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class LuceneRecord method search.

// ** 实体方法 **/
/**
 * 获取数据
 *
 * 以下参数为特殊参数, 可在 default.properties 中配置:
 * id   ID, 仅指定单个 id 时则返回详情(info)
 * rn   行数, 明确指定为 0 则不分页
 * gn   分页
 * pn   页码
 * wd   搜索
 * ob   排序
 * rb   字段
 * or   多组"或"关系条件
 * nr   多组"否"关系条件
 * ar   串联多组关系条件
 * 请注意尽量避免将其作为字段名(id,wd除外)
 *
 * @param rd
 * @return
 * @throws HongsException
 */
@Override
public Map search(Map rd) throws HongsException {
    // 指定单个 id 则走 getOne
    Object id = rd.get(Cnst.ID_KEY);
    if (id instanceof String || id instanceof Number) {
        if ("".equals(id)) {
            // id 为空则不获取
            return new HashMap();
        }
        Map info = getOne(rd);
        Map data = new HashMap();
        data.put("info", info);
        /**
         * 与 list 保持一致, 用 rn 控制 page
         * rn= 1 正常
         * rn= 0 不给 page
         * rn=-1 返回 page.state=-1 (缺失), page.state=-2 (受限)
         */
        int rn = Synt.declare(rd.get(Cnst.RN_KEY), 1);
        if (rn == 0) {
            return data;
        }
        Map page = new HashMap();
        data.put("page", page);
        /**
         * 查不到可能是不存在、已删除或受限
         * 需通过 id 再查一遍,区分不同错误
         */
        page.put(Cnst.RN_KEY, rn);
        if (null != info && !info.isEmpty()) {
            page.put("state", 1);
            page.put("count", 1);
        } else if (rn >= 1) {
            page.put("state", 0);
            page.put("count", 0);
        } else if (null != getDoc(id.toString())) {
            page.put("state", 0);
            page.put("count", 1);
        } else {
            page.put("state", 0);
            page.put("count", 0);
        }
        return data;
    }
    // 默认仅返回可以列举的字段
    Set rb = Synt.declare(rd.get(Cnst.RB_KEY), Set.class);
    if (rb == null || rb.isEmpty()) {
        rd = new LinkedHashMap(rd);
        rd.put(Cnst.RB_KEY, getListable());
    }
    int rn = Synt.declare(rd.get(Cnst.RN_KEY), Cnst.RN_DEF);
    if (rn < 0) {
        throw new HongsException(400, "Wrong param " + Cnst.RN_KEY);
    }
    int pn = Synt.declare(rd.get(Cnst.PN_KEY), 1);
    if (pn < 0) {
        throw new HongsException(400, "Wrong param " + Cnst.PN_KEY);
    }
    // 指定行数 0, 则获取全部
    if (rn == 0) {
        Map data = new HashMap();
        List list = getAll(rd);
        data.put("list", list);
        return data;
    }
    // 指定页码 0, 仅获取分页
    boolean nl = pn == 0;
    if (nl)
        pn = /**
         */
        1;
    int bn = rn * (pn - 1);
    Loop roll = search(rd, bn, rn);
    long rc = (long) roll.leng();
    long pc = (long) Math.ceil((double) rc / rn);
    int st = rc > bn ? 1 : 0;
    Map resp = new HashMap();
    Map page = new HashMap();
    page.put(Cnst.RN_KEY, rn);
    page.put(Cnst.PN_KEY, pn);
    page.put("count", rc);
    page.put("pages", pc);
    page.put("state", st);
    if (!nl) {
        List list = roll.toList();
        resp.put("list", list);
    }
    resp.put("page", page);
    return resp;
}
Also used : FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) HongsException(io.github.ihongs.HongsException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 48 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class LuceneRecord method getDoc.

public Document getDoc(String id) throws HongsException {
    /* Maybe changed */
    getReader();
    IndexSearcher ff = getFinder();
    try {
        Query qq = new TermQuery(new Term("@" + Cnst.ID_KEY, id));
        TopDocs tt = ff.search(qq, 1);
        ScoreDoc[] hh = tt.scoreDocs;
        if (0 != hh.length) {
            return ff.doc(hh[0].doc);
        } else {
            return null;
        }
    } catch (IOException ex) {
        throw new HongsException(ex);
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HongsException(io.github.ihongs.HongsException) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 49 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class TableDesc method getInstance.

public static TableDesc getInstance(Table table) throws HongsException {
    TableDesc desc = new TableDesc();
    try {
        List rows;
        Iterator it;
        /**
         * 组织字段描述
         */
        rows = table.db.fetchAll("SHOW FULL COLUMNS FROM `" + table.tableName + "`");
        it = rows.iterator();
        while (it.hasNext()) {
            Map row = (Map) it.next();
            String dfn = desc.getDefine(row);
            String col = (String) row.get("Field");
            desc.addColumn(col, dfn);
        }
        /**
         * 获取索引字段
         */
        rows = table.db.fetchAll("SHOW INDEXES FROM `" + table.tableName + "`");
        it = rows.iterator();
        while (it.hasNext()) {
            Map row = (Map) it.next();
            String key = (String) row.get("Key_name");
            String col = (String) row.get("Column_name");
            if ("PRIMARY".equals(key)) {
                desc.addPriCol(col);
            } else if ("0".equals(row.get("Non_unique"))) {
                desc.addUniKey(col, key);
            } else {
                desc.addIdxKey(col, key);
            }
        }
    } catch (HongsException ex) {
        if (ex.getErrno() == 1143) {
            String msg = ex.getMessage();
            if (msg.startsWith("Ex1143: Table ") && msg.endsWith(" doesn't exist")) {
                return desc;
            }
        }
        throw ex;
    }
    return desc;
}
Also used : HongsException(io.github.ihongs.HongsException) Iterator(java.util.Iterator) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 50 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class JRecord method del.

/**
 * 删除数据
 * @param key
 */
@Override
public void del(String key) throws HongsException {
    // table.db.open( );
    table.db.ready();
    try (PreparedStatement ps = table.db.prepareStatement("DELETE FROM `" + table.tableName + "` WHERE id = ?")) {
        ps.setString(1, key);
        ps.executeUpdate();
    } catch (SQLException e) {
        throw new HongsException(1045, e);
    }
}
Also used : SQLException(java.sql.SQLException) HongsException(io.github.ihongs.HongsException) PreparedStatement(java.sql.PreparedStatement)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9