Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add highlighting and parameters to include termvectors and fields in …

…results
  • Loading branch information...
commit 4227051df5657d4a0178b67c371f5c7f117b855d 1 parent 900997e
@nesteffe nesteffe authored
View
27 src/main/java/com/github/rnewson/couchdb/lucene/DatabaseIndexer.java
@@ -43,6 +43,7 @@
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexReader.FieldOption;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
@@ -52,6 +53,7 @@
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
+import org.apache.lucene.search.vectorhighlight.FastVectorHighlighter;
import org.apache.lucene.search.FieldDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
@@ -488,6 +490,7 @@ public void search(final HttpServletRequest req,
return;
final IndexSearcher searcher = state.borrowSearcher(isStaleOk(req));
final String etag = state.getEtag();
+ final FastVectorHighlighter fvh = new FastVectorHighlighter(true, true);
final JSONArray result = new JSONArray();
try {
if (state.notModified(req)) {
@@ -528,6 +531,10 @@ public void search(final HttpServletRequest req,
final boolean include_docs = getBooleanParameter(req,
"include_docs");
+ final int highlights = getIntParameter(req, "highlights", 0);
+ final int highlight_length = max(getIntParameter(req, "highlight_length", 18), 18); // min for fast term vector highlighter is 18
+ final boolean include_termvectors = getBooleanParameter(req, "include_termvectors");
+ final boolean include_fields = getBooleanParameter(req, "include_fields");
final int limit = getIntParameter(req, "limit",
ini.getInt("lucene.limit", 25));
final Sort sort = CustomQueryParser.toSort(req
@@ -561,6 +568,7 @@ public void search(final HttpServletRequest req,
final JSONObject row = new JSONObject();
final JSONObject fields = new JSONObject();
+ final JSONObject highlight_rows = new JSONObject();
// Include stored fields.
for (final Fieldable f : doc.getFields()) {
@@ -592,6 +600,11 @@ public void search(final HttpServletRequest req,
((JSONArray) obj).put(value);
}
}
+
+ if (highlights > 0) {
+ String[] frags = fvh.getBestFragments(fvh.getFieldQuery(q), searcher.getIndexReader(), td.scoreDocs[i].doc, name, highlight_length, highlights);
+ highlight_rows.put(name, frags);
+ }
}
}
}
@@ -607,13 +620,17 @@ public void search(final HttpServletRequest req,
if (include_docs) {
fetch_ids[i - skip] = doc.get("_id");
}
- if (fields.length() > 0) {
+ if (include_fields && fields.length() > 0) {
row.put("fields", fields);
}
-
- final JsonTermVectorMapper mapper = new JsonTermVectorMapper();
- searcher.getIndexReader().getTermFreqVector(td.scoreDocs[i].doc, mapper);
- row.put("termvectors", mapper.getObject());
+ if (highlight_rows.length() > 0) {
+ row.put("highlights", highlight_rows);
+ }
+ if (include_termvectors) {
+ final JsonTermVectorMapper mapper = new JsonTermVectorMapper();
+ searcher.getIndexReader().getTermFreqVector(td.scoreDocs[i].doc, mapper);
+ row.put("termvectors", mapper.getObject());
+ }
rows.put(row);
}
View
4 src/main/java/com/github/rnewson/couchdb/lucene/JsonTermVectorMapper.java
@@ -37,9 +37,13 @@ public void map(String term, int frequency, TermVectorOffsetInfo[] offsets,
arr.put(arr2);
}
field.put("offsets", arr);
+ } else {
+ field.put("offsets", "null");
}
if (positions != null) {
field.put("positions", positions);
+ } else {
+ field.put("positions", "null");
}
currentObj.put(term, field);
} catch (JSONException e) {
Please sign in to comment.
Something went wrong with that request. Please try again.