Elasticsearch shared field name for different fields

24 views Asked by At

I'm a bit confused how to best handle the following scenario with Elasticsearch. I've different types of documents, which have fields with semantically the same content but different field names (see examples). Field type-a.a should be handled like type-b.b or type-c.c.

As far as I know an alias for multiple fields is not supported (enter link description here).

Document 1:

{
  "id": 1,
  "type-a": {
    "a": 300
  }
}

Document 2:

{
  "id": 2,
  "type-b": {
    "b": 200
  }
}

Document 3:

{
  "id": 3,
  "type-c": {
    "c": 200
  }
}

Is there a way to map this fields to common field? What would be best practice? Denormalize the fields before ingesting it to the index?

Thanks for any ideas!

1

There are 1 answers

0
Val On

You might achieve what you want using the copy_to mapping parameter. You can declare each semantically equivalent field to copy its value to another field.

Your mapping would look like this:

PUT numeric/
{
  "mappings": {
    "properties": {
      "type-a": {
        "type": "object",
        "properties": {
          "a": {
            "type": "integer",
            "copy_to": "equivalent"
          }
        }
      },
      "type-b": {
        "type": "object",
        "properties": {
          "b": {
            "type": "integer",
            "copy_to": "equivalent"
          }
        }
      },
      "type-c": {
        "type": "object",
        "properties": {
          "c": {
            "type": "integer",
            "copy_to": "equivalent"
          }
        }
      },
      "equivalent": {
        "type": "integer"
      }
    }
  }
}

Then let's say you index the following data, all documents will have their equivalent integer field populated with the value of the type-specific field a, b or c:

POST numeric/_doc
{
  "type-a": {
    "a": 123
  }
}
POST numeric/_doc
{
  "type-b": {
    "b": 456
  }
}
POST numeric/_doc
{
  "type-c": {
    "c": 789
  }
}

You can finally search your index using the equivalent integer field, regardless of the type of the document. The following query will return all three documents above:

GET numeric/_search
{
  "query": {
    "range": {
      "equivalent": {
        "gte": 0
      }
    }
  }
}