forked from rnewson/couchdb-lucene
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Robert Newson
committed
Mar 29, 2012
1 parent
db213d8
commit ef003db
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/java/com/github/rnewson/couchdb/lucene/JsonTermVectorMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |