未使用类型的编程 PUT 映射?

Programmatic PUT mapping for a type isn't being used?

我已经按照流利 API 单元测试 (FluentMappingFullExampleTests) 中的示例定义了一个类型的编程映射,如下所示:

_client.Map<SomeType>(m => m
            .Type("mytype")
            ...

然后我通过像

这样的调用将 SomeType 的实例添加到索引
_client.Index<SomeType>(instance)

但是,当我搜索一个实例时,我没有找到 'mytype' 的任何实例;相反,有一个 'sometype' 的实例,并且已经为 'sometype' 创建了一个新的类型映射。我原以为在执行插入时 PUT 映射会得到尊重。

我没有按照应有的方式使用 PUT 映射吗?不幸的是,单元测试没有演示往返,所以我不确定我是否应该做其他事情。

编辑:值得一提的是,我正在努力实现 100% 的编程映射,在这里;该类型没有 NEXT 属性。

我能够在我的示例中处理您的用例:

//request url: http://localhost:9200/indexName
var indicesOperationResponse = client.CreateIndex(indexName);

//request url: http://localhost:9200/indexName/document2/_mapping
var response = client.Map<Document>(m => m
    .Type("document2")
    .Properties(p => p.String(s => s.Name(n => n.Name).Index(FieldIndexOption.NotAnalyzed))));

//request url: http://localhost:9200/indexName/document2/1
client.Index(new Document { Id = 1, Name = "test"});

client.Refresh();

//request url: http://localhost:9200/indexName/document2/_search
var searchResponse = client.Search<Document>(s => s.Query(q => q.MatchAll()));

关键是用 ElasticType 属性标记 Document class:

[ElasticType(Name = "document2")]
public class Document
{
    public int Id { get; set; }
    public string Name { get; set; }
}

希望对您有所帮助。

更新

你的评论很有道理。您可以更改 ElasticClient.

的类型名称推断,而不是使用 ElasticType
var uri = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(uri)
    .SetDefaultIndex(indexName)
    .MapDefaultTypeNames(d => d.Add(typeof(Document), "document2"))

var client = new ElasticClient(settings);

所以我们可以从 Document class

中删除属性
public class Document
{
    public int Id { get; set; }
    public string Name { get; set; }
}

我们也不需要在映射中指定类型别名:

var response = client.Map<Document>(m => m
    .Properties(p => p.String(s => s.Name(n => n.Name).Index(FieldIndexOption.NotAnalyzed))));