mirror of
https://github.com/spring-projects/spring-data-elasticsearch.git
synced 2025-06-08 13:12:10 +00:00
parent
d95af9fcfa
commit
675b77982b
@ -1419,7 +1419,7 @@ public class MappingElasticsearchConverter
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!fieldName.contains(".")) {
|
if (property.hasExplicitFieldName() || !fieldName.contains(".")) {
|
||||||
return target.get(fieldName);
|
return target.get(fieldName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,6 +39,12 @@ public interface ElasticsearchPersistentProperty extends PersistentProperty<Elas
|
|||||||
*/
|
*/
|
||||||
String getFieldName();
|
String getFieldName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {@literal true} if the field name comes from an explicit value in the field annotation
|
||||||
|
* @since 5.1
|
||||||
|
*/
|
||||||
|
boolean hasExplicitFieldName();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the current property is a {@link SeqNoPrimaryTerm} property.
|
* Returns whether the current property is a {@link SeqNoPrimaryTerm} property.
|
||||||
*
|
*
|
||||||
|
@ -142,7 +142,8 @@ public class SimpleElasticsearchPersistentProperty extends
|
|||||||
return storeEmptyValue;
|
return storeEmptyValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected boolean hasExplicitFieldName() {
|
@Override
|
||||||
|
public boolean hasExplicitFieldName() {
|
||||||
return StringUtils.hasText(getAnnotatedFieldName());
|
return StringUtils.hasText(getAnnotatedFieldName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1947,6 +1947,48 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
assertEquals(expected, json, true);
|
assertEquals(expected, json, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2502
|
||||||
|
@DisplayName("should write entity with dotted field name")
|
||||||
|
void shouldWriteEntityWithDottedFieldName() throws JSONException {
|
||||||
|
|
||||||
|
@Language("JSON")
|
||||||
|
var expected = """
|
||||||
|
{
|
||||||
|
"_class": "org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverterUnitTests$FieldNameDotsEntity",
|
||||||
|
"id": "42",
|
||||||
|
"dotted.field": "dotted field"
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
var entity = new FieldNameDotsEntity();
|
||||||
|
entity.setId("42");
|
||||||
|
entity.setDottedField("dotted field");
|
||||||
|
|
||||||
|
Document document = Document.create();
|
||||||
|
mappingElasticsearchConverter.write(entity, document);
|
||||||
|
String json = document.toJson();
|
||||||
|
|
||||||
|
assertEquals(expected, json, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test // #2502
|
||||||
|
@DisplayName("should read entity with dotted field name")
|
||||||
|
void shouldReadEntityWithDottedFieldName() {
|
||||||
|
|
||||||
|
@Language("JSON")
|
||||||
|
String json = """
|
||||||
|
{
|
||||||
|
"id": "42",
|
||||||
|
"dotted.field": "dotted field"
|
||||||
|
}""";
|
||||||
|
|
||||||
|
Document document = Document.parse(json);
|
||||||
|
|
||||||
|
FieldNameDotsEntity entity = mappingElasticsearchConverter.read(FieldNameDotsEntity.class, document);
|
||||||
|
|
||||||
|
assertThat(entity.id).isEqualTo("42");
|
||||||
|
assertThat(entity.getDottedField()).isEqualTo("dotted field");
|
||||||
|
}
|
||||||
|
|
||||||
// region entities
|
// region entities
|
||||||
public static class Sample {
|
public static class Sample {
|
||||||
@Nullable public @ReadOnlyProperty String readOnly;
|
@Nullable public @ReadOnlyProperty String readOnly;
|
||||||
@ -3150,6 +3192,31 @@ public class MappingElasticsearchConverterUnitTests {
|
|||||||
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
|
this.mapToNotWriteWhenEmpty = mapToNotWriteWhenEmpty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
static class FieldNameDotsEntity {
|
||||||
|
@Id
|
||||||
|
@Nullable private String id;
|
||||||
|
@Nullable
|
||||||
|
@Field(name = "dotted.field", type = FieldType.Text) private String dottedField;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(@Nullable String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getDottedField() {
|
||||||
|
return dottedField;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDottedField(@Nullable String dottedField) {
|
||||||
|
this.dottedField = dottedField;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
private static String reverse(Object o) {
|
private static String reverse(Object o) {
|
||||||
|
@ -279,6 +279,14 @@ public abstract class MappingBuilderIntegrationTests extends MappingContextBaseT
|
|||||||
indexOps.createWithMapping();
|
indexOps.createWithMapping();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2502
|
||||||
|
@DisplayName(" should write mapping with field name with dots")
|
||||||
|
void shouldWriteMappingWithFieldNameWithDots() {
|
||||||
|
|
||||||
|
IndexOperations indexOps = operations.indexOps(FieldNameDotsEntity.class);
|
||||||
|
indexOps.createWithMapping();
|
||||||
|
}
|
||||||
|
|
||||||
// region Entities
|
// region Entities
|
||||||
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
||||||
static class Book {
|
static class Book {
|
||||||
@ -901,5 +909,12 @@ public abstract class MappingBuilderIntegrationTests extends MappingContextBaseT
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Field(type = FieldType.Dense_Vector, dims = 1) String denseVectorField;
|
@Field(type = FieldType.Dense_Vector, dims = 1) String denseVectorField;
|
||||||
}
|
}
|
||||||
|
@Document(indexName = "#{@indexNameProvider.indexName()}")
|
||||||
|
private static class FieldNameDotsEntity {
|
||||||
|
@Id
|
||||||
|
@Nullable private String id;
|
||||||
|
@Nullable
|
||||||
|
@Field(name = "dotted.field", type = Text) private String dottedField;
|
||||||
|
}
|
||||||
// endregion
|
// endregion
|
||||||
}
|
}
|
||||||
|
@ -1065,6 +1065,30 @@ public class MappingBuilderUnitTests extends MappingContextBaseTests {
|
|||||||
|
|
||||||
assertEquals(expected, mapping, true);
|
assertEquals(expected, mapping, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // #2502
|
||||||
|
@DisplayName("should use custom name with dots")
|
||||||
|
void shouldUseCustomNameWithDots() throws JSONException {
|
||||||
|
|
||||||
|
var expected = """
|
||||||
|
{
|
||||||
|
"properties": {
|
||||||
|
"_class": {
|
||||||
|
"type": "keyword",
|
||||||
|
"index": false,
|
||||||
|
"doc_values": false
|
||||||
|
},
|
||||||
|
"dotted.field": {
|
||||||
|
"type": "text"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
String mapping = getMappingBuilder().buildPropertyMapping(FieldNameDotsEntity.class);
|
||||||
|
|
||||||
|
assertEquals(expected, mapping, true);
|
||||||
|
}
|
||||||
|
|
||||||
// region entities
|
// region entities
|
||||||
|
|
||||||
@Document(indexName = "ignore-above-index")
|
@Document(indexName = "ignore-above-index")
|
||||||
@ -2220,8 +2244,14 @@ public class MappingBuilderUnitTests extends MappingContextBaseTests {
|
|||||||
@Nullable
|
@Nullable
|
||||||
@Field(type = Text) private String someText;
|
@Field(type = Text) private String someText;
|
||||||
@Nullable
|
@Nullable
|
||||||
@IndexedIndexName
|
@IndexedIndexName private String storedIndexName;
|
||||||
private String storedIndexName;
|
}
|
||||||
|
|
||||||
|
private static class FieldNameDotsEntity {
|
||||||
|
@Id
|
||||||
|
@Nullable private String id;
|
||||||
|
@Nullable
|
||||||
|
@Field(name = "dotted.field", type = Text) private String dottedField;
|
||||||
}
|
}
|
||||||
// endregion
|
// endregion
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user