0

I want to have a binary parameter stored in my Elasticsearch server from my C# code. None of the types I tried to use in my index class translates to it.

Is there a way to explicitly instruct my program to store a binary, say from a byte array (could be converted to other types of course)?

Alternatively, is there a way to configure the parameter not to be stored (like with the Json property "stored": false)? As the main problem for me is the copying and indexing of that big parameter (not ideal but sufficient)

Update: I tried to downgrade my NEST version to 1.6.1 to use the attribute

[ElasticProperty(Name = "Data", Type = FieldType.Binary, Store = false)]
public byte[] Data { get; set; }

But when I save a document with that property, it still insists to map a string (I check by running GET mydb/_mapping in my Sense plugin)

1 Answer 1

5

Elasticsearch supports binary types which can be set using attributes within NEST using the following in NEST 1.x

public class Document
{
    [ElasticProperty(Type = FieldType.Binary, Store = false)]
    public string Binary { get; set; }
}

or

public class Document
{
    [Binary(Store= false)]
    public string Binary { get; set; }
}

in NEST 2.x

Note that the binary type should be sent to Elasticsearch as a base 64 encoded string (1.x docs or 2.x docs). You could handle the conversion in your POCO type with something like (for 2.x)

public class Document
{
    [JsonIgnore]
    public byte[] BinaryBytes { get; set;}

    [Binary]
    [JsonProperty("binary")]
    public string Binary
    {
        get
        {
            return BinaryBytes != null ? Convert.ToBase64String(BinaryBytes) : null;
        }
        protected set
        {
            if (value != null) BinaryBytes = Convert.FromBase64String(value);
        }
    }
}  

client.CreateIndex("index-name", c => c
    .Mappings(m => m
        .Map<Document>(d => d
            .AutoMap()
        )
    )
);

which yields

{
  "mappings": {
    "document": {
      "properties": {
        "binary": {
          "type": "binary"
        }
      }
    }
  }
}

Then you would set BinaryBytes on the model, and NEST would send the contents of Binary in the request. You could also make Binary a private property if it would be less confusing for users of the model.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch for your help, It seems that just adding the client.CreateIndex part instead of letting NEST doing it by itself gave me what I wanted
@OdedSayar there are two things that Elasticsearch does to help in getting up and running; first, create an index automatically if it does not yet exist. This behaviour can be controlled with - elastic.co/guide/en/elasticsearch/reference/current/…. Secondly, dynamically map (i.e. infer the types of) the fields of a document if an explicit mapping is not specified for the document type. This can also be controlled - elastic.co/guide/en/elasticsearch/guide/current/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.