- fixed another Null ptr when there is no highlight content in rare cases

- tweaked composite Solr query escaping
This commit is contained in:
adam-m 2012-01-04 18:06:58 -05:00
parent 8bf50478af
commit 54758bf45a
3 changed files with 28 additions and 21 deletions

View File

@ -18,10 +18,8 @@
*/
package org.sleuthkit.autopsy.keywordsearch;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.logging.Level;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrRequest.METHOD;
@ -40,7 +38,6 @@ class HighlightedMatchesSource implements MarkupSource {
private static final String HIGHLIGHT_PRE = "<span style=\"background:yellow\">";
private static final String HIGHLIGHT_POST = "</span>";
private static final String ANCHOR_PREFIX = HighlightedMatchesSource.class.getName() + "_";
private Content content;
private String solrQuery;
private Core solrCore;
@ -61,7 +58,7 @@ class HighlightedMatchesSource implements MarkupSource {
SolrQuery q = new SolrQuery();
final String queryEscaped = KeywordSearchUtil.escapeLuceneQuery(solrQuery);
q.setQuery(queryEscaped);
q.addFilterQuery("id:" + content.getId());
q.addHighlightField("content");
@ -71,9 +68,16 @@ class HighlightedMatchesSource implements MarkupSource {
try {
QueryResponse response = solrCore.query(q, METHOD.POST);
List<String> contentHighlights = response.getHighlighting().get(Long.toString(content.getId())).get("content");
Map<String, Map<String, List<String>>> responseHighlight = response.getHighlighting();
long contentID = content.getId();
Map<String, List<String>> responseHighlightID = responseHighlight.get(Long.toString(contentID));
final String NO_MATCHES = "<span style=\"background:red\">No matches in content.</span>";
if (responseHighlightID == null) {
return NO_MATCHES;
}
List<String> contentHighlights = responseHighlightID.get("content");
if (contentHighlights == null) {
return "<span style=\"background:red\">No matches in content.</span>";
return NO_MATCHES;
} else {
// extracted content (minus highlight tags) is HTML-escaped
String highlightedContent = contentHighlights.get(0).trim();
@ -89,28 +93,28 @@ class HighlightedMatchesSource implements MarkupSource {
public String toString() {
return "Search Matches";
}
@Override
public boolean isSearchable() {
return true;
}
@Override
public String getAnchorPrefix() {
return ANCHOR_PREFIX;
}
@Override
public int getNumberHits() {
@Override
public int getNumberHits() {
return numberHits;
}
}
private String insertAnchors(String searchableContent) {
int searchOffset = 0;
int index = -1;
StringBuilder buf = new StringBuilder(searchableContent);
final String searchToken = HIGHLIGHT_PRE;
final int indexSearchTokLen = searchToken.length();
final String insertPre = "<a name=\"" + ANCHOR_PREFIX;
@ -120,10 +124,10 @@ class HighlightedMatchesSource implements MarkupSource {
String insertString = insertPre + Integer.toString(count) + insertPost;
int insertStringLen = insertString.length();
buf.insert(index, insertString);
searchOffset = index + indexSearchTokLen + insertStringLen ; //next offset past this anchor
searchOffset = index + indexSearchTokLen + insertStringLen; //next offset past this anchor
++count;
}
}
this.numberHits = count;
return buf.toString();
}

View File

@ -189,6 +189,8 @@ public class KeywordSearchResultFactory extends ChildFactory<KeyValueThing> {
highlightQuery.append(termS);
highlightQuery.append(" ");
}
//String highlightQueryEscaped = KeywordSearchUtil.escapeLuceneQuery(highlightQuery.toString());
String highlightQueryEscaped = highlightQuery.toString();
int resID = 0;
for (FsContent f : fsContents) {
@ -196,7 +198,7 @@ public class KeywordSearchResultFactory extends ChildFactory<KeyValueThing> {
Map<String, Object> resMap = new LinkedHashMap<String, Object>();
AbstractFsContentNode.fillPropertyMap(resMap, f);
setCommonProperty(resMap, CommonPropertyTypes.MATCH, f.getName());
toPopulate.add(new KeyValueThingContent(f.getName(), resMap, ++resID, f, highlightQuery.toString()));
toPopulate.add(new KeyValueThingContent(f.getName(), resMap, ++resID, f, highlightQueryEscaped));
}
return true;

View File

@ -166,6 +166,7 @@ public class TermComponentQuery implements KeywordSearchQuery {
//requires http POST query method due to potentially large query size
StringBuilder filesQueryB = new StringBuilder();
for (Term term : terms) {
//final String termS = KeywordSearchUtil.escapeLuceneQuery(term.getTerm());
final String termS = term.getTerm();
filesQueryB.append(termS);
filesQueryB.append(" ");
@ -173,7 +174,7 @@ public class TermComponentQuery implements KeywordSearchQuery {
List<FsContent> uniqueMatches = new ArrayList<FsContent>();
LuceneQuery filesQuery = new LuceneQuery(filesQueryB.toString());
filesQuery.escape();
filesQuery.escape(); //TODO escaping invididual terms above instead could make a difference to Solr
try {
uniqueMatches = filesQuery.performQuery();
}