我正在开发一个asp.net应用程序,我有庞大的数据库。我想实现弹性搜索。这是我在代码中所做的:
var node = new Uri("http://localhost:9200/");
var setting = new ConnectionSettings(node);
setting.DefaultIndex("businessuser");
client = new ElasticClient(setting);
CanadaBusinessDBEntities db = new CanadaBusinessDBEntities();
client.DeleteIndex("businessuser", null);
var ListofBusiness = db.CanadaTables.ToList();
foreach (var Business in ListofBusiness)
{
var resutl = client.Index(Business, null);
}
此代码是在构造函数中编写的,它获取所有记录,然后使用弹性搜索对它们进行索引。索引需要很长时间。我想问一下这是否正确?我是弹性搜索的新手。请建议更好的方法来做到这一点。
感谢。
答案 0 :(得分:0)
你必须使用ElasticsearchContext Class of Nest。
private readonly ElasticsearchContext _elasticsearchContext;
private const string ConnectionString = "http://localhost:9200";
private readonly IElasticsearchMappingResolver _elasticsearchMappingResolver;
_elasticsearchMappingResolver = new ElasticsearchMappingResolver();
_elasticsearchContext = new ElasticsearchContext(ConnectionString, new ElasticsearchSerializerConfiguration(_elasticsearchMappingResolver,true,true));
出于删除目的,您可以像下面那样使用
public void DeleteSkill(int Id)
{
_elasticsearchContext.DeleteDocument<Tag>(Id);
_elasticsearchContext.SaveChanges();
}