From 414751f445a7707acd116bf4309cd52c80b0bb35 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Fri, 5 Nov 2010 22:16:34 +0000 Subject: [PATCH] SOLR-792: fix pivot facet native type representation via FieldType.toObject(indexedTerm) git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1031854 13f79535-47bb-0310-9956-ffa450edef68 --- .../handler/component/PivotFacetHelper.java | 18 ++--- .../org/apache/solr/schema/BoolField.java | 5 ++ .../org/apache/solr/schema/FieldType.java | 7 ++ .../java/org/apache/solr/schema/StrField.java | 72 ++----------------- .../org/apache/solr/schema/TextField.java | 7 ++ .../org/apache/solr/schema/TrieDateField.java | 5 ++ .../org/apache/solr/schema/TrieField.java | 18 +++++ 7 files changed, 58 insertions(+), 74 deletions(-) diff --git a/solr/src/java/org/apache/solr/handler/component/PivotFacetHelper.java b/solr/src/java/org/apache/solr/handler/component/PivotFacetHelper.java index 96120da0f92..aa368778847 100644 --- a/solr/src/java/org/apache/solr/handler/component/PivotFacetHelper.java +++ b/solr/src/java/org/apache/solr/handler/component/PivotFacetHelper.java @@ -17,6 +17,8 @@ package org.apache.solr.handler.component; +import org.apache.lucene.util.BytesRef; +import org.apache.solr.schema.SchemaField; import org.apache.solr.search.SolrIndexSearcher; import org.apache.solr.search.DocSet; import org.apache.solr.common.SolrException; @@ -95,10 +97,8 @@ public class PivotFacetHelper { SolrIndexSearcher searcher = rb.req.getSearcher(); // TODO: optimize to avoid converting to an external string and then having to convert back to internal below - FieldType ftype = searcher.getSchema().getField(field).getType(); - - // Required to translate back to an object - Field f = new Field( field, "X", Store.YES, Index.ANALYZED ); + SchemaField sfield = searcher.getSchema().getField(field); + FieldType ftype = sfield.getType(); String nextField = fnames.poll(); @@ -106,19 +106,21 @@ public class PivotFacetHelper for (Map.Entry kv : superFacets) { // Only sub-facet if parent facet has positive count - still may not be any values for the sub-field though if (kv.getValue() > minMatch ) { - String internal = ftype.toInternal( kv.getKey() ); - f.setValue( internal ); + // don't reuse the same BytesRef each time since we will be constructing Term + // objects that will most likely be cached. + BytesRef termval = new BytesRef(); + ftype.readableToIndexed(kv.getKey(), termval); SimpleOrderedMap pivot = new SimpleOrderedMap(); pivot.add( "field", field ); - pivot.add( "value", ftype.toObject( f ) ); + pivot.add( "value", ftype.toObject(sfield, termval) ); pivot.add( "count", kv.getValue() ); if( subField == null ) { values.add( pivot ); } else { - Query query = new TermQuery(new Term(field, internal)); + Query query = new TermQuery(new Term(field, termval)); DocSet subset = searcher.getDocSet(query, docs); SimpleFacets sf = getFacetImplementation(rb.req, subset, rb.req.getParams()); diff --git a/solr/src/java/org/apache/solr/schema/BoolField.java b/solr/src/java/org/apache/solr/schema/BoolField.java index 9130c2cb751..0ab4c260a3b 100644 --- a/solr/src/java/org/apache/solr/schema/BoolField.java +++ b/solr/src/java/org/apache/solr/schema/BoolField.java @@ -109,6 +109,11 @@ public class BoolField extends FieldType { return Boolean.valueOf( toExternal(f) ); } + @Override + public Object toObject(SchemaField sf, BytesRef term) { + return term.bytes[0] == 'T'; + } + public String indexedToReadable(String indexedForm) { char ch = indexedForm.charAt(0); return ch=='T' ? "true" : "false"; diff --git a/solr/src/java/org/apache/solr/schema/FieldType.java b/solr/src/java/org/apache/solr/schema/FieldType.java index a762885ecc5..94e01364b05 100644 --- a/solr/src/java/org/apache/solr/schema/FieldType.java +++ b/solr/src/java/org/apache/solr/schema/FieldType.java @@ -346,6 +346,13 @@ public abstract class FieldType extends FieldProperties { return toExternal(f); // by default use the string } + public Object toObject(SchemaField sf, BytesRef term) { + CharArr ext = new CharArr(term.length); + indexedToReadable(term, ext); + Field f = createField(sf, ext.toString(), 1.0f); + return toObject(f); + } + /** Given an indexed term, return the human readable representation */ public String indexedToReadable(String indexedForm) { return indexedForm; diff --git a/solr/src/java/org/apache/solr/schema/StrField.java b/solr/src/java/org/apache/solr/schema/StrField.java index 33b1f6f6efe..e90807131ea 100644 --- a/solr/src/java/org/apache/solr/schema/StrField.java +++ b/solr/src/java/org/apache/solr/schema/StrField.java @@ -28,6 +28,7 @@ import org.apache.solr.search.function.FieldCacheSource; import org.apache.solr.search.function.DocValues; import org.apache.solr.search.function.StringIndexDocValues; import org.apache.solr.search.QParser; +import org.apache.solr.util.ByteUtils; import java.util.Map; import java.io.IOException; @@ -54,72 +55,11 @@ public class StrField extends FieldType { public ValueSource getValueSource(SchemaField field, QParser parser) { return new StrFieldSource(field.getName()); } + + @Override + public Object toObject(SchemaField sf, BytesRef term) { + return ByteUtils.UTF8toUTF16(term); + } } -class StrFieldSource extends FieldCacheSource { - - public StrFieldSource(String field) { - super(field); - } - - public String description() { - return "str(" + field + ')'; - } - - public DocValues getValues(Map context, IndexReader reader) throws IOException { - return new StringIndexDocValues(this, reader, field) { - protected String toTerm(String readableValue) { - return readableValue; - } - - public float floatVal(int doc) { - return (float)intVal(doc); - } - - public int intVal(int doc) { - int ord=termsIndex.getOrd(doc); - return ord; - } - - public long longVal(int doc) { - return (long)intVal(doc); - } - - public double doubleVal(int doc) { - return (double)intVal(doc); - } - - public int ordVal(int doc) { - return termsIndex.getOrd(doc); - } - - public int numOrd() { - return termsIndex.numOrd(); - } - - public String strVal(int doc) { - int ord=termsIndex.getOrd(doc); - if (ord == 0) { - return null; - } else { - return termsIndex.lookup(ord, new BytesRef()).utf8ToString(); - } - } - - public String toString(int doc) { - return description() + '=' + strVal(doc); - } - }; - } - - public boolean equals(Object o) { - return o instanceof StrFieldSource - && super.equals(o); - } - - private static int hcode = SortableFloatFieldSource.class.hashCode(); - public int hashCode() { - return hcode + super.hashCode(); - }; -} \ No newline at end of file diff --git a/solr/src/java/org/apache/solr/schema/TextField.java b/solr/src/java/org/apache/solr/schema/TextField.java index ce4876e32e9..77513464d66 100644 --- a/solr/src/java/org/apache/solr/schema/TextField.java +++ b/solr/src/java/org/apache/solr/schema/TextField.java @@ -31,9 +31,11 @@ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; import org.apache.lucene.analysis.CachingTokenFilter; import org.apache.lucene.analysis.TokenStream; import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.util.BytesRef; import org.apache.solr.response.TextResponseWriter; import org.apache.solr.response.XMLWriter; import org.apache.solr.search.QParser; +import org.apache.solr.util.ByteUtils; import java.util.Map; import java.util.List; @@ -78,6 +80,11 @@ public class TextField extends FieldType { return parseFieldQuery(parser, getQueryAnalyzer(), field.getName(), externalVal); } + @Override + public Object toObject(SchemaField sf, BytesRef term) { + return ByteUtils.UTF8toUTF16(term); + } + static Query parseFieldQuery(QParser parser, Analyzer analyzer, String field, String queryText) { int phraseSlop = 0; diff --git a/solr/src/java/org/apache/solr/schema/TrieDateField.java b/solr/src/java/org/apache/solr/schema/TrieDateField.java index 953d40b58cf..24dcdc7c4ae 100755 --- a/solr/src/java/org/apache/solr/schema/TrieDateField.java +++ b/solr/src/java/org/apache/solr/schema/TrieDateField.java @@ -72,6 +72,11 @@ public class TrieDateField extends DateField { return new Date(TrieField.toLong(arr)); } + @Override + public Object toObject(SchemaField sf, BytesRef term) { + return new Date(NumericUtils.prefixCodedToLong(term)); + } + @Override public SortField getSortField(SchemaField field, boolean top) { return new SortField(new LongValuesCreator( field.getName(), FieldCache.NUMERIC_UTILS_LONG_PARSER, CachedArrayCreator.CACHE_VALUES_AND_BITS ), top); diff --git a/solr/src/java/org/apache/solr/schema/TrieField.java b/solr/src/java/org/apache/solr/schema/TrieField.java index f5ade52e4e6..4d88e7062c0 100644 --- a/solr/src/java/org/apache/solr/schema/TrieField.java +++ b/solr/src/java/org/apache/solr/schema/TrieField.java @@ -479,6 +479,24 @@ public class TrieField extends FieldType { out.write(s); } + @Override + public Object toObject(SchemaField sf, BytesRef term) { + switch (type) { + case INTEGER: + return NumericUtils.prefixCodedToInt(term); + case FLOAT: + return NumericUtils.sortableIntToFloat(NumericUtils.prefixCodedToInt(term)); + case LONG: + return NumericUtils.prefixCodedToLong(term); + case DOUBLE: + return NumericUtils.sortableLongToDouble(NumericUtils.prefixCodedToLong(term)); + case DATE: + return new Date(NumericUtils.prefixCodedToLong(term)); + default: + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type); + } + } + @Override public String storedToIndexed(Fieldable f) { // TODO: optimize to remove redundant string conversion