public String highlightQuery(QueryInfo info, String fieldName, String text) { String result = ""; // String fieldNameOriginal=fieldName; // String textOriginal=text; // String attributeName=""; try { EFOExpandableQueryInfo queryInfo = null; // logger.debug("text->{}", text); // logger.debug("fieldName->{}", fieldName); if (info instanceof EFOExpandableQueryInfo) { queryInfo = (EFOExpandableQueryInfo) info; } // if(fieldName.startsWith("attributes:")){ // fieldName="attributes"; // attributeName=fieldNameOriginal.replace("attributes:", ""); // text=attributeName+"="+text; // // } if (null == queryInfo) { result = doHighlightQuery(info.getQuery(), fieldName, text, HIT_OPEN_MARK, HIT_CLOSE_MARK); } else { result = doHighlightQuery( queryInfo.getOriginalQuery(), fieldName, text, HIT_OPEN_MARK, HIT_CLOSE_MARK); result = doHighlightQuery( queryInfo.getSynonymPartQuery(), fieldName, result, SYN_OPEN_MARK, SYN_CLOSE_MARK); result = doHighlightQuery( queryInfo.getEfoExpansionPartQuery(), fieldName, result, EFO_OPEN_MARK, EFO_CLOSE_MARK); result = EFO_AND_SYN_REGEX.replace(result, SYN_OPEN_MARK + "$1" + SYN_CLOSE_MARK); result = SYN_AND_HIT_REGEX.replace(result, HIT_OPEN_MARK + "$1" + HIT_CLOSE_MARK); result = EFO_AND_HIT_REGEX.replace(result, HIT_OPEN_MARK + "$1" + HIT_CLOSE_MARK); } } catch (Exception x) { logger.error("Caught an exception:", x); } // logger.debug("result->"+result); return result; }
@Override public Query construct(IndexEnvironment env, Map<String, String[]> querySource) throws ParseException { if ("1".equals(StringTools.arrayToString(querySource.get("queryversion"), ""))) { // preserving old stuff: // 1. all lucene special chars to be quoted // 2. if "wholewords" is "on" or "true" -> don't add *_*, otherwise add *_* BooleanQuery result = new BooleanQuery(); String wholeWords = StringTools.arrayToString(querySource.get("wholewords"), ""); boolean useWildcards = !(null != wholeWords && StringTools.stringToBoolean(wholeWords)); for (Map.Entry<String, String[]> queryItem : querySource.entrySet()) { String field = queryItem.getKey(); if (env.fields.containsKey(field) && queryItem.getValue().length > 0) { for (String value : queryItem.getValue()) { if (null != value) { value = value.trim().toLowerCase(); if (0 != value.length()) { if ("keywords".equals(field) && ACCESSION_REGEX.test(value)) { result.add(new TermQuery(new Term("accession", value)), BooleanClause.Occur.MUST); } else if ("keywords".equals(field) && '"' == value.charAt(0) && '"' == value.charAt(value.length() - 1)) { value = value.substring(1, value.length() - 1); PhraseQuery q = new PhraseQuery(); String[] tokens = value.split("\\s+"); for (String token : tokens) { q.add(new Term(field, token)); } result.add(q, BooleanClause.Occur.MUST); } else { String[] tokens = value.split("\\s+"); for (String token : tokens) { // we use wildcards for keywords depending on "wholewords" switch, // *ALWAYS* for other fields, *NEVER* for user id and accession or boolean // fields Query q = !"boolean".equals(env.fields.get(field).type) && !" userid accession ".contains(" " + field + " ") && (useWildcards || (!" keywords ".contains(" " + field + " "))) ? new WildcardQuery(new Term(field, "*" + token + "*")) : new TermQuery(new Term(field, token)); result.add(q, BooleanClause.Occur.MUST); } } } } } } } return result; } else { return super.construct(env, querySource); } }
@Override protected void doRequest( HttpServletRequest request, HttpServletResponse response, RequestType requestType) throws ServletException, IOException { RegexHelper MATCH_URL_REGEX = new RegexHelper("/+(.+)", "i"); RegexHelper TEST_HOST_IN_URL_REGEX = new RegexHelper("^http\\:/{2}([^/]+)/", "i"); RegexHelper SQUARE_BRACKETS_REGEX = new RegexHelper("\\[\\]", "g"); logRequest(logger, request, requestType); String url = MATCH_URL_REGEX.matchFirst(request.getPathInfo()); url = url.replaceFirst( "http:/{1,2}", "http://"); // stupid hack as tomcat 6.0 removes second forward slash String queryString = request.getQueryString(); if (0 < url.length()) { if (!TEST_HOST_IN_URL_REGEX.test(url)) { // no host here, will self url = "http://localhost:" + String.valueOf(request.getLocalPort()) + "/" + url; } logger.debug("Will access [{}]", url); GetMethod getMethod = new GetMethod(url); if (null != queryString) { queryString = SQUARE_BRACKETS_REGEX.replace(queryString, "%5B%5D"); getMethod.setQueryString(queryString); } Enumeration requestHeaders = request.getHeaderNames(); while (requestHeaders.hasMoreElements()) { String name = (String) requestHeaders.nextElement(); String value = request.getHeader(name); if (null != value) { getMethod.setRequestHeader(name, value); } } try { httpClient.executeMethod(getMethod); int statusCode = getMethod.getStatusCode(); long contentLength = getMethod.getResponseContentLength(); logger.debug("Got response [{}], length [{}]", statusCode, contentLength); Header[] responseHeaders = getMethod.getResponseHeaders(); for (Header responseHeader : responseHeaders) { String name = responseHeader.getName(); String value = responseHeader.getValue(); if (null != name && null != value && !(name.equals("Server") || name.equals("Date") || name.equals("Transfer-Encoding"))) { response.setHeader(responseHeader.getName(), responseHeader.getValue()); } } if (200 != statusCode) { response.setStatus(statusCode); } InputStream inStream = getMethod.getResponseBodyAsStream(); if (null != inStream) { BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream())); CharBuffer buffer = CharBuffer.allocate(PROXY_BUFFER_SIZE); while (in.read(buffer) >= 0) { buffer.flip(); out.append(buffer); buffer.clear(); } in.close(); out.close(); } } catch (Exception x) { if (x.getClass().getName().equals("org.apache.catalina.connector.ClientAbortException")) { logger.warn("Client aborted connection"); } else { logger.error("Caught an exception:", x); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, x.getMessage()); } } finally { getMethod.releaseConnection(); } } }