Elasticsearch field name aliasing

12,818

Solution 1

Just a quick update, Elasticsearch 6.4 came up with feature called Alias Datatype. Check the below mapping and query as sample.

Note that the type of the field is alias in the below mapping for fieldname fn

Sample Mapping:

PUT myindex
{
  "mappings": {
    "_doc": {
      "properties": {
        "firstname": {
          "type": "text"
        },
        "fn": {
          "type": "alias",
          "path": "firstname" 
        }
      }
    }
  }
}

Sample Query:

GET myindex/_search
{
  "query": {
    "match" : {
      "fn" : "Steve"
    }
  }
}

The idea is to use the alias for actual field on which inverted index is created. Note that fields with alias datatype aren't meant for write operations and its only meant for querying purpose.

Although you can refer to the link I've mentioned for more details, below are just some of the important points.

  • Field alias is only meant to be used when your index has a single mapping. Index has to be created post 6.xx version or be created in older version with the setting index.mapping.single_type: true
  • Can be used in querying, aggregations, sorting, highlighting and suggestion operations
  • Target field must be actual field on which inverted index is created
  • Cannot create alias of another alias field
  • Cannot use alias on multiple fields. Single alias, Single field.
  • Cannot be used as part of source filtering using _source.

Solution 2

There is no direct field alias functionality. However, you could rename the fields upon indexing using the index_name property in your mappings.

index_name : The name of the field that will be stored in the index. Defaults to the property/field name.

See here for more information: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-core-types.html

Solution 3

Adding alias fn for existing field firstname

PUT myindex/_mapping
{
  "properties": {
    "fn": {
      "type": "alias",
      "path": "firstname" 
    }
  }
}

Should work this way as of Elasticsearch 7.

Share:
12,818

Related videos on Youtube

user3658423
Author by

user3658423

Updated on June 04, 2022

Comments

  • user3658423
    user3658423 almost 2 years

    Is it possible to setup alias for field names in elasticsearch? (Just like how index names can be aliased)

    For example: i have a document {'firstname': 'John', 'lastname': 'smith'}

    I would like to alias 'firstname' to 'fn'...

  • user3658423
    user3658423 about 9 years
    My reason to use alias is to reduce the db size....by reducing the size of longer key names to shorter alias name