BLUR-44 Signed-off-by: Aaron McCurry <[email protected]>
Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/a6109b3b Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/a6109b3b Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/a6109b3b Branch: refs/heads/0.2-dev Commit: a6109b3b0431682feec39ebf1651f04d8b4bbfab Parents: dbb5df6 Author: Gagan <[email protected]> Authored: Mon Dec 17 07:26:01 2012 -0500 Committer: Aaron McCurry <[email protected]> Committed: Mon Dec 17 07:41:34 2012 -0500 ---------------------------------------------------------------------- .../org/apache/blur/analysis/BlurAnalyzer.java | 81 +++++-- .../org/apache/blur/analysis/FieldManager.java | 173 +++++++++++++++ 2 files changed, 230 insertions(+), 24 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a6109b3b/src/blur-core/src/main/java/org/apache/blur/analysis/BlurAnalyzer.java ---------------------------------------------------------------------- diff --git a/src/blur-core/src/main/java/org/apache/blur/analysis/BlurAnalyzer.java b/src/blur-core/src/main/java/org/apache/blur/analysis/BlurAnalyzer.java index c6dd23f..5bde2c5 100644 --- a/src/blur-core/src/main/java/org/apache/blur/analysis/BlurAnalyzer.java +++ b/src/blur-core/src/main/java/org/apache/blur/analysis/BlurAnalyzer.java @@ -19,20 +19,19 @@ package org.apache.blur.analysis; import static org.apache.blur.lucene.LuceneVersionConstant.LUCENE_VERSION; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.blur.thrift.generated.AnalyzerSubType; +import org.apache.blur.thrift.generated.AnalyzerType; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.AnalyzerWrapper; -import org.apache.lucene.analysis.core.KeywordAnalyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; -import org.apache.lucene.document.Field.Store; public final class BlurAnalyzer extends AnalyzerWrapper { - - public static final BlurAnalyzer BLANK_ANALYZER = new BlurAnalyzer(new KeywordAnalyzer()); - private Map<String, Store> _storeMap = new HashMap<String, Store>(); private Map<String, Set<String>> _subIndexNameLookups = new HashMap<String, Set<String>>(); private Analyzer _defaultAnalyzer; private Map<String, Analyzer> _analyzers = new HashMap<String, Analyzer>(); @@ -50,11 +49,59 @@ public final class BlurAnalyzer extends AnalyzerWrapper { } public BlurAnalyzer(org.apache.blur.thrift.generated.Analyzer analyzer) { - this(convert(analyzer)); + _defaultAnalyzer = new StandardAnalyzer(LUCENE_VERSION); + convert(analyzer); } - private static Analyzer convert(org.apache.blur.thrift.generated.Analyzer analyzer) { - return new StandardAnalyzer(LUCENE_VERSION); + private void convert(org.apache.blur.thrift.generated.Analyzer analyzer) { + List<AnalyzerType> analyzerTypes = analyzer.getAnalyzerTypes(); + try { + for (AnalyzerType analyzerType : analyzerTypes) { + String indexName = analyzerType.getFieldName(); + org.apache.blur.thrift.generated.ClassDefinition analyzerClassDefinition = analyzerType.getClassDefinition(); + if (analyzerClassDefinition != null) { + String analyzerClassName = analyzerClassDefinition.getClassName(); + Object analyzerClassObject; + + analyzerClassObject = analyzerClassName == null ? null : Class.forName(analyzerClassName).newInstance(); + + if (analyzerClassObject != null) { + if (analyzerClassObject instanceof Analyzer) { + _analyzers.put(indexName, (Analyzer) analyzerClassObject); + } else if (!(analyzerClassObject instanceof IndexableFieldFactory)) { + throw new RuntimeException("Blur Analyzer can not be created."); + } + } + List<AnalyzerSubType> analyzerSubTypes = analyzerType.getAnalyzerSubTypes(); + if (analyzerSubTypes != null && analyzerSubTypes.size() > 0) { + HashSet<String> subIndexNames = new HashSet<String>(); + for (AnalyzerSubType analyzerSubType : analyzerSubTypes) { + String subIndexName = analyzerSubType.getSubFieldName(); + String subAnalyzerClassName = analyzerSubType.getClassDefinition().getClassName(); + Object subAnalyzerClassObject = subAnalyzerClassName == null ? null : Class.forName(subAnalyzerClassName).newInstance(); + if (subAnalyzerClassObject != null) { + if (subAnalyzerClassObject instanceof Analyzer) { + _analyzers.put(indexName, (Analyzer) subAnalyzerClassObject); + subIndexNames.add(subIndexName); + } else if (!(analyzerClassObject instanceof IndexableFieldFactory)) { + throw new RuntimeException("Blur Analyzer can not be created."); + } + } + } + if (subIndexNames != null && subIndexNames.size() > 0) { + _subIndexNameLookups.put(indexName, subIndexNames); + } + + } + } + } + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } } private Analyzer getAnalyzer(String name) { @@ -62,19 +109,10 @@ public final class BlurAnalyzer extends AnalyzerWrapper { return analyzer; } - public Store getStore(String indexName) { - Store store = _storeMap.get(indexName); - if (store == null) { - return Store.YES; - } - return store; - } - - public void close() { } - + @Override protected Analyzer getWrappedAnalyzer(String fieldName) { Analyzer analyzer = getAnalyzer(fieldName); @@ -86,9 +124,4 @@ public final class BlurAnalyzer extends AnalyzerWrapper { return components; } - - - - - -} +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a6109b3b/src/blur-core/src/main/java/org/apache/blur/analysis/FieldManager.java ---------------------------------------------------------------------- diff --git a/src/blur-core/src/main/java/org/apache/blur/analysis/FieldManager.java b/src/blur-core/src/main/java/org/apache/blur/analysis/FieldManager.java new file mode 100644 index 0000000..2c5e10d --- /dev/null +++ b/src/blur-core/src/main/java/org/apache/blur/analysis/FieldManager.java @@ -0,0 +1,173 @@ +package org.apache.blur.analysis; + +import java.io.IOException; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.blur.thrift.generated.AnalyzerSubType; +import org.apache.blur.thrift.generated.AnalyzerType; +import org.apache.blur.thrift.generated.Field; +import org.apache.blur.thrift.generated.TYPE; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.document.DoubleField; +import org.apache.lucene.document.Field.Store; +import org.apache.lucene.document.FieldType; +import org.apache.lucene.document.FloatField; +import org.apache.lucene.document.IntField; +import org.apache.lucene.document.LongField; +import org.apache.lucene.document.StoredField; +import org.apache.lucene.document.StringField; +import org.apache.lucene.document.TextField; +import org.apache.lucene.index.IndexableField; + +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with this + * work for additional information regarding copyright ownership. The ASF + * licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +public class FieldManager { + + private Map<String, IndexableFieldFactory> indexableFieldFactories = new HashMap<String, IndexableFieldFactory>(); + private Map<String, Set<String>> subIndexNameLookups = new HashMap<String, Set<String>>(); + + + public FieldManager() { + + } + + public FieldManager(org.apache.blur.thrift.generated.Analyzer analyzer) { + convert(analyzer); + } + + private void convert(org.apache.blur.thrift.generated.Analyzer analyzer) { + List<AnalyzerType> analyzerTypes = analyzer.getAnalyzerTypes(); + try { + for (AnalyzerType analyzerType : analyzerTypes) { + String indexName = analyzerType.getFieldName(); + org.apache.blur.thrift.generated.ClassDefinition analyzerClassDefinition = analyzerType + .getClassDefinition(); + if (analyzerClassDefinition != null) { + String indexableFieldFactoryClassName = analyzerClassDefinition.getClassName(); + Object indexableFieldFactoryObject = (indexableFieldFactoryClassName == null) ? null : Class + .forName(indexableFieldFactoryClassName).newInstance(); + + if (indexableFieldFactoryObject != null + && indexableFieldFactoryObject instanceof IndexableFieldFactory) { + indexableFieldFactories.put(indexName, + (IndexableFieldFactory) indexableFieldFactoryObject); + } + } + List<AnalyzerSubType> analyzerSubTypes = analyzerType + .getAnalyzerSubTypes(); + if (analyzerSubTypes != null && analyzerSubTypes.size() > 0) { + HashSet<String> subIndexNames = new HashSet<String>(); + + for (AnalyzerSubType analyzerSubType : analyzerSubTypes) { + String subIndexName = analyzerSubType.getSubFieldName(); + String subIndexableFieldFactoryClassName = analyzerSubType.getClassDefinition() + .getClassName(); + Object subIndexableFieldFactoryObject = subIndexableFieldFactoryClassName == null ? null + : Class.forName(subIndexableFieldFactoryClassName).newInstance(); + if (subIndexableFieldFactoryObject != null + && subIndexableFieldFactoryObject instanceof Analyzer) { + indexableFieldFactories.put(indexName,(IndexableFieldFactory) subIndexableFieldFactoryObject); + subIndexNames.add(subIndexName); + } + } + subIndexNameLookups.put(indexName, subIndexNames); + } + } + } catch (InstantiationException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + public IndexableField getIndexableField(Field field){ + IndexableFieldFactory factory = indexableFieldFactories.get(field.getName()); + return factory.getIndexableField(field) == null ? null : factory.getIndexableField(field); + } + + public IndexableFieldFactory getIndexableFieldFactory(String fieldName){ + IndexableFieldFactory factory = indexableFieldFactories.get(fieldName); + return factory == null ? null : factory; + } + + public Set<String> getSubIndexNames(String indexName) { + return subIndexNameLookups.get(indexName); + } + + public static IndexableField toLucene(Field field) throws IOException { + TYPE type = field.getType(); + org.apache.lucene.document.Field result; + switch (type) { + case STRING: + result = new StringField(field.getName(), toString(field.getValue()), Store.YES); + break; + case TEXT: + result = new TextField(field.getName(), toString(field.getValue()), Store.YES); + break; + case INT: + result = new IntField(field.getName(), toInteger(field.getValue()), Store.YES); + break; + case FLOAT: + result = new FloatField(field.getName(), toFloat(field.getValue()), Store.YES); + break; + case LONG: + result = new LongField(field.getName(), toLong(field.getValue()), Store.YES); + break; + case DOUBLE: + result = new DoubleField(field.getName(), toDouble(field.getValue()), Store.YES); + break; + case BINARY: + result = new StoredField(field.getName(), field.getValue()); + break; + default: + throw new RuntimeException("Not supported"); + } + FieldType fieldType = result.fieldType(); + if (fieldType.indexed() && !fieldType.omitNorms()) { + result.setBoost((float) field.getBoost()); + } + return result; + } + + public static double toDouble(byte[] value) { + throw new RuntimeException("Not implemented"); + } + + public static long toLong(byte[] value) { + throw new RuntimeException("Not implemented"); + } + + public static float toFloat(byte[] value) { + throw new RuntimeException("Not implemented"); + } + + public static int toInteger(byte[] value) { + throw new RuntimeException("Not implemented"); + } + + public static String toString(byte[] value) throws IOException { + return new String(value, "UTF-8"); + } + +} \ No newline at end of file
