This content is archived. See latest version here.

Last updated: Jun 10 2014

Introduction

Most of our plans allow you to index attachments, meaning files such as Word and PDF documents. For a full list of supported formats see the Apache Tika documentation. To index attachments using the .NET API you create an instance of a class that has a property of type Attachment (found in the EPiServer.Find namespace). The Attachment class constructor has a single parameter of type Func<FileStream>. There is also a class named FileAttachment (also in the EPiServer.Find namespace) that instead requires a file path as a constructor parameter.

Examples

As an example, assume we create a class named Document:

C#
public class Document
{
    public string Name { get; set; }
    public Attachment Attachment { get; set; }
}

We can then index an instance of the Document class to index a Word document along with some meta data (Name in this example).

C#
var path = "TestData/Memoirs.docx";

var document = new Document()
  {
    Name = "My memoirs",
    Attachment = new FileAttachment(path);
  }

client.Index(document);

We are now able to search for the contents of the indexed Word document. For instance, assuming it contained the word "Banana" the result variable below would cointain a hit.

C#
var result = client.Search<Document>()
                   .For("Banana").GetResult();

Important note

At the moment there is an issue in the REST API that will cause an exception the first time a an instance of a type with an Attachment property (Document in this example) is indexed. This only happens the first time and after that everything will work as expected.

Comments