Monday, December 21, 2020

ElasticSearch Mapping Visitor Pattern Not Applied on Dynamic Object

The joy of learning new technology is learning new constraints. There is never enough documentation.

Problem

I need to insert dynamic objects into ElasticSearch. For the sake of consistency, I need some properties mapped to specify types and the rest will be mapped text. One amazing thing is the ElasticSearch automagical conversion called AutoMap. Seems like AutoMap with visitor pattern and properties override will meet my requirements, so I have something like this in my code:

public class MapToTextPropertyVisitor : NoopPropertyVisitor
{
    public override IProperty Visit(PropertyInfo propertyInfo,
            ElasticsearchPropertyAttributeBase attribute) => new TextProperty();
}

var createIndexResponse = _client.Indices.Create("<index_name>", c => c
    .Map<dynamic>(m => m
        .AutoMap(new MapToTextPropertyVisitor())
        .Properties(p => p.Date(d => d.Name("<overrides_date_field>"));


AutoMap with manual overrides using fluent mapping:
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/fluent-mapping.html#_auto_mapping_overrides_down_the_object_graph

Visitor pattern: 
https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/visitor-pattern-mapping.html

But for somewhat reason, some of my dynamic object's properties that are not overridden are still mapped to date type when it is supposed to be mapped to text.

Solution

I found out later that the visitor pattern doesn't apply to dynamic mapping in which my dynamic objects are subjected to. It only applies to POCO with clear types. 

Another thing is in my case, the properties in my dynamic object are all of type string. String in ElasticSearch for dynamic mapping has two different kind of detections applied to it:

  • Date detection
  • Number detection

So, my problem were solved by disabling the two detections. That makes the string properties stay as string.

Tuesday, December 8, 2020

Amazon ECR Accessing Private Repository through AWS CLI

 Remote repository is always an easy way to share code. However, I can't seem to find an easy way to access my private repository in ECR.

Problem

In order to push or pull from ECR, we have to first login via AWS CLI and pass the credentials to docker. The script is pretty straightforward and will work in most cases except mine:

aws ecr get-login-password --region <region> | sudo docker login --username AWS --password-stdin <registry_url>

Solution

The problem is I save my credentials for the registry in a different AWS CLI profile, thus I need to change my script to the following so I can push to and pull from the private repository:

aws ecr get-login-password --region <region> --profile <custom_profile> | sudo docker login --username AWS --password-stdin <registry_url>

FTP Access Denied When Attempting to Transfer Files

Permission is, as usual, a double-edged sword. It happened when I tried to transfer files to a remote Linux server.

Problem

I created a directory on the remote server and attempting to transfer files via FTP client but got an access denied error. It is weird because I do login using the same account and thus as the owner of the directory.

Solution

The one that solves the problem in my case is by changing ownership of the folder recursively (-R option) and use name of the user as group name such as:

sudo chown -R <user>:<user> <directory>