From e31a9da835c6e381bbcd46aa4390089f6bcc1c15 Mon Sep 17 00:00:00 2001 From: Michael McCandless Date: Tue, 19 Aug 2008 10:40:39 +0000 Subject: [PATCH] LUCENE-1334: add Term(String fieldName) constructor that sets term text to empty string git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@687014 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 3 +++ .../query/QueryAutoStopWordAnalyzer.java | 2 +- .../apache/lucene/index/memory/MemoryIndex.java | 4 ++-- .../apache/lucene/index/FieldNormModifier.java | 2 +- .../apache/lucene/misc/LengthNormModifier.java | 2 +- .../apache/lucene/search/DuplicateFilter.java | 4 ++-- .../apache/lucene/search/FuzzyLikeThisQuery.java | 2 +- .../lucene/search/spell/LuceneDictionary.java | 2 +- .../org/apache/lucene/index/ParallelReader.java | 2 +- src/java/org/apache/lucene/index/Term.java | 11 +++++++++++ .../lucene/search/ExtendedFieldCacheImpl.java | 6 +++--- .../org/apache/lucene/search/FieldCacheImpl.java | 16 ++++++++-------- .../org/apache/lucene/search/RangeFilter.java | 4 ++-- .../org/apache/lucene/search/RangeQuery.java | 2 +- 14 files changed, 38 insertions(+), 24 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 03ec689c1ca..e75738b8338 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -105,6 +105,9 @@ API Changes the provided byte[] result to getBinaryValue. (Eks Dev via Mike McCandless) +16. LUCENE-1334: Add new constructor for Term: Term(String fieldName) + which defaults term text to "". (DM Smith via Mike McCandless) + Bug fixes 1. LUCENE-1134: Fixed BooleanQuery.rewrite to only optimize a single diff --git a/contrib/analyzers/src/java/org/apache/lucene/analysis/query/QueryAutoStopWordAnalyzer.java b/contrib/analyzers/src/java/org/apache/lucene/analysis/query/QueryAutoStopWordAnalyzer.java index e46a51af2b3..50bd352d2cd 100644 --- a/contrib/analyzers/src/java/org/apache/lucene/analysis/query/QueryAutoStopWordAnalyzer.java +++ b/contrib/analyzers/src/java/org/apache/lucene/analysis/query/QueryAutoStopWordAnalyzer.java @@ -138,7 +138,7 @@ public class QueryAutoStopWordAnalyzer extends Analyzer { public int addStopWords(IndexReader reader, String fieldName, int maxDocFreq) throws IOException { HashSet stopWords = new HashSet(); String internedFieldName = fieldName.intern(); - TermEnum te = reader.terms(new Term(fieldName, "")); + TermEnum te = reader.terms(new Term(fieldName)); Term term = te.term(); while (term != null) { if (term.field() != internedFieldName) { diff --git a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java index 75f392cec55..f13c5906932 100644 --- a/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java +++ b/contrib/memory/src/java/org/apache/lucene/index/memory/MemoryIndex.java @@ -704,7 +704,7 @@ public class MemoryIndex implements Serializable { /////////////////////////////////////////////////////////////////////////////// // Nested classes: /////////////////////////////////////////////////////////////////////////////// - private static final Term MATCH_ALL_TERM = new Term("", ""); + private static final Term MATCH_ALL_TERM = new Term(""); /** * Search support for Lucene framework integration; implements all methods @@ -821,7 +821,7 @@ public class MemoryIndex implements Serializable { Term template = info.template; if (template == null) { // not yet cached? String fieldName = (String) sortedFields[pos].getKey(); - template = new Term(fieldName, ""); + template = new Term(fieldName); info.template = template; } diff --git a/contrib/miscellaneous/src/java/org/apache/lucene/index/FieldNormModifier.java b/contrib/miscellaneous/src/java/org/apache/lucene/index/FieldNormModifier.java index 761425dc0c8..e5fd7a04bf2 100644 --- a/contrib/miscellaneous/src/java/org/apache/lucene/index/FieldNormModifier.java +++ b/contrib/miscellaneous/src/java/org/apache/lucene/index/FieldNormModifier.java @@ -118,7 +118,7 @@ public class FieldNormModifier { if (sim == null) fakeNorms = SegmentReader.createFakeNorms(reader.maxDoc()); try { - termEnum = reader.terms(new Term(field,"")); + termEnum = reader.terms(new Term(field)); try { termDocs = reader.termDocs(); do { diff --git a/contrib/miscellaneous/src/java/org/apache/lucene/misc/LengthNormModifier.java b/contrib/miscellaneous/src/java/org/apache/lucene/misc/LengthNormModifier.java index 427253f2d3b..e6c4afa66de 100644 --- a/contrib/miscellaneous/src/java/org/apache/lucene/misc/LengthNormModifier.java +++ b/contrib/miscellaneous/src/java/org/apache/lucene/misc/LengthNormModifier.java @@ -114,7 +114,7 @@ public class LengthNormModifier { reader = IndexReader.open(dir); termCounts = new int[reader.maxDoc()]; try { - termEnum = reader.terms(new Term(field,"")); + termEnum = reader.terms(new Term(field)); try { termDocs = reader.termDocs(); do { diff --git a/contrib/queries/src/java/org/apache/lucene/search/DuplicateFilter.java b/contrib/queries/src/java/org/apache/lucene/search/DuplicateFilter.java index 4137b92bd14..30fe7042222 100644 --- a/contrib/queries/src/java/org/apache/lucene/search/DuplicateFilter.java +++ b/contrib/queries/src/java/org/apache/lucene/search/DuplicateFilter.java @@ -83,7 +83,7 @@ public class DuplicateFilter extends Filter { OpenBitSet bits=new OpenBitSet(reader.maxDoc()); //assume all are INvalid - Term startTerm=new Term(fieldName,""); + Term startTerm=new Term(fieldName); TermEnum te = reader.terms(startTerm); if(te!=null) { @@ -123,7 +123,7 @@ public class DuplicateFilter extends Filter OpenBitSet bits=new OpenBitSet(reader.maxDoc()); bits.set(0,reader.maxDoc()); //assume all are valid - Term startTerm=new Term(fieldName,""); + Term startTerm=new Term(fieldName); TermEnum te = reader.terms(startTerm); if(te!=null) { diff --git a/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java b/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java index e4d14a6a05c..0810942c8dc 100644 --- a/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java +++ b/contrib/queries/src/java/org/apache/lucene/search/FuzzyLikeThisQuery.java @@ -106,7 +106,7 @@ public class FuzzyLikeThisQuery extends Query TokenStream ts=analyzer.tokenStream(f.fieldName,new StringReader(f.queryString)); Token token=ts.next(); int corpusNumDocs=reader.numDocs(); - Term internSavingTemplateTerm =new Term(f.fieldName,""); //optimization to avoid constructing new Term() objects + Term internSavingTemplateTerm =new Term(f.fieldName); //optimization to avoid constructing new Term() objects HashSet processedTerms=new HashSet(); while(token!=null) { diff --git a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java index 9a05ea52477..eb87a5168fa 100755 --- a/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java +++ b/contrib/spellchecker/src/java/org/apache/lucene/search/spell/LuceneDictionary.java @@ -57,7 +57,7 @@ public class LuceneDictionary implements Dictionary { LuceneIterator() { try { - termEnum = reader.terms(new Term(field, "")); + termEnum = reader.terms(new Term(field)); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/java/org/apache/lucene/index/ParallelReader.java b/src/java/org/apache/lucene/index/ParallelReader.java index 265451bc426..db9f0dfa816 100644 --- a/src/java/org/apache/lucene/index/ParallelReader.java +++ b/src/java/org/apache/lucene/index/ParallelReader.java @@ -486,7 +486,7 @@ public class ParallelReader extends IndexReader { } while (fieldIterator.hasNext()) { field = (String) fieldIterator.next(); - termEnum = ((IndexReader)fieldToReader.get(field)).terms(new Term(field, "")); + termEnum = ((IndexReader)fieldToReader.get(field)).terms(new Term(field)); Term term = termEnum.term(); if (term!=null && term.field()==field) return true; diff --git a/src/java/org/apache/lucene/index/Term.java b/src/java/org/apache/lucene/index/Term.java index 1c4accdbfe6..23d2569aa5f 100644 --- a/src/java/org/apache/lucene/index/Term.java +++ b/src/java/org/apache/lucene/index/Term.java @@ -35,6 +35,17 @@ public final class Term implements Comparable, java.io.Serializable { public Term(String fld, String txt) { this(fld, txt, true); } + + /** Constructs a Term with the given field and empty text. + * This serves two purposes: 1) reuse of a Term with the same field. + * 2) pattern for a query. + * + * @param fld + */ + public Term(String fld) { + this(fld, "", true); + } + Term(String fld, String txt, boolean intern) { field = intern ? fld.intern() : fld; // field names are interned text = txt; // unless already known to be diff --git a/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java b/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java index a189b266d76..64f1953178e 100644 --- a/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java +++ b/src/java/org/apache/lucene/search/ExtendedFieldCacheImpl.java @@ -45,7 +45,7 @@ class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCach LongParser parser = (LongParser) entry.custom; final long[] retArray = new long[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term(field, "")); + TermEnum termEnum = reader.terms (new Term(field)); try { do { Term term = termEnum.term(); @@ -85,7 +85,7 @@ class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCach DoubleParser parser = (DoubleParser) entry.custom; final double[] retArray = new double[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -115,7 +115,7 @@ class ExtendedFieldCacheImpl extends FieldCacheImpl implements ExtendedFieldCach protected Object createValue(IndexReader reader, Object fieldKey) throws IOException { String field = ((String)fieldKey).intern(); - TermEnum enumerator = reader.terms (new Term (field, "")); + TermEnum enumerator = reader.terms (new Term (field)); try { Term term = enumerator.term(); if (term == null) { diff --git a/src/java/org/apache/lucene/search/FieldCacheImpl.java b/src/java/org/apache/lucene/search/FieldCacheImpl.java index 4ac9f91c8e7..6962a02adbb 100644 --- a/src/java/org/apache/lucene/search/FieldCacheImpl.java +++ b/src/java/org/apache/lucene/search/FieldCacheImpl.java @@ -176,7 +176,7 @@ implements FieldCache { ByteParser parser = (ByteParser) entry.custom; final byte[] retArray = new byte[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -215,7 +215,7 @@ implements FieldCache { ShortParser parser = (ShortParser) entry.custom; final short[] retArray = new short[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -254,7 +254,7 @@ implements FieldCache { IntParser parser = (IntParser) entry.custom; final int[] retArray = new int[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -295,7 +295,7 @@ implements FieldCache { FloatParser parser = (FloatParser) entry.custom; final float[] retArray = new float[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -327,7 +327,7 @@ implements FieldCache { String field = ((String) fieldKey).intern(); final String[] retArray = new String[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); @@ -360,7 +360,7 @@ implements FieldCache { final int[] retArray = new int[reader.maxDoc()]; String[] mterms = new String[reader.maxDoc()+1]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); int t = 0; // current term number // an entry for documents that have no terms in this field @@ -431,7 +431,7 @@ implements FieldCache { protected Object createValue(IndexReader reader, Object fieldKey) throws IOException { String field = ((String)fieldKey).intern(); - TermEnum enumerator = reader.terms (new Term (field, "")); + TermEnum enumerator = reader.terms (new Term (field)); try { Term term = enumerator.term(); if (term == null) { @@ -488,7 +488,7 @@ implements FieldCache { SortComparator comparator = (SortComparator) entry.custom; final Comparable[] retArray = new Comparable[reader.maxDoc()]; TermDocs termDocs = reader.termDocs(); - TermEnum termEnum = reader.terms (new Term (field, "")); + TermEnum termEnum = reader.terms (new Term (field)); try { do { Term term = termEnum.term(); diff --git a/src/java/org/apache/lucene/search/RangeFilter.java b/src/java/org/apache/lucene/search/RangeFilter.java index 049f1fc8c21..42281333229 100644 --- a/src/java/org/apache/lucene/search/RangeFilter.java +++ b/src/java/org/apache/lucene/search/RangeFilter.java @@ -102,7 +102,7 @@ public class RangeFilter extends Filter { TermEnum enumerator = (null != lowerTerm ? reader.terms(new Term(fieldName, lowerTerm)) - : reader.terms(new Term(fieldName,""))); + : reader.terms(new Term(fieldName))); try { @@ -164,7 +164,7 @@ public class RangeFilter extends Filter { TermEnum enumerator = (null != lowerTerm ? reader.terms(new Term(fieldName, lowerTerm)) - : reader.terms(new Term(fieldName,""))); + : reader.terms(new Term(fieldName))); try { diff --git a/src/java/org/apache/lucene/search/RangeQuery.java b/src/java/org/apache/lucene/search/RangeQuery.java index 30e4c405154..c61b73b7121 100644 --- a/src/java/org/apache/lucene/search/RangeQuery.java +++ b/src/java/org/apache/lucene/search/RangeQuery.java @@ -69,7 +69,7 @@ public class RangeQuery extends Query this.lowerTerm = lowerTerm; } else { - this.lowerTerm = new Term(upperTerm.field(), ""); + this.lowerTerm = new Term(upperTerm.field()); } this.upperTerm = upperTerm;