首页 文章

Elasticsearch 2.3 - Nest - 使用语音分析器

提问于
浏览
0

我正在使用Elasticsearch 2.3 - Nest API来搜索数据 . 我正在使用文档的属性映射 . 我想知道如何使用属性映射使用Phonetic Analyzer .

文件类:

[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; }
}

索引创建和插入:

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);

ESMapper仅用于从一种类型转换为另一种类型 .

结果映射:

{
  "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"
          }
        }
      }
    }
  }
}

我还安装了Phonetic Analysis Plugin

1 回答

  • 0

    Take a look at the automapping documentation;您需要将分析器的名称分配给属性上的 Analyzer 属性

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

    然后在映射文档类型时调用 AutoMap() ,例如在创建索引时

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

    产量

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

相关问题