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

View File

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

View File

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