List<QueryAnalysisResult> queryAnalysisResultList = getQueryAnalysisResults(customQueryString);
BooleanQuery resultQuery = new BooleanQuery();
CrescentCollectionField searchTargetField = null;
String fieldName = "";
Occur occur = Occur.SHOULD;
String userRequestQuery = "";
float boost = 0F;
boolean isRangeQuery = false;
boolean any = true;
boolean isLongField = false;
boolean isAnalyzed = false;
for(QueryAnalysisResult queryAnalysisResult : queryAnalysisResultList) {
fieldName = queryAnalysisResult.getFieldName();
occur = queryAnalysisResult.getOccur();
userRequestQuery = queryAnalysisResult.getUserQuery();
boost = queryAnalysisResult.getBoost();
isRangeQuery = queryAnalysisResult.isRangeQuery();
//field가 검색 대상에 있는지 확인..
for(CrescentCollectionField crescentField : indexedFields) {
if(fieldName.equals(crescentField.getName())) {
any = false;
searchTargetField = crescentField;
isLongField = "LONG".equals(crescentField.getType());
isAnalyzed = crescentField.isAnalyze();
logger.debug("selected searchTargetField : {} ", searchTargetField);
break;
}
}
if(any) {
logger.error("검색 할 수 없는 필드입니다. {} " , fieldName);
throw new CrescentInvalidRequestException("검색 할 수 없는 필드입니다. [" + fieldName + "]");
}
//range쿼리인 경우에는 RangeQuery 생성
if(isRangeQuery) {
//QueryParser qp = new QueryParser(Version.LUCENE_36, fieldName, analyzer);
String minValue = "";
String maxValue = "";
boolean isIncludeMin = false;
boolean isIncludeMax = false;
String[] splitQuery = userRequestQuery.split("TO");
logger.info("splitQuery : {}", Arrays.toString(splitQuery));
if(splitQuery.length != 2) {
logger.error("문법 오류 확인바랍니다. {} " , userRequestQuery);
throw new CrescentInvalidRequestException("문법 오류 확인바랍니다. [" + userRequestQuery + "]");
}
if(splitQuery[0].trim().startsWith("[")) {
isIncludeMin = true;
}
if(splitQuery[1].trim().endsWith("]")) {
isIncludeMax = true;
}
logger.debug("minInclude : {}, maxInclude : {}", isIncludeMin, isIncludeMax);
minValue = splitQuery[0].trim().substring(1);
maxValue = splitQuery[1].trim().substring(0, splitQuery[1].trim().length() - 1);
logger.debug("minValue : {}, maxValue : {}", minValue, maxValue);
boolean isNumeric = false;
isNumeric = StringUtils.isNumeric(minValue) && StringUtils.isNumeric(maxValue);
logger.debug("isLongField : {}", isLongField);
logger.debug("is numeric : {}", isNumeric);
Query query = null;
if(isAnalyzed) {
logger.error("범위검색 대상 field는 analyzed값이 false이어야 합니다. {} " , userRequestQuery);
throw new CrescentInvalidRequestException("범위검색 대상 field는 analyzed값이 false이어야 합니다. [" + userRequestQuery + "]");
}
if(isLongField && isNumeric) {
query = NumericRangeQuery.newLongRange(fieldName, Long.parseLong(minValue), Long.parseLong(maxValue), isIncludeMin, isIncludeMax);
} else if (!(isLongField && isNumeric)){
BytesRef minValBytes = new BytesRef(minValue);
BytesRef maxValBytes = new BytesRef(maxValue);
query = new TermRangeQuery(fieldName, minValBytes, maxValBytes, isIncludeMin, isIncludeMax);
} else {
logger.error("범위검색은 필드의 타입과 쿼리의 타입이 맞아야 합니다. {} " , userRequestQuery);
throw new CrescentInvalidRequestException("범위검색은 필드의 타입과 쿼리의 타입이 맞아야 합니다. [" + userRequestQuery + "]");
}
resultQuery.add(query, occur);
} else {
//쿼리 생성..
String[] keywords = userRequestQuery.split( " " );
if(logger.isDebugEnabled()) {
logger.debug("split keyword : {}", Arrays.toString(keywords));
}
for(int i = 0; i < keywords.length; i++) {
ArrayList<String> analyzedTokenList = analyzedTokenList(analyzer, keywords[i]);
if(!isAnalyzed || analyzedTokenList.size() == 0) {
Term t = new Term(fieldName, keywords[i]);
Query query = new TermQuery(t);
if(searchTargetField.getBoost() > 1F && boost > 1F) {
query.setBoost(searchTargetField.getBoost() + boost);
} else if (boost > 1F) {
query.setBoost(boost);
} else if (searchTargetField.getBoost() > 1F) {
query.setBoost(searchTargetField.getBoost());
}
resultQuery.add(query, occur);
logger.debug("query : {} ", query.toString());
logger.debug("result query : {} ", resultQuery.toString());
} else {
for(String str : analyzedTokenList) {
Term t = new Term(fieldName, str);
Query query = new TermQuery(t);
if(searchTargetField.getBoost() > 1F && boost > 1F) {
query.setBoost(searchTargetField.getBoost() + boost);
} else if (boost > 1F) {
query.setBoost(boost);
} else if (searchTargetField.getBoost() > 1F) {
query.setBoost(searchTargetField.getBoost());
}
resultQuery.add(query, occur);
logger.debug("query : {} ", query.toString());