Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

Commit

Permalink
Fix mapping of timestamps and dates by their underlying numeric value (
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Alonso authored Sep 30, 2016
1 parent 8b0c127 commit ba38344
Show file tree
Hide file tree
Showing 17 changed files with 138 additions and 42 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## 2.2.8.0 (Upcoming)

* Upgrade to Apache Cassandra 2.2.8

Merged from 3.0.8.1:
* Fix mapping of timestamp and date by the underlying numeric value (#177)

## 2.2.7.1 (July 18, 2016)

* Fix mapper referenced by alias in sortFields
Expand Down
2 changes: 1 addition & 1 deletion builder/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<parent>
<groupId>com.stratio.cassandra</groupId>
<artifactId>cassandra-lucene-index-parent</artifactId>
<version>2.2.7.2-RC1-SNAPSHOT</version>
<version>2.2.8.0-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>
Expand Down
8 changes: 4 additions & 4 deletions doc/documentation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ Maps a 64-bit decimal number.
};
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, text, timestamp, tinyint, varchar, varint
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, text, tinyint, varchar, varint

Float mapper
____________
Expand Down Expand Up @@ -967,7 +967,7 @@ Maps a 32-bit decimal number.
};
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, timestamp, tinyint, varchar, varint
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, tinyint, varchar, varint

Geo point mapper
________________
Expand Down Expand Up @@ -1265,7 +1265,7 @@ Maps a 32-bit integer number.
};
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, text, timestamp, tinyint, varchar, varint
Supported CQL types: ascii, bigint, date, decimal, double, float, int, smallint, text, timestamp, tinyint, varchar, varint

Long mapper
___________
Expand Down Expand Up @@ -1293,7 +1293,7 @@ Maps a 64-bit integer number.
};
Supported CQL types: ascii, bigint, decimal, double, float, int, smallint, text, timestamp, tinyint, varchar, varint
Supported CQL types: ascii, bigint, date, decimal, double, float, int, smallint, text, timestamp, tinyint, varchar, varint

String mapper
_____________
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<parent>
<groupId>com.stratio.cassandra</groupId>
<artifactId>cassandra-lucene-index-parent</artifactId>
<version>2.2.7.2-RC1-SNAPSHOT</version>
<version>2.2.8.0-SNAPSHOT</version>
</parent>

<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@
package com.stratio.cassandra.lucene.schema.mapping;

import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.schema.Schema;
import com.stratio.cassandra.lucene.schema.column.Column;
import org.apache.cassandra.db.marshal.*;
import org.apache.cassandra.serializers.SimpleDateSerializer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.SortedNumericSortField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
* A {@link Mapper} to map an integer field.
Expand All @@ -31,6 +38,7 @@
*/
public class IntegerMapper extends SingleColumnMapper.SingleFieldMapper<Integer> {

private static final Logger logger = LoggerFactory.getLogger(IntegerMapper.class);
/** The default boost. */
public static final Float DEFAULT_BOOST = 1.0f;

Expand Down Expand Up @@ -61,15 +69,22 @@ public IntegerMapper(String field, String column, Boolean validated, Float boost
Int32Type.instance,
LongType.instance,
ShortType.instance,
UTF8Type.instance);
UTF8Type.instance,
SimpleDateType.instance,
TimestampType.instance);
this.boost = boost == null ? DEFAULT_BOOST : boost;
}

