浏览代码

rename limit to ignore_above, and create a dedicated test

Shay Banon 13 年之前
父节点
当前提交
bbc45fefe5

+ 14 - 14
src/main/java/org/elasticsearch/index/mapper/core/StringFieldMapper.java

@@ -49,7 +49,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
         // NOTE, when adding defaults here, make sure you add them in the builder
         // NOTE, when adding defaults here, make sure you add them in the builder
         public static final String NULL_VALUE = null;
         public static final String NULL_VALUE = null;
         public static final int POSITION_OFFSET_GAP = 0;
         public static final int POSITION_OFFSET_GAP = 0;
-        public static final int DEFAULT_LIMIT = -1;
+        public static final int IGNORE_ABOVE = -1;
     }
     }
 
 
     public static class Builder extends AbstractFieldMapper.OpenBuilder<Builder, StringFieldMapper> {
     public static class Builder extends AbstractFieldMapper.OpenBuilder<Builder, StringFieldMapper> {
@@ -60,7 +60,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
 
 
         protected NamedAnalyzer searchQuotedAnalyzer;
         protected NamedAnalyzer searchQuotedAnalyzer;
 
 
-        protected int limit = Defaults.DEFAULT_LIMIT;
+        protected int ignoreAbove = Defaults.IGNORE_ABOVE;
 
 
         public Builder(String name) {
         public Builder(String name) {
             super(name);
             super(name);
@@ -97,8 +97,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
             return builder;
             return builder;
         }
         }
 
 
-        public Builder limit(int limit) {
-            this.limit = limit;
+        public Builder ignoreAbove(int ignoreAbove) {
+            this.ignoreAbove = ignoreAbove;
             return this;
             return this;
         }
         }
 
 
@@ -111,7 +111,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
             }
             }
             StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
             StringFieldMapper fieldMapper = new StringFieldMapper(buildNames(context),
                     index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue,
                     index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue,
-                    indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, limit);
+                    indexAnalyzer, searchAnalyzer, searchQuotedAnalyzer, positionOffsetGap, ignoreAbove);
             fieldMapper.includeInAll(includeInAll);
             fieldMapper.includeInAll(includeInAll);
             return fieldMapper;
             return fieldMapper;
         }
         }
@@ -146,8 +146,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
                     if (builder.searchQuotedAnalyzer == null) {
                     if (builder.searchQuotedAnalyzer == null) {
                         builder.searchQuotedAnalyzer = parserContext.analysisService().defaultSearchQuoteAnalyzer();
                         builder.searchQuotedAnalyzer = parserContext.analysisService().defaultSearchQuoteAnalyzer();
                     }
                     }
-                } else if (propName.equals("limit")) {
-                    builder.limit(XContentMapValues.nodeIntegerValue(propNode, -1));
+                } else if (propName.equals("ignore_above")) {
+                    builder.ignoreAbove(XContentMapValues.nodeIntegerValue(propNode, -1));
                 }
                 }
             }
             }
             return builder;
             return builder;
@@ -162,24 +162,24 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
 
 
     private NamedAnalyzer searchQuotedAnalyzer;
     private NamedAnalyzer searchQuotedAnalyzer;
 
 
-    private int limit;
+    private int ignoreAbove;
 
 
     protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
     protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
                                 float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
                                 float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
                                 String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
                                 String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer) {
         this(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer,
         this(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, nullValue, indexAnalyzer,
-                searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP, Defaults.DEFAULT_LIMIT);
+                searchAnalyzer, searchAnalyzer, Defaults.POSITION_OFFSET_GAP, Defaults.IGNORE_ABOVE);
     }
     }
 
 
     protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
     protected StringFieldMapper(Names names, Field.Index index, Field.Store store, Field.TermVector termVector,
                                 float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
                                 float boost, boolean omitNorms, boolean omitTermFreqAndPositions,
                                 String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer,
                                 String nullValue, NamedAnalyzer indexAnalyzer, NamedAnalyzer searchAnalyzer,
-                                NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap, int limit) {
+                                NamedAnalyzer searchQuotedAnalyzer, int positionOffsetGap, int ignoreAbove) {
         super(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, indexAnalyzer, searchAnalyzer);
         super(names, index, store, termVector, boost, omitNorms, omitTermFreqAndPositions, indexAnalyzer, searchAnalyzer);
         this.nullValue = nullValue;
         this.nullValue = nullValue;
         this.positionOffsetGap = positionOffsetGap;
         this.positionOffsetGap = positionOffsetGap;
         this.searchQuotedAnalyzer = searchQuotedAnalyzer != null ? searchQuotedAnalyzer : this.searchAnalyzer;
         this.searchQuotedAnalyzer = searchQuotedAnalyzer != null ? searchQuotedAnalyzer : this.searchAnalyzer;
-        this.limit = limit;
+        this.ignoreAbove = ignoreAbove;
     }
     }
 
 
     @Override
     @Override
