Skip to content

Commit

Permalink
expose term vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Newson committed Mar 29, 2012
1 parent db213d8 commit ef003db
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -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.