Skip to content

Allow field names with dots #2504

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

Merged
merged 1 commit into from
Mar 22, 2023

Conversation

sothawo
Copy link
Collaborator

@sothawo sothawo commented Mar 22, 2023

This PR adds the possbility to to define a custom field name in Elasticsearch that contains dots, for example:

@Nullable
@Field(name = "dotted.text", type = FieldType.Text)
private String dottedText;

terminology

dfn means dotted field name

field mapping

It is possible to send a mapping with a dfn, and Spring Data Elasticsearch will do so for such a field:

{
	"mappings": {
		"properties": {
			"dotted.field": {
				"type": "text"
			}
		}
	}
}

When retrieving the mapping, it is returned like nested object properties:

{
	"indexname": {
		"mappings": {
			"properties": {
				"dotted": {
					"properties": {
						"field": {
							"type": "text"
						}
					}
				}
			}
		}
	}
}

The same happens when a document with a dfn is sent to Elasticsearch directly without using Spring Data Elasticsearch.

Possible queries

A property that has a dfn name can be queried with:

@Query annotation:

@Query("""
	{
		"match": {
		"dotted.text": {
			"query": "?0"
			}
		}
	}
	""")
SearchHits<Foo> searchByDottedText(String text);
	}

CriteriaQuery:

return operations.search(CriteriaQuery.builder(
	Criteria.where("dottedText").is(text))
	.build(), Foo.class);

NativeQuery:

return operations.search(NativeQuery.builder()
	.withQuery(QueryBuilders.match(b -> b.field("dotted.text").query(text)))
	.build(), Foo.class);

Name based query:

SearchHits<Foo> searchByDottedText(String text);

The name based query works, becaus the java property has the name dottedField, what would not work would be findByDotted_Text.

Closes #2502

@sothawo sothawo merged commit 675b779 into spring-projects:main Mar 22, 2023
@sothawo sothawo deleted the #2502-field-names-with-dots branch March 23, 2023 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for field mappings containing dots
1 participant