# How to search in attachment content?

**URL:** <https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287>\
**Category:** Elasticsearch\
**Created:** [May 20, 2022, 11:49am UTC](https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287 "2022-05-20T11:49:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![frankmehlhop](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@frankmehlhop](https://discuss.elastic.co/u/frankmehlhop)\
**Post date:** [May 20, 2022, 11:49am UTC](https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287/1 "2022-05-20T11:49:01Z")

</div>

I ingest pdf files into elastic.  
I can successful search in it using Kibana Console.  
But at my C# code I don't get any result.  
I'm quit sure the problem is syntax of my code.  
But I tried many things and still I don't get the result.  
I always tried using the fields "Content" and "Attachment" to search in.  
**How do I search a attachment using C#?**  
Is there a documentation about the elastic - C# - syntax?

my successful request:

```auto
GET /attachments/_search 
{
  "query": {
    "match": {
      "attachment.content": "Preisangebot"
    }
  }
}

```

My unsuccessful C# request:

```auto
            var searchResponse = client.Search<Document>(s => s
               .Query(q => q
                    .Match(m => m
                    .Field(f => f.Attachment.Content)
                    .Query("Preisangebot")
                    )
               )
            );

```

also unsuccessful:

```auto
            var searchRequest = new SearchRequest<Document>(Nest.Indices.All)
            {
                Query = new MatchQuery
                {
                    Field = Infer.Field<Document>(f => f.Content),
                    Query = "Preisangebot"
                }
            };
            var searchResponse2 = client.Search<Document>(searchRequest);

```

also unsuccessful:

```auto
            var searchResponse3 = client.Search<Document>(s => s
                .Index("attachments")
                .Query(q => q
                .Bool(b => b
                .Must(m => m
                .Match(mt => mt.Field(f => f.Content).Query("Preisangebot"))
            ))));

```

ingesting pdf files:

```auto
                        var base64File = Convert.ToBase64String(File.ReadAllBytes(path));
                        var indexResponse = client.Index(new Document
                        {
                            Id = i,
                            Path = path,
                            Content = base64File
                        }, i => i
                          .Pipeline("attachments")
                          .Refresh(Refresh.WaitFor)
                        );

```

Document class:

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

```

---

<div class="post-metadata">

**Author:** ![frankmehlhop](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@frankmehlhop](https://discuss.elastic.co/u/frankmehlhop)\
**Post date:** [May 23, 2022, 11:13am UTC](https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287/2 "2022-05-23T11:13:17Z")

</div>

I'm still very much interested in any ideas or answers.

---

<div class="post-metadata">

**Author:** ![frankmehlhop](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@frankmehlhop](https://discuss.elastic.co/u/frankmehlhop)\
**Post date:** [June 8, 2022, 7:17am UTC](https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287/3 "2022-06-08T07:17:33Z")

</div>

Für alle die sich für das Thema interessieren, [hier habe ich meine derzeitige Lösung](https://stackoverflow.com/questions/72458664/how-can-i-use-c-sharp-to-search-the-contents-of-pdfs-in-elastic) beschrieben.  
Bin aber offen für weitere Lösungen mit ElasticClient.

---

<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:** [July 6, 2022, 7:17am UTC](https://discuss.elastic.co/t/how-to-search-in-attachment-content/305287/4 "2022-07-06T07:17:43Z")

</div>

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