From 9debf4f381cd38e0c571d7c07a1a36805936e74b Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Thu, 30 Jun 2011 05:09:04 +0000 Subject: [PATCH] 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 --- .../component/TermVectorComponent.java | 32 ++++++++--------- .../component/TermVectorComponentTest.java | 34 ++++++++++++++++++- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/solr/src/java/org/apache/solr/handler/component/TermVectorComponent.java b/solr/src/java/org/apache/solr/handler/component/TermVectorComponent.java index da751db9493..a90d40e4a30 100644 --- a/solr/src/java/org/apache/solr/handler/component/TermVectorComponent.java +++ b/solr/src/java/org/apache/solr/handler/component/TermVectorComponent.java @@ -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 docIds = getInts(params.getParams(TermVectorParams.DOC_IDS)); Iterator 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 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 theOffsets = new NamedList(); 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 positionsNL = new NamedList(); 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 } } diff --git a/solr/src/test/org/apache/solr/handler/component/TermVectorComponentTest.java b/solr/src/test/org/apache/solr/handler/component/TermVectorComponentTest.java index 2a4dd253746..efbc17cad22 100644 --- a/solr/src/test/org/apache/solr/handler/component/TermVectorComponentTest.java +++ b/solr/src/test/org/apache/solr/handler/component/TermVectorComponentTest.java @@ -135,7 +135,39 @@ public class TermVectorComponentTest extends SolrTestCaseJ4 { assertJQ(req("json.nl","map", "qt",tv, "q", "id:0", TermVectorComponent.COMPONENT_NAME, "true" , 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 list = new ArrayList(); + 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