cleaned up some if statements and added a test that selects TV options at random

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1141400 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2011-06-30 05:09:04 +00:00
parent 49661530e9
commit 9debf4f381
2 changed files with 48 additions and 18 deletions

View File

@ -5,7 +5,6 @@ import org.apache.lucene.document.Fieldable;
import org.apache.lucene.document.SetBasedFieldSelector;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermVectorMapper;
import org.apache.lucene.index.TermVectorOffsetInfo;
import org.apache.lucene.index.Terms;
@ -103,8 +102,7 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
allFields.tfIdf = params.getBool(TermVectorParams.TF_IDF, false);
//boolean cacheIdf = params.getBool(TermVectorParams.IDF, false);
//short cut to all values.
boolean all = params.getBool(TermVectorParams.ALL, false);
if (all == true) {
if (params.getBool(TermVectorParams.ALL, false)) {
allFields.termFreq = true;
allFields.positions = true;
allFields.offsets = true;
@ -145,11 +143,11 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
option.tfIdf = params.getFieldBool(field, TermVectorParams.TF_IDF, allFields.tfIdf);
//Validate these are even an option
option.positions = params.getFieldBool(field, TermVectorParams.POSITIONS, allFields.positions);
if (option.positions == true && sf.storeTermPositions() == false){
if (option.positions && !sf.storeTermPositions()){
noPos.add(field);
}
option.offsets = params.getFieldBool(field, TermVectorParams.OFFSETS, allFields.offsets);
if (option.offsets == true && sf.storeTermOffsets() == false){
if (option.offsets && !sf.storeTermOffsets()){
noOff.add(field);
}
} else {//field doesn't have term vectors
@ -162,26 +160,26 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
}
} //else, deal with all fields
boolean hasWarnings = false;
if (noTV.isEmpty() == false) {
if (!noTV.isEmpty()) {
warnings.add("noTermVectors", noTV);
hasWarnings = true;
}
if (noPos.isEmpty() == false) {
if (!noPos.isEmpty()) {
warnings.add("noPositions", noPos);
hasWarnings = true;
}
if (noOff.isEmpty() == false) {
if (!noOff.isEmpty()) {
warnings.add("noOffsets", noOff);
hasWarnings = true;
}
if (hasWarnings == true) {
if (hasWarnings) {
termVectors.add("warnings", warnings);
}
DocListAndSet listAndSet = rb.getResults();
List<Integer> docIds = getInts(params.getParams(TermVectorParams.DOC_IDS));
Iterator<Integer> iter;
if (docIds != null && docIds.isEmpty() == false) {
if (docIds != null && !docIds.isEmpty()) {
iter = docIds.iterator();
} else {
DocList list = listAndSet.docList;
@ -218,7 +216,7 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
termVectors.add("uniqueKeyFieldName", uniqFieldName);
}
}
if (fieldOptions.isEmpty() == false) {
if (!fieldOptions.isEmpty()) {
for (Map.Entry<String, FieldOptions> entry : fieldOptions.entrySet()) {
mapper.fieldOptions = entry.getValue();
reader.getTermFreqVector(docId, entry.getKey(), mapper);
@ -310,7 +308,7 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
if (fieldOptions.termFreq == true) {
termInfo.add("tf", frequency);
}
if (useOffsets == true) {
if (useOffsets) {
NamedList<Number> theOffsets = new NamedList<Number>();
termInfo.add("offsets", theOffsets);
for (int i = 0; i < offsets.length; i++) {
@ -319,17 +317,17 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
theOffsets.add("end", offset.getEndOffset());
}
}
if (usePositions == true) {
if (usePositions) {
NamedList<Integer> positionsNL = new NamedList<Integer>();
for (int i = 0; i < positions.length; i++) {
positionsNL.add("position", positions[i]);
}
termInfo.add("positions", positionsNL);
}
if (fieldOptions.docFreq == true) {
if (fieldOptions.docFreq) {
termInfo.add("df", getDocFreq(term));
}
if (fieldOptions.tfIdf == true) {
if (fieldOptions.tfIdf) {
double tfIdfVal = ((double) frequency) / getDocFreq(term);
termInfo.add("tf-idf", tfIdfVal);
}
@ -362,12 +360,12 @@ public class TermVectorComponent extends SearchComponent implements SolrCoreAwar
@Override
public boolean isIgnoringPositions() {
return fieldOptions.positions == false; // if we are not interested in positions, then return true telling Lucene to skip loading them
return !fieldOptions.positions; // if we are not interested in positions, then return true telling Lucene to skip loading them
}
@Override
public boolean isIgnoringOffsets() {
return fieldOptions.offsets == false; // if we are not interested in offsets, then return true telling Lucene to skip loading them
return !fieldOptions.offsets; // if we are not interested in offsets, then return true telling Lucene to skip loading them
}
}

View File

@ -136,6 +136,38 @@ public class TermVectorComponentTest extends SolrTestCaseJ4 {
, TermVectorParams.TF, "true", TermVectorParams.DF, "true", TermVectorParams.OFFSETS, "true", TermVectorParams.POSITIONS, "true", TermVectorParams.TF_IDF, "true")
,"/termVectors/doc-0/test_posofftv/anoth=={'tf':1, 'offsets':{'start':20, 'end':27}, 'positions':{'position':1}, 'df':2, 'tf-idf':0.5}"
);
assertJQ(req("json.nl","map", "qt",tv, "q", "id:0", TermVectorComponent.COMPONENT_NAME, "true"
, TermVectorParams.ALL, "true")
,"/termVectors/doc-0/test_posofftv/anoth=={'tf':1, 'offsets':{'start':20, 'end':27}, 'positions':{'position':1}, 'df':2, 'tf-idf':0.5}"
);
// test each combination at random
final List<String> list = new ArrayList<String>();
list.addAll(Arrays.asList("json.nl","map", "qt",tv, "q", "id:0", TermVectorComponent.COMPONENT_NAME, "true"));
String[][] options = new String[][] { { TermVectorParams.TF, "'tf':1" },
{ TermVectorParams.OFFSETS, "'offsets':{'start':20, 'end':27}" },
{ TermVectorParams.POSITIONS, "'positions':{'position':1}" },
{ TermVectorParams.DF, "'df':2" },
{ TermVectorParams.TF_IDF, "'tf-idf':0.5" } };
StringBuilder expected = new StringBuilder("/termVectors/doc-0/test_posofftv/anoth=={");
boolean first = true;
for (int i = 0; i < options.length; i++) {
final boolean use = random.nextBoolean();
if (use) {
if (!first) {
expected.append(", ");
}
first = false;
expected.append(options[i][1]);
}
list.add(options[i][0]);
list.add(use ? "true" : "false");
}
expected.append("}");
assertJQ(req(list.toArray(new String[0])), expected.toString());
}
@Test