babenkoivan / elastic-migrations

Elasticsearch migrations for Laravel
MIT License
188 stars 32 forks source link

Definition of dynamic templates. #8

Closed VKambulov closed 3 years ago

VKambulov commented 4 years ago

Hello! I think it would be a good idea to make it possible to insert dynamic templates into migrations. For example, we have an object with a large nesting of related relations, the names of the fields and the purpose are approximately similar:

{
  "id": 1,
  "name": "123456",
  "manufacturer": "carry-a-tune technologies",
  "SKU": 12345,
  "brand": {
    "id": 1,
    "name": "test",
    "main_manufacturer": "carry-a-tune technologies",
    "SKU": 12333,
    "organization": {
      "id": 1,
      "name": "Mayo Corporation",
      "SKU": 5544
    }
  }
}

In order not to assign a keyword type to each SKU field, we could do something like this:

{
  "mappings": {
    "dynamic_templates": [
      {
        "manufacturer": {
          "match": "*manufacturer",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "SKU": {
          "match": "SKU",
          "mapping": {
            "type": "keyword"
          }
        }
      },
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "text",
            "fields": {
              "raw": {
                "type":  "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    ],
    "properties": {
      "id": {
        "type": "text"
      },
      "data": {
        "type": "nested"
      }
    }
  }
}

I have not found how to define dynamic templates in the current implementation, and judging by the code, this is not possible now, since the Mapping class works only with properties. I would suggest adding the dynamicTemplates function and passing an array into it, or making dynamic templates define similar to the __call function, for example:

$properties = [
 'match' => 'id',
 'mapping' => [
  'type' => 'keyword'
 ]
];

$mapping->template('name', $properties);

or:

$maping->dynamicTemplates([
 'name' => [
   'match' => 'id',
   'mapping' => [
    'type' => 'keyword'
   ]
]);

In some situations, I think dynamic templates could help a lot. Please correct me if I am wrong about something, or if this possibility already exists.

Updated: Sorry, I saw late that this is more related to Elastic Adapter.

babenkoivan commented 4 years ago

Hi @VKambulov, it's a good feature to add in the package. I've added it in my list.

babenkoivan commented 3 years ago

Hi @VKambulov, sorry for taking so long. The feature is now available in v1.3.0.

$mapping->dynamicTemplate('my_template_name', [
        'match_mapping_type' => 'long',
        'mapping' => [
            'type' => 'integer',
        ],
]);