将命名映射动态添加到Index

时间:2017-01-11 08:05:41

标签: c# .net elasticsearch nest

我想在创建索引后将索引添加到索引中。我已经创建了索引:

client.CreateIndex("typeaheads", c => c
                         .Settings(t => t.Analysis(m => m.TokenFilters(fl => fl.EdgeNGram("edge_ngram_filter", ad => ad.MinGram(2).MaxGram(20)))
                         .Analyzers(anz => anz.Custom("edge_ngram_analyzer", an => an.Filters("lowercase", "edge_ngram_filter").Tokenizer("standard"))))));

变量typeName是我想要的映射名称。

执行此操作时:

var map = new CreateIndexDescriptor("typeaheads")
                               .Mappings(ms => ms
                               .Map(typeName, d => d.Properties(ps => ps.String(s => s.Name("countryCode")))
                               .Properties(ps => ps.String(s => s.Name("display_ID")))
                               .Properties(ps => ps.String(s => s.Name("display_String")))
                               .Properties(ps => ps.String(s => s.Name("id")))
                               .Properties(ps => ps.String(s => s.Name("languageCode")))
                               .Properties(ps => ps.String(s => s.Name("match_String").SearchAnalyzer("standard").Index(FieldIndexOption.Analyzed).Analyzer("edge_ngram_analyzer")))
                               .Properties(ps => ps.String(s => s.Name("type")))
                               .Properties(ps => ps.Number(s => s.Name("boostFactor").Type(NumberType.Long)))));
var response = client.Index(map);

我在ES服务上获得此输出: Wrong Mapping

我想得到这个:Correct Mapping

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

如果您有现有索引并希望添加映射,可以使用Put Mapping API完成此操作,在NEST中显示为client.Map<T>()client.MapAsync<T>()

var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);              
var client = new ElasticClient(connectionSettings);

var typeName = "my-type";

var mappingResponse = client.Map<object>(d => d
    .Index("typeaheads")
    .Type(typeName)
    .Properties(ps => ps
        .String(s => s.Name("countryCode"))
        .String(s => s.Name("display_ID"))
        .String(s => s.Name("display_String"))
        .String(s => s.Name("id"))
        .String(s => s.Name("languageCode"))
        .String(s => s
            .Name("match_String")
            .SearchAnalyzer("standard")
            .Index(FieldIndexOption.Analyzed)
            .Analyzer("edge_ngram_analyzer")
        )
        .String(s => s.Name("type"))
        .Number(s => s
            .Name("boostFactor")
            .Type(NumberType.Long)
        )
    )
);

发送以下请求

PUT http://localhost:9200/typeaheads/my-type/_mapping?pretty=true 
{
  "properties": {
    "countryCode": {
      "type": "string"
    },
    "display_ID": {
      "type": "string"
    },
    "display_String": {
      "type": "string"
    },
    "id": {
      "type": "string"
    },
    "languageCode": {
      "type": "string"
    },
    "match_String": {
      "type": "string",
      "index": "analyzed",
      "analyzer": "edge_ngram_analyzer",
      "search_analyzer": "standard"
    },
    "type": {
      "type": "string"
    },
    "boostFactor": {
      "type": "long"
    }
  }
}