Dynamically add named mapping to Index

2019-09-16 07:43发布

I would like to add mappings to an index after I've created it. I've created the index as such:

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

The variable typeName, is the name I want for the mapping.

When I execute this:

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

I get this output on my ES service: Wrong Mapping

I would like to get this: Correct Mapping

Any ideas?

1条回答
萌系小妹纸
2楼-- · 2019-09-16 08:21

If you have an existing index and wish to add a mapping to it, this can be done with the Put Mapping API, exposed in NEST as client.Map<T>() and 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)
        )
    )
);

which sends the following request

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"
    }
  }
}
查看更多
登录 后发表回答