IqlAnalyse.java 3.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
package com.farm.lucene.common;

import org.apache.log4j.Logger;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;

import com.farm.lucene.constant.IndexConf;

public class IqlAnalyse implements IqlAnalyseInter {
	// WHERE(sdf,dfd=sdfsfd)ORDER_BY(dddd ASC,dddd ASC)
	private static final Logger log = Logger.getLogger(IqlAnalyse.class);

	public IqlAnalyse(String IQL, int cPage, int psize) {
		if (LUCENE_QUERY_MAXNUM == 0) {
			String para = null;
			try {
				para = IndexConf.getString("config.file.luncene_max_query_num");
				LUCENE_QUERY_MAXNUM = Integer.valueOf(para);
			} catch (Exception e) {
				log.error("系统参数LUCENE_QUERY_MAXNUM错误:" + para);
			}
		}
		iql = IQL.trim();
		if (iql.indexOf("WHERE(") >= 0) {
			String where;
			where = iql.substring(iql.indexOf("WHERE(") + "WHERE(".length(), iql.indexOf(")"));
			String[] whereArray = where.split("=");
			if (whereArray.length < 2) {
				throw new RuntimeException("where错误");
			}
			title = whereArray[0];
			value = whereArray[1];

		}
		if (iql.indexOf("ORDER_BY(") >= 0) {
			String iqlpart = iql.substring(iql.indexOf("ORDER_BY("));
			group = iqlpart.substring(iqlpart.indexOf("ORDER_BY(") + "ORDER_BY(".length(), iqlpart.indexOf(")"));
		}

		this.currentPage = cPage;
		pageSize = psize;
	}

	private String iql;
	private String title;
	private String value;
	// 排序语句
	private String group;
	// 分页语句
	private int currentPage;
	private int pageSize;
	private static int LUCENE_QUERY_MAXNUM;

	public static void main(String[] args) {
		// AloneBaseManager.instance().getParameterFace().initConfig();
		// new IqlAnalyse("WHERE(sdf,dfd=sdfsfd)ORDER_BY(dddd ASC)LIMIT(5,5)");
	}

	@Override
	public String[] getLimitTitle() {
		return title.trim().toUpperCase().split(",");
	}

	@Override
	public String getLimitValue() {
		return value.replaceAll("\\(", "").replaceAll("\\)", "");
	}

	@Override
	public int getMaxTopNum() {
		return LUCENE_QUERY_MAXNUM;
	}

	@Override
	public Sort getSortTitle() {
		// ORDER_BY(dddd:int ASC,dddd:string ASC)
		// 注意被排序的字段必须被存储索引
		Sort sort = new Sort();
		if (group == null || group.trim().length() <= 0) {
			return sort;
		}
		String[] sortstr = group.split(",");
		SortField[] fields = new SortField[sortstr.length];
		try {
			for (int i = 0; i < sortstr.length; i++) {
				String sortString = sortstr[i];
				String[] para = sortString.trim().split(" ");
				String[] para2 = para[0].trim().split(":");
				String title = para2[0].trim().toUpperCase();
				String type = para2[1].trim().toUpperCase();
				String sortType = para[1].trim().toUpperCase();
				int typeInt = 0;
				if (title == null || title.length() <= 0 || type == null || type.length() <= 0 || sortType == null
						|| sortType.length() <= 0) {
					throw new RuntimeException("排序语句错误" + group);
				}
				if (type.equals("DOUBLE")) {
					typeInt = SortField.DOUBLE;
				}
				if (type.equals("STRING")) {
					typeInt = SortField.STRING;
				}
				if (type.equals("LONG")) {
					typeInt = SortField.LONG;
				}

				fields[i] = new SortField(title, typeInt, sortType.toUpperCase().equals("DESC"));
			}
			sort.setSort(fields);
		} catch (Exception e) {
			log.error("排序条件解析错误" + e);
			throw new RuntimeException("排序语句错误" + group + "/" + e);
		}
		return sort;
	}

	@Override
	public ScoreDoc[] subDoc(ScoreDoc[] allDoc) {
		int curentSize = allDoc.length - ((currentPage - 1) * pageSize);
		if (curentSize <= 0) {
			curentSize = 0;
		}
		if (curentSize > pageSize) {
			curentSize = pageSize;
		}
		ScoreDoc[] newScore = new ScoreDoc[curentSize];
		int m = 0;
		for (int i = ((currentPage - 1) * pageSize); i < ((currentPage - 1) * pageSize) + curentSize; i++) {
			if (allDoc.length < i) {
				break;
			}
			newScore[m] = allDoc[i];
			m++;
		}
		return newScore;
	}

	@Override
	public String getIQL() {
		return iql;
	}

}