Permalink
Browse files

expose term vectors

  • Loading branch information...
1 parent db213d8 commit ef003db4acc2ebf9ed66cecda68e89b1f7f48eed Robert Newson committed Mar 29, 2012
@@ -46,6 +46,8 @@
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LogByteSizeMergePolicy;
import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermFreqVector;
+import org.apache.lucene.index.TermPositionVector;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.queryParser.QueryParser.Operator;
@@ -602,6 +604,11 @@ public void search(final HttpServletRequest req,
if (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());
+
rows.put(row);
}
// Fetch documents (if requested).
@@ -0,0 +1,54 @@
+package com.github.rnewson.couchdb.lucene;
+
+import org.apache.lucene.index.TermVectorMapper;
+import org.apache.lucene.index.TermVectorOffsetInfo;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+class JsonTermVectorMapper extends TermVectorMapper {
+
+ private JSONObject result = new JSONObject();
+ private JSONObject currentObj;
+
+ @Override
+ public void setExpectations(String field, int numTerms,
+ boolean storeOffsets, boolean storePositions) {
+ currentObj = new JSONObject();
+ try {
+ result.put(field, currentObj);
+ } catch (JSONException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void map(String term, int frequency, TermVectorOffsetInfo[] offsets,
+ int[] positions) {
+ try {
+ final JSONObject field = new JSONObject();
+ field.put("freq", frequency);
+ if (offsets != null) {
+ final JSONArray arr = new JSONArray();
+ for (int i = 0; i < offsets.length; i++) {
+ final JSONArray arr2 = new JSONArray();
+ arr2.put(offsets[i].getStartOffset());
+ arr2.put(offsets[i].getEndOffset());
+ arr.put(arr2);
+ }
+ field.put("offsets", arr);
+ }
+ if (positions != null) {
+ field.put("positions", positions);
+ }
+ currentObj.put(term, field);
+ } catch (JSONException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public JSONObject getObject() {
+ return result;
+ }
+
+}

0 comments on commit ef003db

Please sign in to comment.