Kevin
Kevin

Reputation: 1950

Specifying the Analyzer for an auto mapped attachment field

Using Nest, I have a function that sets up all the Elasticsearch document fields and analyzers/SearchAnalyzers.

One of the fields is a Nest Attachment object. This is used to attach a PDF so the content of the PDF can be indexed by Elasticsearch.

Running an "Analyze" Elasticsearch request on the Attachment field shows it is using a 'default' analyzer.

What I want to know is how do I specify a custom Analyzer for this Attachment field (thats auto mapped.)

I would like to map the Attachment.Content field to my analyzer called "literalNameSynonymsAnalyzer" as this is the field that holds the content of the PDF after its been parsed and converted to plain english text.

protected override TypeMappingDescriptor<HondaData> ConfigureDocumentMapping(TypeMappingDescriptor<HondaData> mapping)
        {
            return mapping
                .AutoMap()
                .Properties(prop => prop
                    .Text(text => text
                        .Name(name => name.Id)
                        .Analyzer("keyword"))
                    .Text(text => text
                        .Name(name => name.VehicleIds)
                        .Analyzer("keyword"))
                    .Text(text => text
                        .Name(name => name.Titles)
                        .Analyzer("english")
                        .SearchAnalyzer("literalNameSynonymsAnalyzer"))
                    .Text(text => text
                        .Name(name => name.FileContent)
                        .Analyzer("english")
                        .SearchAnalyzer("literalNameSynonymsAnalyzer"))
                    .Object<Attachment>(a => a
                        .Name(n => n.Attachment)
                        .AutoMap()));
        }

This is the pipeline function

protected Task CreateAttachmentPipeline()
        {
            return Client.PutPipelineAsync(
                "HondaPdfPipeline",
                p => p
                    .Processors(pr => pr
                        .Attachment<HondaData>(a => a
                            .Field(f => f.Base64FileContent)
                            .TargetField(f => f.Attachment))
                        .Remove<HondaData>(r => r
                            .Field(f => f.Base64FileContent))));
        }

Upvotes: 0

Views: 126

Answers (1)

Russ Cam
Russ Cam

Reputation: 125498

The Content property of the Attachment mapping can be specified to use the custom analyzer

protected override TypeMappingDescriptor<HondaData> ConfigureDocumentMapping(TypeMappingDescriptor<HondaData> mapping)
{
    return mapping
        .AutoMap()
        .Properties(prop => prop
            .Text(text => text
                .Name(name => name.Id)
                .Analyzer("keyword"))
            .Text(text => text
                .Name(name => name.VehicleIds)
                .Analyzer("keyword"))
            .Text(text => text
                .Name(name => name.Titles)
                .Analyzer("english")
                .SearchAnalyzer("literalNameSynonymsAnalyzer"))
            .Text(text => text
                .Name(name => name.FileContent)
                .Analyzer("english")
                .SearchAnalyzer("literalNameSynonymsAnalyzer"))
            .Object<Attachment>(a => a
                .Name(n => n.Attachment)
                .AutoMap()
                .Properties(pp => pp
                    .Text(t => t
                        .Name(n => n.Content)
                        .Analyzer("literalNameSynonymsAnalyzer")
                    )
                )
            )
        );
}

Upvotes: 1

Related Questions