@Override public void process() throws ProcessingException { if (!enabled) { return; } if (query == null) { query = ""; } // Create regexp patterns for each query word final String[] queryTerms = querySanitizePatternCompiled.matcher(query).replaceAll("").split("\\s+"); Pattern queryPattern = null; List<String> patterns = Lists.newArrayList(); for (String queryTerm : queryTerms) { if (Strings.isNullOrEmpty(queryTerm)) { continue; } if (dontHighlightPatternCompiled != null && dontHighlightPatternCompiled.matcher(queryTerm).matches()) { continue; } patterns.add("(" + Pattern.quote(escapeLtGt(queryTerm)) + ")"); } if (patterns.size() > 0) { queryPattern = Pattern.compile( Joiner.on("|").join(patterns), Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE); } // As we're going to modify documents, we need to copy them to // avoid ConcurrentModificationExceptions. final List<Document> inputDocuments = documents; final List<Document> outputDocuments = Lists.newArrayListWithCapacity(inputDocuments.size()); for (Document document : inputDocuments) { final Document clonedDocument = document.clone(); for (String fieldName : fields) { highlightQueryTerms(clonedDocument, fieldName, queryPattern); } outputDocuments.add(clonedDocument); } documents = outputDocuments; }
@Test public void testRemoteUrl() throws MalformedURLException { String base = System.getProperty("carrot2.xml.feed.url.base"); assumeTrue("carrot2.xml.feed.url.base property undefined.", !Strings.isNullOrEmpty(base)); IResource xml = new URLResourceWithParams(new URL(base + "&q=${query}&results=${results}")); final String query = "apple computer"; processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeNames.QUERY, query); processingAttributes.put(AttributeNames.RESULTS, 50); final int documentCount = runQuery(); assertEquals(50, documentCount); assertEquals(query, resultAttributes.get(AttributeNames.QUERY)); }