Date Field Mapper: Allow to use timestamp value (milliseconds since epoch, UTC), closes #801.
This commit is contained in:
parent
9f44c93a25
commit
0d150e6918
|
@ -177,20 +177,33 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
|
|||
}
|
||||
|
||||
@Override protected Fieldable parseCreateField(ParseContext context) throws IOException {
|
||||
String dateAsString;
|
||||
String dateAsString = null;
|
||||
long value = -1;
|
||||
if (context.externalValueSet()) {
|
||||
dateAsString = (String) context.externalValue();
|
||||
if (dateAsString == null) {
|
||||
dateAsString = nullValue;
|
||||
Object externalValue = context.externalValue();
|
||||
if (externalValue instanceof Number) {
|
||||
value = ((Number) externalValue).longValue();
|
||||
} else {
|
||||
dateAsString = (String) externalValue;
|
||||
if (dateAsString == null) {
|
||||
dateAsString = nullValue;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (context.parser().currentToken() == XContentParser.Token.VALUE_NULL) {
|
||||
XContentParser.Token token = context.parser().currentToken();
|
||||
if (token == XContentParser.Token.VALUE_NULL) {
|
||||
dateAsString = nullValue;
|
||||
} else if (token == XContentParser.Token.VALUE_NUMBER) {
|
||||
value = context.parser().longValue();
|
||||
} else {
|
||||
dateAsString = context.parser().text();
|
||||
}
|
||||
}
|
||||
|
||||
if (value != -1) {
|
||||
return new LongFieldMapper.CustomLongNumericField(this, value);
|
||||
}
|
||||
|
||||
if (dateAsString == null) {
|
||||
return null;
|
||||
}
|
||||
|
@ -198,7 +211,7 @@ public class DateFieldMapper extends NumberFieldMapper<Long> {
|
|||
context.allEntries().addText(names.fullName(), dateAsString, boost);
|
||||
}
|
||||
|
||||
final long value = parseStringValue(dateAsString);
|
||||
value = parseStringValue(dateAsString);
|
||||
return new LongFieldMapper.CustomLongNumericField(this, value);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* 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.index.mapper.xcontent.date;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.mapper.ParsedDocument;
|
||||
import org.elasticsearch.index.mapper.xcontent.MapperTests;
|
||||
import org.elasticsearch.index.mapper.xcontent.XContentDocumentMapper;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.*;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@Test
|
||||
public class SimpleDateMappingTests {
|
||||
|
||||
@Test public void testTimestampAsDate() throws Exception {
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("properties").startObject("date_field").field("type", "date").endObject().endObject()
|
||||
.endObject().endObject().string();
|
||||
|
||||
XContentDocumentMapper defaultMapper = MapperTests.newParser().parse(mapping);
|
||||
|
||||
long value = System.currentTimeMillis();
|
||||
ParsedDocument doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("date_field", value)
|
||||
.endObject()
|
||||
.copiedBytes());
|
||||
|
||||
assertThat(doc.doc().getFieldable("date_field").tokenStreamValue(), notNullValue());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue