From 1b5afe0f2695c50c5c0cdffc6f61d3bdc327a7c6 Mon Sep 17 00:00:00 2001 From: Tommaso Teofili Date: Sat, 9 Aug 2014 20:07:00 +0000 Subject: [PATCH] LUCENE-5880 - avoid NegativeArraySizeException in DocToDoubleVectorUtils git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1617010 13f79535-47bb-0310-9956-ffa450edef68 --- .../utils/DocToDoubleVectorUtils.java | 2 +- .../utils/DocToDoubleVectorUtilsTest.java | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java b/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java index 1c29255b526..c2db61e6d41 100644 --- a/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java +++ b/lucene/classification/src/java/org/apache/lucene/classification/utils/DocToDoubleVectorUtils.java @@ -41,7 +41,7 @@ public class DocToDoubleVectorUtils { public static Double[] toSparseLocalFreqDoubleArray(Terms docTerms, Terms fieldTerms) throws IOException { TermsEnum fieldTermsEnum = fieldTerms.iterator(null); Double[] freqVector = null; - if (docTerms != null) { + if (docTerms != null && fieldTerms.size() > -1) { freqVector = new Double[(int) fieldTerms.size()]; int i = 0; TermsEnum docTermsEnum = docTerms.iterator(null); diff --git a/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java b/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java index b132a1248d3..24de791e947 100644 --- a/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java +++ b/lucene/classification/src/test/org/apache/lucene/classification/utils/DocToDoubleVectorUtilsTest.java @@ -94,12 +94,14 @@ public class DocToDoubleVectorUtilsTest extends LuceneTestCase { @Test public void testSparseFreqDoubleArrayConversion() throws Exception { Terms fieldTerms = MultiFields.getTerms(index, "text"); - IndexSearcher indexSearcher = new IndexSearcher(index); - for (ScoreDoc scoreDoc : indexSearcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE).scoreDocs) { - Terms docTerms = index.getTermVector(scoreDoc.doc, "text"); - Double[] vector = DocToDoubleVectorUtils.toSparseLocalFreqDoubleArray(docTerms, fieldTerms); - assertNotNull(vector); - assertTrue(vector.length > 0); + if (fieldTerms != null && fieldTerms.size() != -1) { + IndexSearcher indexSearcher = new IndexSearcher(index); + for (ScoreDoc scoreDoc : indexSearcher.search(new MatchAllDocsQuery(), Integer.MAX_VALUE).scoreDocs) { + Terms docTerms = index.getTermVector(scoreDoc.doc, "text"); + Double[] vector = DocToDoubleVectorUtils.toSparseLocalFreqDoubleArray(docTerms, fieldTerms); + assertNotNull(vector); + assertTrue(vector.length > 0); + } } } }