弹性搜索嵌套索引查询总是返回false
Elastic search Nest index query always returning false
var local = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(local, null);
var elastic = new ElasticClient(settings);
var res = elastic.CreateIndex(ci => ci
.Index("my_first_index_final2")
.AddMapping<BlogPost>(m => m.MapFromAttributes()));
Console.WriteLine(res.RequestInformation.Success);
var blogPost = new BlogPost
{
Id = Guid.NewGuid(),
Title = "First blog post",
Body = "This is very long blog post!"
};
var firstId = blogPost.Id;
var result = elastic.Index(blogPost, p => p
.Index("my_first_index_final2")
.Id(blogPost.Id.ToString())
.Refresh());
Console.WriteLine(result.RequestInformation.Success);
博文class:
[ElasticType(IdProperty = "Id", Name = "blog_post")]
public class BlogPost
{
[ElasticProperty(Name = "_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public Guid? Id { get; set; }
[ElasticProperty(Name = "title", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Title { get; set; }
[ElasticProperty(Name = "body", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Body { get; set; }
public override string ToString()
{
return string.Format("Id: '{0}', Title: '{1}', Body: '{2}'", Id, Title, Body);
}
}
这是我的代码。每次 returns:
真的
假
意思是,它创建索引但无法将文档插入索引。我不明白原因。
此外,每次我 运行 这个演示控制台应用程序时,我都必须重命名我的索引名称,因为我认为我们不能插入具有相同名称的索引。我怎样才能避免这样做?
我正在学习本教程:
https://www.devbridge.com/articles/getting-started-with-elastic-using-net-nest-library-part-two/
任何其他学习nest和elasticsearch的资源,欢迎提出。
我猜你正在使用 Elasticsearch 2.x。您的代码不会在 Elasticsearch 1.x 中中断。问题是,您正试图在文档中添加字段 _id
。它是元数据字段之一,Elasticsearch 2.x 禁止您在文档中对其进行索引。要使您的代码正常工作,只需将 Id
字段的名称从 _id
更改为其他名称,例如 id
.
var local = new Uri("http://localhost:9200");
var settings = new ConnectionSettings(local, null);
var elastic = new ElasticClient(settings);
var res = elastic.CreateIndex(ci => ci
.Index("my_first_index_final2")
.AddMapping<BlogPost>(m => m.MapFromAttributes()));
Console.WriteLine(res.RequestInformation.Success);
var blogPost = new BlogPost
{
Id = Guid.NewGuid(),
Title = "First blog post",
Body = "This is very long blog post!"
};
var firstId = blogPost.Id;
var result = elastic.Index(blogPost, p => p
.Index("my_first_index_final2")
.Id(blogPost.Id.ToString())
.Refresh());
Console.WriteLine(result.RequestInformation.Success);
博文class:
[ElasticType(IdProperty = "Id", Name = "blog_post")]
public class BlogPost
{
[ElasticProperty(Name = "_id", Index = FieldIndexOption.NotAnalyzed, Type = FieldType.String)]
public Guid? Id { get; set; }
[ElasticProperty(Name = "title", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Title { get; set; }
[ElasticProperty(Name = "body", Index = FieldIndexOption.Analyzed, Type = FieldType.String)]
public string Body { get; set; }
public override string ToString()
{
return string.Format("Id: '{0}', Title: '{1}', Body: '{2}'", Id, Title, Body);
}
}
这是我的代码。每次 returns: 真的 假
意思是,它创建索引但无法将文档插入索引。我不明白原因。
此外,每次我 运行 这个演示控制台应用程序时,我都必须重命名我的索引名称,因为我认为我们不能插入具有相同名称的索引。我怎样才能避免这样做?
我正在学习本教程: https://www.devbridge.com/articles/getting-started-with-elastic-using-net-nest-library-part-two/
任何其他学习nest和elasticsearch的资源,欢迎提出。
我猜你正在使用 Elasticsearch 2.x。您的代码不会在 Elasticsearch 1.x 中中断。问题是,您正试图在文档中添加字段 _id
。它是元数据字段之一,Elasticsearch 2.x 禁止您在文档中对其进行索引。要使您的代码正常工作,只需将 Id
字段的名称从 _id
更改为其他名称,例如 id
.