【问题标题】:Elasticsearch 2.3 - Nest - using Phonetic AnalyzerElasticsearch 2.3 - Nest - 使用语音分析器
【发布时间】:2016-11-08 16:33:52
【问题描述】:

我正在使用 Elasticsearch 2.3 - Nest API 来搜索数据。我正在为文档使用属性映射。我想知道如何通过属性映射来使用语音分析器。

文档类:

[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

【问题讨论】:

    标签: elasticsearch nest elasticsearch-plugin


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回复,但上面的代码没有产生“语音”分析器,我得到了相同的文档映射。
    • @ShahidAzim 你能在问题中显示你正在做什么的代码吗?
    • @ShahidAzim - 我怀疑您正在创建的索引已经存在。您是否在创建之前先删除索引? createIndexResponse.IsValid 的值是多少?
    • 是的,我先删除索引,响应也是有效的,不完全确定我缺少什么。
    猜你喜欢
    • 2020-10-24
    • 2016-06-16
    • 1970-01-01
    • 2018-11-05
    • 2017-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    相关资源
    最近更新 更多