From 8e4c139c8bc7757eba259ce76261f8e6d8eeeb6d Mon Sep 17 00:00:00 2001 From: kimchy Date: Thu, 17 Jun 2010 23:23:53 +0300 Subject: [PATCH] Analysis: fields that are `not_analyzed` should automatically default to keyword analyzer, closes #229. --- .../org/elasticsearch/common/lucene/Lucene.java | 7 ++++--- .../elasticsearch/index/analysis/AnalyzerScope.java | 3 ++- .../elasticsearch/index/analysis/NamedAnalyzer.java | 4 ++++ .../index/mapper/xcontent/XContentFieldMapper.java | 13 +++++++++++-- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/Lucene.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/Lucene.java index ff7e75c725a..25a3381f010 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/Lucene.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/Lucene.java @@ -30,12 +30,13 @@ import org.apache.lucene.util.Version; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.trove.TIntArrayList; +import org.elasticsearch.index.analysis.AnalyzerScope; import org.elasticsearch.index.analysis.NamedAnalyzer; import java.io.IOException; /** - * @author kimchy (Shay Banon) + * @author kimchy (shay.banon) */ public class Lucene { @@ -43,8 +44,8 @@ public class Lucene { public static Version ANALYZER_VERSION = VERSION; public static Version QUERYPARSER_VERSION = VERSION; - public static final NamedAnalyzer STANDARD_ANALYZER = new NamedAnalyzer("_standard", new StandardAnalyzer(ANALYZER_VERSION)); - public static final NamedAnalyzer KEYWORD_ANALYZER = new NamedAnalyzer("_keyword", new KeywordAnalyzer()); + public static final NamedAnalyzer STANDARD_ANALYZER = new NamedAnalyzer("_standard", AnalyzerScope.GLOBAL, new StandardAnalyzer(ANALYZER_VERSION)); + public static final NamedAnalyzer KEYWORD_ANALYZER = new NamedAnalyzer("_keyword", AnalyzerScope.GLOBAL, new KeywordAnalyzer()); public static final int NO_DOC = -1; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalyzerScope.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalyzerScope.java index c9c3f65f3b2..08ea93e01fe 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalyzerScope.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalyzerScope.java @@ -24,5 +24,6 @@ package org.elasticsearch.index.analysis; */ public enum AnalyzerScope { INDEX, - INDICES + INDICES, + GLOBAL } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java index fe9f78d4778..e9a4bbcd145 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/NamedAnalyzer.java @@ -92,4 +92,8 @@ public class NamedAnalyzer extends Analyzer { @Override public void close() { analyzer.close(); } + + @Override public String toString() { + return "analyzer name[" + name + "], analyzer [" + analyzer + "]"; + } } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentFieldMapper.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentFieldMapper.java index 261ec57a6f9..b88114ffa40 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentFieldMapper.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/XContentFieldMapper.java @@ -24,6 +24,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.document.Fieldable; import org.apache.lucene.index.Term; import org.apache.lucene.search.*; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.lucene.search.TermFilter; import org.elasticsearch.common.xcontent.builder.XContentBuilder; import org.elasticsearch.index.analysis.NamedAnalyzer; @@ -211,8 +212,16 @@ public abstract class XContentFieldMapper implements FieldMapper, XContent this.boost = boost; this.omitNorms = omitNorms; this.omitTermFreqAndPositions = omitTermFreqAndPositions; - this.indexAnalyzer = indexAnalyzer; - this.searchAnalyzer = searchAnalyzer; + if (indexAnalyzer == null && !index.isAnalyzed()) { + this.indexAnalyzer = Lucene.KEYWORD_ANALYZER; + } else { + this.indexAnalyzer = indexAnalyzer; + } + if (searchAnalyzer == null && !index.isAnalyzed()) { + this.searchAnalyzer = Lucene.KEYWORD_ANALYZER; + } else { + this.searchAnalyzer = searchAnalyzer; + } } @Override public String name() {