Fix custom AUTO issue with Fuzziness#toXContent (#35807)
Currently when a Fuzziness instance with custom AUTO distance values gets written to XContent, the customized lower and upper distance values are ommited and can consequently not be parsed back. This changes this to write the String including the optional custom values when writing to XContent and fixes the tests that should have caught this in the first place, e.g. by adding the custom low and high distance values to the equality check.
This commit is contained in:
parent
a2338bb116
commit
51a7dc54ec
|
@ -177,7 +177,7 @@ public final class Fuzziness implements ToXContentFragment, Writeable {
|
|||
|
||||
@Override
|
||||
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.field(X_FIELD_NAME, fuzziness);
|
||||
builder.field(X_FIELD_NAME, asString());
|
||||
return builder;
|
||||
}
|
||||
|
||||
|
@ -231,11 +231,13 @@ public final class Fuzziness implements ToXContentFragment, Writeable {
|
|||
return false;
|
||||
}
|
||||
Fuzziness other = (Fuzziness) obj;
|
||||
return Objects.equals(fuzziness, other.fuzziness);
|
||||
return Objects.equals(fuzziness, other.fuzziness) &&
|
||||
lowDistance == other.lowDistance &&
|
||||
highDistance == other.highDistance;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return fuzziness.hashCode();
|
||||
return Objects.hash(fuzziness, lowDistance, highDistance);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -94,16 +94,19 @@ public class FuzzinessTests extends ESTestCase {
|
|||
{
|
||||
XContentBuilder json;
|
||||
boolean isDefaultAutoFuzzinessTested = randomBoolean();
|
||||
Fuzziness expectedFuzziness = Fuzziness.AUTO;
|
||||
if (isDefaultAutoFuzzinessTested) {
|
||||
json = Fuzziness.AUTO.toXContent(jsonBuilder().startObject(), null).endObject();
|
||||
} else {
|
||||
String auto = randomBoolean() ? "AUTO" : "auto";
|
||||
StringBuilder auto = new StringBuilder();
|
||||
auto = randomBoolean() ? auto.append("AUTO") : auto.append("auto");
|
||||
if (randomBoolean()) {
|
||||
auto += ":" + randomIntBetween(1, 3) + "," + randomIntBetween(4, 10);
|
||||
int lowDistance = randomIntBetween(1, 3);
|
||||
int highDistance = randomIntBetween(4, 10);
|
||||
auto.append(":").append(lowDistance).append(",").append(highDistance);
|
||||
expectedFuzziness = Fuzziness.build(auto.toString());
|
||||
}
|
||||
json = jsonBuilder().startObject()
|
||||
.field(Fuzziness.X_FIELD_NAME, auto)
|
||||
.endObject();
|
||||
json = expectedFuzziness.toXContent(jsonBuilder().startObject(), null).endObject();
|
||||
}
|
||||
try (XContentParser parser = createParser(json)) {
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
|
||||
|
@ -111,7 +114,9 @@ public class FuzzinessTests extends ESTestCase {
|
|||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
Fuzziness fuzziness = Fuzziness.parse(parser);
|
||||
if (isDefaultAutoFuzzinessTested) {
|
||||
assertThat(fuzziness, sameInstance(Fuzziness.AUTO));
|
||||
assertThat(fuzziness, sameInstance(expectedFuzziness));
|
||||
} else {
|
||||
assertEquals(expectedFuzziness, fuzziness);
|
||||
}
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.END_OBJECT));
|
||||
}
|
||||
|
@ -151,21 +156,11 @@ public class FuzzinessTests extends ESTestCase {
|
|||
}
|
||||
|
||||
public void testSerializationCustomAuto() throws IOException {
|
||||
String auto = "AUTO:4,7";
|
||||
XContentBuilder json = jsonBuilder().startObject()
|
||||
.field(Fuzziness.X_FIELD_NAME, auto)
|
||||
.endObject();
|
||||
|
||||
try (XContentParser parser = createParser(json)) {
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
||||
assertThat(parser.nextToken(), equalTo(XContentParser.Token.VALUE_STRING));
|
||||
Fuzziness fuzziness = Fuzziness.parse(parser);
|
||||
|
||||
Fuzziness deserializedFuzziness = doSerializeRoundtrip(fuzziness);
|
||||
assertEquals(fuzziness, deserializedFuzziness);
|
||||
assertEquals(fuzziness.asString(), deserializedFuzziness.asString());
|
||||
}
|
||||
Fuzziness original = Fuzziness.build("AUTO:4,7");
|
||||
Fuzziness deserializedFuzziness = doSerializeRoundtrip(original);
|
||||
assertNotSame(original, deserializedFuzziness);
|
||||
assertEquals(original, deserializedFuzziness);
|
||||
assertEquals(original.asString(), deserializedFuzziness.asString());
|
||||
}
|
||||
|
||||
private static Fuzziness doSerializeRoundtrip(Fuzziness in) throws IOException {
|
||||
|
|
Loading…
Reference in New Issue