我使用nest 7.0.0和asp.net core 2.2。我想从poco类创建indxe。在elasticsearch索引中创建但没有任何映射。创建索引的方法是:
public async Task CreateIndex()
{
try
{
var getIndexResponse =await ElasticClient.Indices.GetAsync("myindex");
if (getIndexResponse.Indices == null || !getIndexResponse.Indices.Any())
{
var createIndexResponse = await ElasticClient.Indices.CreateAsync("myindex", c => c
.Map(p => p.AutoMap<MyModel>()));
}
}
catch (Exception)
{
}
}
和MyModel是这样的:
[ElasticsearchType(IdProperty = nameof(Id), RelationName = "MyModelMessage")]
public class MyModel
{
[Number(NumberType.Long, Index = true, DocValues = false)]
public long UserId { get; set; }
[Date(Index = true)]
public DateTime CreatedAt { get; set; }
[Text(Index = false)]
public string ObjectName { get; set; }
[Date(Index = true)]
public DateTime UpdateAt { get; set; }
}
答案 0 :(得分:0)
我对照NEST 7.0.1和elasticsearch 7.2.0检查了您的代码,并为该类型创建了映射:
class Program
{
[ElasticsearchType(IdProperty = nameof(Id), RelationName = "MyModelMessage")]
public class MyModel
{
[Number(NumberType.Long, Index = true, DocValues = false)]
public long UserId { get; set; }
[Date(Index = true)] public DateTime CreatedAt { get; set; }
[Text(Index = false)] public string ObjectName { get; set; }
[Date(Index = true)] public DateTime UpdateAt { get; set; }
}
static async Task Main(string[] args)
{
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings = new ConnectionSettings(pool);
connectionSettings.DefaultIndex("documents");
var client = new ElasticClient(connectionSettings);
var getIndexResponse = await client.Indices.GetAsync("myindex");
if (getIndexResponse.Indices == null || !getIndexResponse.Indices.Any())
{
var createIndexResponse = await client.Indices.CreateAsync("myindex", c => c
.Map(p => p.AutoMap<MyModel>()));
Console.WriteLine("index created");
}
}
}
和http://localhost:9200/myindex/_mapping
返回:
{
"myindex": {
"mappings": {
"properties": {
"createdAt": {
"type": "date"
},
"objectName": {
"type": "text",
"index": false
},
"updateAt": {
"type": "date"
},
"userId": {
"type": "long",
"doc_values": false
}
}
}
}
}
缺少什么吗?