# Update blog attachment code example

**URL:** <https://discuss.elastic.co/t/update-blog-attachment-code-example/193862>\
**Category:** Elasticsearch\
**Created:** [August 5, 2019, 6:06pm UTC](https://discuss.elastic.co/t/update-blog-attachment-code-example/193862 "2019-08-05T18:06:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![futureshock2038](https://avatars.discourse-cdn.com/v4/letter/f/f1d935/32.png) [@futureshock2038](https://discuss.elastic.co/u/futureshock2038)\
**Post date:** [August 5, 2019, 6:06pm UTC](https://discuss.elastic.co/t/update-blog-attachment-code-example/193862/1 "2019-08-05T18:06:50Z")

</div>

Russ,  
I am hoping you can help me get the code working from the following blog post:

> **[The Future of Attachments for Elasticsearch and .NET](https://www.elastic.co/blog/the-future-of-attachments-for-elasticsearch-and-dotnet)**
>
> Working with attachments using NEST, the official .NET Elasticsearch high level client.

It is written using a previous version of NEST, but the Attachment method seems to have been deprecated and will not work with NEST 7.1 version.

Thanks,

Karl

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [August 5, 2019, 11:52pm UTC](https://discuss.elastic.co/t/update-blog-attachment-code-example/193862/2 "2019-08-05T23:52:04Z")

</div>

Hey @futureshock2038, the [latter part of the blog post is still relevant](https://www.elastic.co/blog/the-future-of-attachments-for-elasticsearch-and-dotnet#the-very-near-future), although the API methods have been reorganized into "_namespaced_" properties, to make them more discoverable and to match the naming convention used in the REST API specs. For example,

- `client.CreateIndex(...)` becomes `client.Indices.Create()`
- `client.PutPipeline()` becomes `client.Ingest.PutPipeline()`

Here's a working example with NEST 7.1.0 against Elasticsearch 7.3.0, using [the example\_one.docx file from this gist](https://gist.github.com/russcam/2879cacccb77a292c658b48f642067fc#file-example_one-docx)

```auto
private static void Main()
{
    var defaultIndex = "attachments";
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex)
        .DisableDirectStreaming()
        .PrettyJson()
        .OnRequestCompleted(callDetails =>
        {
            if (callDetails.RequestBodyInBytes != null)
            {
                Console.WriteLine(
                    $"{callDetails.HttpMethod} {callDetails.Uri} \n" +
                    $"{Encoding.UTF8.GetString(callDetails.RequestBodyInBytes)}");
            }
            else
            {
                Console.WriteLine($"{callDetails.HttpMethod} {callDetails.Uri}");
            }

            Console.WriteLine();

            if (callDetails.ResponseBodyInBytes != null)
            {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                         $"{Encoding.UTF8.GetString(callDetails.ResponseBodyInBytes)}\n" +
                         $"{new string('-', 30)}\n");
            }
            else
            {
                Console.WriteLine($"Status: {callDetails.HttpStatusCode}\n" +
                         $"{new string('-', 30)}\n");
            }
        });

    var client = new ElasticClient(settings);

	if (client.Indices.Exists(defaultIndex).Exists)
	{
		var deleteIndexResponse = client.Indices.Delete(defaultIndex);
	}

    var createIndexResponse = client.Indices.Create(defaultIndex, c => c
        .Settings(s => s
            .Analysis(a => a
                .Analyzers(ad => ad
                    .Custom("windows_path_hierarchy_analyzer", ca => ca
                        .Tokenizer("windows_path_hierarchy_tokenizer")
                    )
                )
                .Tokenizers(t => t
                    .PathHierarchy("windows_path_hierarchy_tokenizer", ph => ph
                        .Delimiter('\\')
                    )
                )
            )
        )
        .Map<Document>(mp => mp
            .AutoMap()
            .Properties(ps => ps
                .Text(s => s
                    .Name(n => n.Path)
                    .Analyzer("windows_path_hierarchy_analyzer")
                )
                .Object<Attachment>(a => a
                    .Name(n => n.Attachment)
                    .AutoMap()
                )
            )
        )
    );

	var putPipelineResponse = client.Ingest.PutPipeline("attachments", p => p
		.Description("Document attachment pipeline")
		.Processors(pr => pr
			.Attachment<Document>(a => a
				.Field(f => f.Content)
				.TargetField(f => f.Attachment)
			)
			.Remove<Document>(r => r
				.Field(ff => ff
					.Field(f => f.Content)
				)
			)
		)
	);

	var base64File = Convert.ToBase64String(File.ReadAllBytes(@"C:\example_one.docx"));
	
	var indexResponse = client.Index(new Document
	{
		Id = 1,
		Path = @"\\share\documents\examples\example_one.docx",
		Content = base64File
	}, i => i
		.Pipeline("attachments")
		.Refresh(Refresh.WaitFor)
	);

	var searchResponse = client.Search<Document>(s => s
	  .Query(q => q
		.Match(m => m
		  .Field(a => a.Attachment.Content)
		  .Query("NEST")
		)
	  )
	);
	
	foreach(var hit in searchResponse.Hits)
	{
		var attachment = hit.Source.Attachment;
		Console.WriteLine($"Attachment details for doc id {hit.Id}");
		Console.WriteLine($"date: {attachment.Date}");
		Console.WriteLine($"content type: {attachment.ContentType}");
		Console.WriteLine($"author: {attachment.Author}");
		Console.WriteLine($"language: {attachment.Language}");
		Console.WriteLine($"content: {attachment.Content}");
		Console.WriteLine($"content length: {attachment.ContentLength}");
	}
}

public class Document
{
    public int Id { get; set; }
    public string Path { get; set; }
    public string Content { get; set; }
    public Attachment Attachment { get; set; }
}

```

This prints out

```auto
Attachment details for doc id 1
date: 30/08/2016 5:48:00 AM
content type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
author: Russ Cam
language: en
content: The Present and Future of Attachments

This is a sample document to demonstrate indexing attachments using NEST and the new Attachment type
content length: 141

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 2, 2019, 11:52pm UTC](https://discuss.elastic.co/t/update-blog-attachment-code-example/193862/3 "2019-09-02T23:52:04Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
