Mapping - Support specifying string as number values (for example, for boost), closes #53.

This commit is contained in:
kimchy 2010-03-10 11:22:57 +02:00
parent ae5bcb6d2e
commit 7c68489758
2 changed files with 83 additions and 14 deletions

View File

@ -40,6 +40,7 @@ import java.util.Map;
import static com.google.common.collect.Lists.*; import static com.google.common.collect.Lists.*;
import static org.elasticsearch.index.mapper.json.JsonMapperBuilders.*; import static org.elasticsearch.index.mapper.json.JsonMapperBuilders.*;
import static org.elasticsearch.util.json.JacksonNodes.*;
/** /**
* @author kimchy (Shay Banon) * @author kimchy (Shay Banon)
@ -151,7 +152,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getNumberValue().floatValue()); builder.nullValue(nodeFloatValue(propNode));
} }
} }
return builder; return builder;
@ -180,7 +181,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String fieldName = entry.getKey(); String fieldName = entry.getKey();
JsonNode fieldNode = entry.getValue(); JsonNode fieldNode = entry.getValue();
// if (fieldName.equals("compressionThreshold")) { // if (fieldName.equals("compressionThreshold")) {
// builder.compressionThreshold(fieldNode.getNumberValue().intValue()); // builder.compressionThreshold(nodeIn...);
// } else if (fieldName.equals("compressionType")) { // } else if (fieldName.equals("compressionType")) {
// String compressionType = fieldNode.getTextValue(); // String compressionType = fieldNode.getTextValue();
// if ("zip".equals(compressionType)) { // if ("zip".equals(compressionType)) {
@ -204,7 +205,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String fieldName = entry.getKey(); String fieldName = entry.getKey();
JsonNode fieldNode = entry.getValue(); JsonNode fieldNode = entry.getValue();
if (fieldName.equals("dynamic")) { if (fieldName.equals("dynamic")) {
builder.dynamic(fieldNode.getBooleanValue()); builder.dynamic(nodeBooleanValue(fieldNode));
} else if (fieldName.equals("type")) { } else if (fieldName.equals("type")) {
String type = fieldNode.getTextValue(); String type = fieldNode.getTextValue();
if (!type.equals("object")) { if (!type.equals("object")) {
@ -227,7 +228,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
builder.dateTimeFormatter(dateTimeFormatters); builder.dateTimeFormatter(dateTimeFormatters);
} }
} else if (fieldName.equals("enabled")) { } else if (fieldName.equals("enabled")) {
builder.enabled(fieldNode.getBooleanValue()); builder.enabled(nodeBooleanValue(fieldNode));
} else if (fieldName.equals("pathType")) { } else if (fieldName.equals("pathType")) {
builder.pathType(parsePathType(name, fieldNode.getValueAsText())); builder.pathType(parsePathType(name, fieldNode.getValueAsText()));
} else if (fieldName.equals("properties")) { } else if (fieldName.equals("properties")) {
@ -362,7 +363,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getNumberValue().intValue()); builder.nullValue(nodeIntegerValue(propNode));
} }
} }
return builder; return builder;
@ -376,7 +377,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getNumberValue().longValue()); builder.nullValue(nodeLongValue(propNode));
} }
} }
return builder; return builder;
@ -390,7 +391,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getNumberValue().floatValue()); builder.nullValue(nodeFloatValue(propNode));
} }
} }
return builder; return builder;
@ -404,7 +405,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getNumberValue().doubleValue()); builder.nullValue(nodeDoubleValue(propNode));
} }
} }
return builder; return builder;
@ -438,7 +439,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("nullValue")) { if (propName.equals("nullValue")) {
builder.nullValue(propNode.getBooleanValue()); builder.nullValue(nodeBooleanValue(propNode));
} }
} }
return builder; return builder;
@ -451,7 +452,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("precisionStep")) { if (propName.equals("precisionStep")) {
builder.precisionStep(propNode.getNumberValue().intValue()); builder.precisionStep(nodeIntegerValue(propNode));
} }
} }
} }
@ -462,7 +463,7 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
String propName = entry.getKey(); String propName = entry.getKey();
JsonNode propNode = entry.getValue(); JsonNode propNode = entry.getValue();
if (propName.equals("indexName")) { if (propName.equals("indexName")) {
builder.indexName(propNode.getValueAsText()); builder.indexName(propNode.getTextValue());
} else if (propName.equals("store")) { } else if (propName.equals("store")) {
builder.store(parseStore(name, propNode.getTextValue())); builder.store(parseStore(name, propNode.getTextValue()));
} else if (propName.equals("index")) { } else if (propName.equals("index")) {
@ -470,11 +471,11 @@ public class JsonDocumentMapperParser implements DocumentMapperParser {
} else if (propName.equals("termVector")) { } else if (propName.equals("termVector")) {
builder.termVector(parseTermVector(name, propNode.getTextValue())); builder.termVector(parseTermVector(name, propNode.getTextValue()));
} else if (propName.equals("boost")) { } else if (propName.equals("boost")) {
builder.boost(propNode.getNumberValue().floatValue()); builder.boost(nodeFloatValue(propNode));
} else if (propName.equals("omitNorms")) { } else if (propName.equals("omitNorms")) {
builder.omitNorms(propNode.getBooleanValue()); builder.omitNorms(nodeBooleanValue(propNode));
} else if (propName.equals("omitTermFreqAndPositions")) { } else if (propName.equals("omitTermFreqAndPositions")) {
builder.omitTermFreqAndPositions(propNode.getBooleanValue()); builder.omitTermFreqAndPositions(nodeBooleanValue(propNode));
} else if (propName.equals("indexAnalyzer")) { } else if (propName.equals("indexAnalyzer")) {
builder.indexAnalyzer(analysisService.analyzer(propNode.getTextValue())); builder.indexAnalyzer(analysisService.analyzer(propNode.getTextValue()));
} else if (propName.equals("searchAnalyzer")) { } else if (propName.equals("searchAnalyzer")) {

View File

@ -0,0 +1,68 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.util.json;
import org.codehaus.jackson.JsonNode;
/**
* @author kimchy (shay.banon)
*/
public class JacksonNodes {
public static float nodeFloatValue(JsonNode node) {
if (node.isNumber()) {
return node.getNumberValue().floatValue();
}
String value = node.getTextValue();
return Float.parseFloat(value);
}
public static double nodeDoubleValue(JsonNode node) {
if (node.isNumber()) {
return node.getNumberValue().doubleValue();
}
String value = node.getTextValue();
return Double.parseDouble(value);
}
public static int nodeIntegerValue(JsonNode node) {
if (node.isNumber()) {
return node.getNumberValue().intValue();
}
String value = node.getTextValue();
return Integer.parseInt(value);
}
public static long nodeLongValue(JsonNode node) {
if (node.isNumber()) {
return node.getNumberValue().longValue();
}
String value = node.getTextValue();
return Long.parseLong(value);
}
public static boolean nodeBooleanValue(JsonNode node) {
if (node.isBoolean()) {
return node.getBooleanValue();
}
String value = node.getTextValue();
return !(value.equals("false") || value.equals("0") || value.equals("off"));
}
}