0
votes

I am using Elasticsearch 2.3 - Nest API to search data. I am using attribute mapping for documents. I would like to know how to use Phonetic Analyzer using attribute mapping.

The document class:

[ElasticsearchType(Name = "contact", IdProperty = nameof(EntityId))]
public class ESContactSearchDocument
{
    [String(Name = "entityid")]
    public string EntityId { get; set; }

    [String(Name = "id")]
    public string Id { get; set; }

    [String(Name = "name")]
    public string Name { get; set; }

    [String(Name = "domain")]
    public string Domain { get; set; }

    [String(Name = "description")]
    public string Description { get; set; }

    [String(Name = "email")]
    public string Email { get; set; }

    [String(Name = "firstname", Analyzer = "phonetic")]
    public string FirstName { get; set; }

    [String(Name = "lastname", Analyzer = "phonetic")]
    public string LastName { get; set; }
}

Index creation and insertion:

var createIndexResponse = serviceClient.CreateIndex(indexName, c => c.Mappings(m => m.Map<ESContactSearchDocument>(d => d.AutoMap())));

var indexManyResponse = await serviceClient.IndexManyAsync(ESMapper.Map<TDocument, ESContactSearchDocument>(documentsBatch), indexName, typeName);

The ESMapper is only used to convert from one type to another.

Resultant mapping:

{
  "contact-uklwcl072": {
    "mappings": {
      "contact": {
        "properties": {
          "description": {
            "type": "string"
          },
          "domain": {
            "type": "string"
          },
          "email": {
            "type": "string"
          },
          "entityid": {
            "type": "string"
          },
          "firstname": {
            "type": "string"
          },
          "id": {
            "type": "string"
          },
          "lastname": {
            "type": "string"
          },
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}

I have also installed Phonetic Analysis Plugin

1

1 Answers

0
votes

Take a look at the automapping documentation; you need to assign the name of the analyzer to the Analyzer property on the attribute

public class Document
{
    [String(Analyzer="phonetic")]
    public string Name {get; set;}
}

then call AutoMap() when mapping your document type e.g. when you create the index

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

yields

{
  "mappings": {
    "document": {
      "properties": {
        "name": {
          "type": "string",
          "analyzer": "phonetic"
        }
      }
    }
  }
}