Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create float type family #96625

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog/96625.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 96625
summary: Create float type family
area: Mapping
type: enhancement
issues:
- 67723
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ public String typeName() {
return CONTENT_TYPE;
}

@Override
public String familyTypeName() {
return NumberFieldMapper.NumberType.FLOAT.typeName();
}

@Override
public boolean mayExistInIndex(SearchExecutionContext context) {
return context.fieldExistsInIndex(name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ public void testValueForSearch() {
assertEquals(10 / ft.getScalingFactor(), ft.valueForDisplay(10L));
}

public void testFieldFamilyTypeIsReportedCorrectly() {
ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1);
var expectedFamilyType = NumberFieldMapper.NumberType.FLOAT.typeName();
assertEquals(expectedFamilyType, ft.familyTypeName());
}

public void testFieldData() throws IOException {
double scalingFactor = 0.1 + randomDouble() * 100;
Directory dir = newDirectory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
return parsed;
}

@Override
public String familyTypeName() {
return NumberFieldMapper.NumberType.FLOAT.typeName();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to keep this very simple instead of adding more mechanism/abstraction for Type Families here. I figure we can decide to add that once we have a few more use cases to generalize from. Happy to change that though based on feedback. WDYT?

}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
float v = parseToFloat(value);
Expand Down Expand Up @@ -1219,6 +1224,10 @@ public final String typeName() {
return name;
}

public String familyTypeName() {
return typeName();
}

/** Get the associated numeric type */
public final NumericType numericType() {
return numericType;
Expand Down Expand Up @@ -1521,6 +1530,11 @@ public String typeName() {
return type.name;
}

@Override
public String familyTypeName() {
return this.type.familyTypeName();
}

/**
* This method reinterprets a double precision value based on the maximum precision of the stored number field. Mostly this
* corrects for unrepresentable values which have different approximations when cast from floats than when parsed as doubles.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@

public class FieldCapabilitiesFilterTests extends MapperServiceTestCase {

/**
* Tests that the field family type is returned instead of the actual field type.
* `half_float` is chosen arbitrarily for this purpose.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of arbitrarily choosing half_float you could use a randomFrom and list there all the "float" types.

*/
public void testFieldFamilyTypeIsReturned() throws IOException {
MapperService mapperService = createMapperService("""
{
"_doc": {
"properties": {
"field1": { "type": "half_float"}
}
}
}
""");
SearchExecutionContext sec = createSearchExecutionContext(mapperService);

Map<String, IndexFieldCapabilities> response = FieldCapabilitiesFetcher.retrieveFieldCaps(
sec,
new String[] { "*" },
new String[] {},
Strings.EMPTY_ARRAY,
f -> true
);

var expectedFamilyType = "float";
assertEquals(expectedFamilyType, response.get("field1").getType());
}

public void testExcludeNestedFields() throws IOException {
MapperService mapperService = createMapperService("""
{ "_doc" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;

import static java.util.Collections.emptyMap;
Expand Down Expand Up @@ -744,6 +745,18 @@ public void write(XContentBuilder b) throws IOException {
}
}

public void testFieldFamilyTypeIsReportedCorrectly() {
for (NumberFieldMapper.NumberType type : NumberFieldMapper.NumberType.values()) {
NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", type);
var floatFamilyTypes = Set.of(NumberType.FLOAT.typeName(), NumberType.HALF_FLOAT.typeName());
if (floatFamilyTypes.contains(fieldType.typeName())) {
assertEquals(NumberType.FLOAT.typeName(), type.familyTypeName());
} else {
assertEquals(type.typeName(), type.familyTypeName());
}
}
}

public void testDisplayValue() {
for (NumberFieldMapper.NumberType type : NumberFieldMapper.NumberType.values()) {
NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", type);
Expand Down