Repository navigation
Feature Proposal: declare field aliases in the schema definition API #1365
Replies: 4 comments
Why a raw mapping passthrough won't do itThe obvious minimal fix is to allow schema.object_type "Address" do |t|
t.field "fullName", "String"
t.field "name", "String" do |f|
f.mapping type: "alias", path: "???.fullName"
end
endThere's nothing to put in that shippingAddress:
properties: {name: {type: alias, path: shippingAddress.fullName}}
billingAddress:
properties: {name: {type: alias, path: billingAddress.fullName}}One place to write |
What this "kind of" unlocks for multi-fields (#904)This provides an alternative approach to support data ingested as a multi-field subfield. #904 asks to index one value into two datastore fields, so it can serve both exact matching and partial search. There's a naming tension in that thread: the request proposes a flat sibling ( The direction that comes up most in practice is a An alias fixes that by elevating the subfield to the parent's level: schema.object_type "Customer" do |t|
t.field "customerName", "String" do |f|
f.mapping type: "text", fields: {keyword: {type: "keyword", ignore_above: 256}}
end
# Proposed: lifts the subfield to an addressable name in the mapping.
t.mapping_alias "customerNameKeyword", to: "customerName.keyword"
# Existing and unchanged: makes that name groupable in GraphQL.
t.field "customerNameKeyword", "String", graphql_only: true
endThe mapping keeps the subfield shape, and That doesn't make #904 unnecessary — a first-class API would be friendlier than declaring the multi-field and the alias separately — but it does make the two additive rather than competing. It also answers #904's open question about reindexing. The mapping update is additive and free, but the datastore only populates a new subfield for documents indexed after it lands, so existing documents need |
|
In #1364, I proposed a generic schema.object_type "Address" do |t|
t.field "fullName", "String"
# ...
end
schema.object_name "Order" do |t|
t.field "shippingAddress", "Address"
t.field "billingAddress", "Address"
t.index "orders" do |i|
i.customize_config do |config|
config["mappings"]["properties"]["shippingAddress"]["name"] = {"type" => "alias", "path" => "shippingAddress.fullName"}
config["mappings"]["properties"]["billingAddress"]["name"] = {"type" => "alias", "path" => "billingAddress.fullName"}
end
end
endAdmittedly, the ergonomics aren't as nice (if |
|
closing this. Will use the proposed generic interface. |
Uh oh!
There was an error while loading. Please reload this page.
Summary
Elasticsearch and OpenSearch support field aliases: an alternate name for an existing field, resolved at query time. ElasticGraph has no way to declare one. Proposing a schema definition API that emits the alias into the generated mapping:
This is a datastore mapping concern only. It adds nothing to the GraphQL schema, no runtime metadata, no JSON schema entry, and no indexer behavior. The datastore doesn't support writes to an alias, so publishers are unaffected. It also adds no data: no storage cost, no reindex, no
_update_by_query— the alias applies to every document already indexed as soon as the mapping update lands.Motivation
The mapping is the contract for everything that reads an index without going through GraphQL — other services, dashboards, ad-hoc queries, migration tooling. An alias lets those consumers use a stable or domain-appropriate name, and lets a renamed field keep resolving under its old name during a migration. Unlike a concrete field, whose mapping type is immutable, an alias can be repointed with a mapping update.
Today the only way to get one is to apply it to the live mapping by hand. ElasticGraph preserves it once applied (
MappingUpdate.build_mapping_updatekeeps current fields that are absent from the desired mapping), so it isn't clobbered — but it is invisible to the artifact dump. That means no single source of truth, nothing to code review, no way to recreate it on a fresh cluster or a new environment, and new rollover indices that silently lack it unless someone remembered to apply it to the template as well as the concrete indices. When it is missing, queries returnnullrather than an error.Index and index template both, with no admin changes
Indexing::Index#to_index_configand#to_index_template_configboth call the same privatemappingsmethod, so an alias emitted through normal mapping generation lands in both with no new emit logic.Propagation to a running deployment also rides machinery that already exists:
put_index_templatefor the template, andForIndex#update_mappingfor concrete indices, including existing rollover indices viaForIndexTemplate#related_index_configurators. Adding an alias is an additive mapping update, soelasticgraph-adminneeds no changes at all — unlike index-level aliases (#1364), which do need new reconciliation logic.One interaction worth noting:
ForIndex#mapping_type_changesflags keys ending in.type, so repointing an alias (apathchange) won't trip the "can't change a field's type" guard, while converting a concrete field into an alias correctly will.Composition with GraphQL, if a user wants it
Since this does nothing to the GraphQL schema, an alias is not queryable through ElasticGraph on its own — that's intentional, and it keeps the feature to one layer.
A user who does want the alias reachable from GraphQL can pair it with an existing
graphql_only: truefield whose index name matches the alias. That works today and needs no new machinery; the only thing missing is the ability to declare the alias. Whether to do that is the user's decision, not something this feature imposes.Semantics
The datastore's rules become validation at
after_user_definition_complete: the target must exist, must be a concrete field rather than an object or another alias, and must share the alias's nested scope — the last holds automatically, since the alias is emitted as a sibling of its target.Relationship to #904
#904 asks to index an additional copy of a value under a different mapping type, which costs storage and requires existing documents to be backfilled before the new field returns results. It is also explicitly about the GraphQL surface. This proposal indexes nothing new and touches nothing above the mapping. The two are complementary.
All reactions