You are using an index alias. I think what you actually would need is a field alias if the new field doesn't exist anywhere yet:
DELETE field_alias
PUT field_alias/_doc/1
{
"foo": "test"
}
GET field_alias/_mapping
GET field_alias/_search
{
"query": {
"match": {
"bar": "test"
}
}
}
PUT field_alias/_mapping/_doc
{
"properties": {
"bar": {
"type": "alias",
"path": "foo"
}
}
}
GET field_alias/_mapping
GET field_alias/_search
{
"query": {
"match": {
"bar": "test"
}
}
}
If the field already exists in the index you will need to run an update query (though this is relatively heavy depending on how many documents need to be reindexed):
DELETE update_index
PUT update_index/_doc/1
{
"foo": "test"
}
PUT update_index/_doc/2
{
"bar": "test"
}
GET update_index/_mapping
GET update_index/_search
{
"query": {
"match": {
"foo": "test"
}
}
}
# This will fail with: mapper [bar] of different type, current_type [text], merged_type [FieldAliasMapper]
PUT update_index/_mapping/_doc
{
"properties": {
"bar": {
"type": "alias",
"path": "foo"
}
}
}
POST update_index/_update_by_query
{
"query": {
"bool": {
"must": {
"exists": {
"field": "bar"
}
},
"must_not": {
"exists": {
"field": "foo"
}
}
}
},
"script": {
"source": """
ctx._source.foo = ctx._source.bar;
"""
}
}
GET update_index/_search
{
"query": {
"match": {
"foo": "test"
}
}
}
Depending on your scenario you could also solve the problem at query time by simply searching both fields:
DELETE query_time
PUT query_time/_doc/1
{
"foo": "test"
}
PUT query_time/_doc/2
{
"bar": "test"
}
GET query_time/_search
{
"query": {
"multi_match": {
"query": "test",
"fields": [ "foo", "bar"]
}
}
}
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.