@@ -269,7 +269,7 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
         if (value == null) {
         if (value == null) {
             return null;
             return null;
         }
         }
-        if (limit > 0 && value.length() > limit) {
+        if (ignoreAbove > 0 && value.length() > ignoreAbove) {
             return null;
             return null;
         }
         }
         if (context.includeInAll(includeInAll, this)) {
         if (context.includeInAll(includeInAll, this)) {
@@ -331,8 +331,8 @@ public class StringFieldMapper extends AbstractFieldMapper<String> implements Al
         if (searchQuotedAnalyzer != null && searchAnalyzer != searchQuotedAnalyzer) {
         if (searchQuotedAnalyzer != null && searchAnalyzer != searchQuotedAnalyzer) {
             builder.field("search_quote_analyzer", searchQuotedAnalyzer.name());
             builder.field("search_quote_analyzer", searchQuotedAnalyzer.name());
         }
         }
-        if (limit != Defaults.DEFAULT_LIMIT) {
-            builder.field("limit", limit);
+        if (ignoreAbove != Defaults.IGNORE_ABOVE) {
+            builder.field("ignore_above", ignoreAbove);
         }
         }
     }
     }
 }
 }

+ 0 - 15
src/test/java/org/elasticsearch/test/unit/index/mapper/simple/SimpleMapperTests.java

@@ -120,19 +120,4 @@ public class SimpleMapperTests {
         DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
         DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
         assertThat((String) builtDocMapper.meta().get("param1"), equalTo("value1"));
         assertThat((String) builtDocMapper.meta().get("param1"), equalTo("value1"));
     }
     }
-
-    @Test
-    public void testLimit() throws Exception {
-        String mapping = copyToStringFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test-mapping.json");
-        DocumentMapper docMapper = MapperTests.newParser().parse(mapping);
-        String builtMapping = docMapper.mappingSource().string();
-
-        // reparse it
-        DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
-        BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/test/unit/index/mapper/simple/test1.json"));
-        Document doc = builtDocMapper.parse(json).rootDoc();
-        assertThat(doc.get(docMapper.uidMapper().names().indexName()), equalTo(Uid.createUid("person", "1")));
-        assertThat(doc.getValues("cats").length, equalTo(1));
-        assertThat(doc.getValues("cats")[0], equalTo("felix"));
-    }
 }
 }

+ 0 - 5
src/test/java/org/elasticsearch/test/unit/index/mapper/simple/test-mapping.json

@@ -71,11 +71,6 @@
                 type:"string",
                 type:"string",
                 index_name:"dog"
                 index_name:"dog"
             },
             },
-            cats:{
-                type:"string",
-                index_name:"cats",
-                limit:10
-            },
             complex:{
             complex:{
                 type:"object",
                 type:"object",
                 properties:{
                 properties:{

+ 0 - 1
src/test/java/org/elasticsearch/test/unit/index/mapper/simple/test1.json

@@ -18,7 +18,6 @@
         birthDate:"1977-11-15",
         birthDate:"1977-11-15",
         nerd:true,
         nerd:true,
         dogs:["buck", "mia"],
         dogs:["buck", "mia"],
-        cats:["name to long for a cat", "felix"],
         complex:[
         complex:[
             {
             {
                 value1:"value1"
                 value1:"value1"

+ 69 - 0
src/test/java/org/elasticsearch/test/unit/index/mapper/string/SimpleStringMappingTests.java

@@ -0,0 +1,69 @@
+/*
+ * Licensed to ElasticSearch and Shay Banon under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. ElasticSearch 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.
+ */
+
+package org.elasticsearch.test.unit.index.mapper.string;
+
+import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.index.mapper.DocumentMapper;
+import org.elasticsearch.index.mapper.ParsedDocument;
+import org.elasticsearch.test.unit.index.mapper.MapperTests;
+import org.testng.annotations.Test;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+
+/**
+ */
+@Test
+public class SimpleStringMappingTests {
+
+    @Test
+    public void testLimit() throws Exception {
+        String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
+                .startObject("properties").startObject("field").field("type", "string").field("ignore_above", 5).endObject().endObject()
+                .endObject().endObject().string();
+
+        DocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
+
+        ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
+                .startObject()
+                .field("field", "1234")
+                .endObject()
+                .bytes());
+
+        assertThat(doc.rootDoc().getFieldable("field"), notNullValue());
+
+        doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
+                .startObject()
+                .field("field", "12345")
+                .endObject()
+                .bytes());
+
+        assertThat(doc.rootDoc().getFieldable("field"), notNullValue());
+
+        doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
+                .startObject()
+                .field("field", "123456")
+                .endObject()
+                .bytes());
+
+        assertThat(doc.rootDoc().getFieldable("field"), nullValue());
+    }
+}