/** {@inheritDoc} */
@Override
protected Integer doBase(String name, Object value) {
logger.debug("parsing an object with type: "+value.getClass()+" and value: "+value.toString());
if (value instanceof Number) {
return ((Number) value).intValue();
} else if (value instanceof Date) {
int ret=SimpleDateSerializer.timeInMillisToDay(((Date) value).getTime());
logger.debug(" returning: " + Integer.toString(ret));
return ret;
} else if (value instanceof String) {
try {
return Double.valueOf((String) value).intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@
package com.stratio.cassandra.lucene.schema.mapping;

import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.schema.column.Column;
import org.apache.cassandra.db.marshal.*;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.SortedNumericSortField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
* A {@link Mapper} to map a long field.
Expand All @@ -31,6 +36,7 @@
*/
public class LongMapper extends SingleColumnMapper.SingleFieldMapper<Long> {

private static final Logger logger = LoggerFactory.getLogger(LongMapper.class);
/** The default boost. */
public static final Float DEFAULT_BOOST = 1.0f;

Expand Down Expand Up @@ -61,15 +67,22 @@ public LongMapper(String field, String column, Boolean validated, Float boost) {
Int32Type.instance,
LongType.instance,
ShortType.instance,
UTF8Type.instance);
UTF8Type.instance,
SimpleDateType.instance,
TimestampType.instance);
this.boost = boost == null ? DEFAULT_BOOST : boost;
}

/** {@inheritDoc} */
@Override
protected Long doBase(String name, Object value) {
logger.debug("parsing an object with type: "+value.getClass()+" and value: "+value.toString());
if (value instanceof Number) {
return ((Number) value).longValue();
} else if (value instanceof Date) {
long ret=((Date) value).getTime();
logger.debug("returning: " + Long.toString(ret));
return ret;
} else if (value instanceof String) {
try {
return Double.valueOf((String) value).longValue();
Expand All @@ -79,9 +92,17 @@ protected Long doBase(String name, Object value) {
}
throw new IndexException("Field '%s' requires a long, but found '%s'", name, value);
}

/** {@inheritDoc} */
@Override
protected <K> Long doBase(Column<K> column) {
if (column.getType() instanceof SimpleDateType) {
return SimpleDateType.instance.toTimeInMillis(column.getDecomposedValue());
} else {
return doBase(column.getFieldName(field), column.getComposedValue());
}
}
/** {@inheritDoc} */
@Override
public Field indexedField(String name, Long value) {
LongField field = new LongField(name, value, STORE);
field.setBoost(boost);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,6 @@ public void testMinValueShort() {
Short shorty = new Short("-32768");
String parsed = mapper.base("test", shorty);
assertEquals("Base value is not properly parsed", "099967231.9999", parsed);

}

@Test
Expand All @@ -280,7 +279,6 @@ public void testMaxValueShort() {
Short shorty = new Short("32767");
String parsed = mapper.base("test", shorty);
assertEquals("Base value is not properly parsed", "100032766.9999", parsed);

}

@Test
Expand All @@ -289,7 +287,6 @@ public void testZeroValueInt() {
Integer integer = 0;
String parsed = mapper.base("test", integer);
assertEquals("Base value is not properly parsed", "09999.9999", parsed);

}

@Test
Expand All @@ -298,7 +295,6 @@ public void testOneValueInt() {
Integer integer = 1;
String parsed = mapper.base("test", integer);
assertEquals("Base value is not properly parsed", "10000.9999", parsed);

}

@Test
Expand All @@ -307,7 +303,6 @@ public void testNegativeValueInt() {
Integer integer = -15;
String parsed = mapper.base("test", integer);
assertEquals("Base value is not properly parsed", "09984.9999", parsed);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public void testValueFloatWithDecimalFloor() {
DoubleMapper mapper = doubleMapper().boost(1f).build("field");
Double parsed = mapper.base("test", 3.5f);
assertEquals("Base for floats is wrong", Double.valueOf(3.5d), parsed);

}

@Test
Expand All @@ -151,15 +150,13 @@ public void testValueDoubleWithDecimalFloor() {
DoubleMapper mapper = doubleMapper().boost(1f).build("field");
Double parsed = mapper.base("test", 3.5d);
assertEquals("Base for doubles is wrong", Double.valueOf(3.5d), parsed);

}

@Test
public void testValueDoubleWithDecimalCeil() {
DoubleMapper mapper = doubleMapper().boost(1f).build("field");
Double parsed = mapper.base("test", 3.6d);
assertEquals("Base for doubles is wrong", Double.valueOf(3.6d), parsed);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,13 @@ public void testValueDoubleWithDecimalFloor() {
FloatMapper mapper = floatMapper().boost(1f).build("field");
Float parsed = mapper.base("test", 3.5d);
assertEquals("Base for doubles is wrong", Float.valueOf(3.5f), parsed);

}

@Test
public void testValueDoubleWithDecimalCeil() {
FloatMapper mapper = floatMapper().boost(1f).build("field");
Float parsed = mapper.base("test", 3.6d);
assertEquals("Base for doubles is wrong", Float.valueOf(3.6f), parsed);

}

@Test
Expand All @@ -182,7 +180,6 @@ public void testValueStringWithDecimalCeil() {
FloatMapper mapper = floatMapper().boost(1f).build("field");
Float parsed = mapper.base("test", "3.6");
assertEquals("Base for strings is wrong", Float.valueOf(3.6f), parsed);

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

import com.stratio.cassandra.lucene.IndexException;
import com.stratio.cassandra.lucene.schema.mapping.builder.IntegerMapperBuilder;
import org.apache.cassandra.serializers.SimpleDateSerializer;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.search.SortField;
import org.junit.Test;

import java.util.Date;

import static com.stratio.cassandra.lucene.schema.SchemaBuilders.integerMapper;
import static org.junit.Assert.*;

Expand Down Expand Up @@ -127,15 +130,13 @@ public void testValueFloatWithDecimalFloor() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", 3.5f);
assertEquals("Base for floats is wrong", Integer.valueOf(3), parsed);

}

@Test
public void testValueFloatWithDecimalCeil() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", 3.6f);
assertEquals("Base for floats is wrong", Integer.valueOf(3), parsed);

}

@Test
Expand All @@ -150,15 +151,13 @@ public void testValueDoubleWithDecimalFloor() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", 3.5d);
assertEquals("Base for doubles is wrong", Integer.valueOf(3), parsed);

}

@Test
public void testValueDoubleWithDecimalCeil() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", 3.6d);
assertEquals("Base for doubles is wrong", Integer.valueOf(3), parsed);

}

@Test
Expand All @@ -173,15 +172,21 @@ public void testValueStringWithDecimalFloor() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", "3.2");
assertEquals("Base for strings is wrong", Integer.valueOf(3), parsed);

}

@Test
public void testValueStringWithDecimalCeil() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", "3.2");
assertEquals("Base for strings is wrong", Integer.valueOf(3), parsed);
}

@Test
public void testValueWithDate() {
IntegerMapper mapper = integerMapper().boost(1f).build("field");
Integer parsed = mapper.base("test", new Date(10));
Integer expected = SimpleDateSerializer.timeInMillisToDay(10);
assertEquals("Base for dates is wrong", expected, parsed);
}

@Test
Expand Down
Loading

0 comments on commit ba38344

Please sign in to comment.