在 Nest 1.7.1 Delete 或 DeleteByQuery 中没有任何作用
In Nest 1.7.1 Delete or DeleteByQuery nothing works
在 Nest 1.7.1 Delete 或 DeleteByQuery 中对我没有任何作用。
我正在尝试删除以下文档:
Article article1 = new Article()
{
Id = 1111,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = _dateToday,
Author = new Author() { Id = 100, Name = "Mikey" },
};
Article article2 = new Article()
{
Id = 2222,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = _dateToday,
Author = new Author() { Id = 100, Name = "Mikey" },
Published = true
};
我原以为下面的查询会删除单个文档和索引中的所有文档,但没有查询正在删除。
_elasticClient.Delete(article).Found;
_elasticClient.DeleteByQuery<Article>(q => q.Query(t => t.Term(m => m.OnField(f => f.Id).Value(articleId))))
.Found;
_elasticClient.DeleteByQuery<Article>(q => q.MatchAll()).IsValid;
如果我做错了什么,请指正。
这是一个工作示例
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"), "articles");
var client = new ElasticClient(settings);
if (client.IndexExists("articles").Exists)
{
client.DeleteIndex("articles");
}
client.CreateIndex("articles", c => c
.AddMapping<Article>(m => m
.MapFromAttributes()
)
);
var today = DateTime.Now.Date;
var article1 = CreateArticle(1111, today);
var article2 = CreateArticle(2222, today);
var article3 = CreateArticle(3333, today);
var article4 = CreateArticle(4444, today);
var bulkRequest = new BulkDescriptor();
bulkRequest.Index<Article>(i => i.Document(article1));
bulkRequest.Index<Article>(i => i.Document(article2));
bulkRequest.Index<Article>(i => i.Document(article3));
bulkRequest.Index<Article>(i => i.Document(article4));
bulkRequest.Refresh();
client.Bulk(bulkRequest);
var searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search: {0}. Expect 4", searchResponse.Documents.Count());
client.Delete(article1, d => d.Refresh());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 3", searchResponse.Documents.Count());
client.Delete(article2, d => d.Refresh());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 2", searchResponse.Documents.Count());
client.DeleteByQuery<Article>(q => q.MatchAll());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 0", searchResponse.Documents.Count());
}
private Article CreateArticle(int id, DateTime articleDate)
{
return new Article()
{
Id = id,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = articleDate,
Author = new Author() { Id = 100, Name = "Mikey" },
Published = true
};
}
public class Article
{
public int Id { get; set;}
public string Title{ get; set;}
public string Summary { get; set;}
public string Body { get; set;}
public DateTime ArticleDate { get; set; }
public Author Author { get; set; }
public bool Published { get; set;}
}
public class Author
{
public int Id { get; set; }
public string Name { get; set;}
}
结果
Documents from search: 4. Expect 4
Documents from search 3. Expect 3
Documents from search 2. Expect 2
Documents from search 0. Expect 0
符合预期。
需要记住的一点是,Elasticsearch 是 最终一致的,这意味着被索引的文档不会出现在 search 结果中,直到在 refresh interval 之后(默认情况下,1 秒);同样,对于删除查询,标记为删除的文档将出现在搜索结果中,直到刷新间隔结束。
对具有给定 ID 的给定文档的 GET 请求 将 但是 return 在刷新间隔之前的文档。
如果您需要文档可搜索(或删除后不显示在搜索结果中),您可以在操作后刷新索引,就像我对上面的批量和删除调用所做的那样,使用 .Refresh()
。您可能想在 every 操作之后调用刷新,但是我建议您仅在真正需要时才使用它,因为它会增加集群的开销并调用 all 时间可能会降低性能。
我终于成功了。
通过 fiddler 中的 NEST
发送的删除请求是 DELETE /articlestest/article/_query
而在插件中工作的查询是 DELETE /articlestest/articles/_query
(文档类型名称在代码中拼写错误) 。这就是原因,查询没有通过 NEST
删除文档。糟糕的是,它甚至没有抱怨不存在的文档类型 :( 我花了一段时间才发现这个问题。
在 Nest 1.7.1 Delete 或 DeleteByQuery 中对我没有任何作用。
我正在尝试删除以下文档:
Article article1 = new Article()
{
Id = 1111,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = _dateToday,
Author = new Author() { Id = 100, Name = "Mikey" },
};
Article article2 = new Article()
{
Id = 2222,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = _dateToday,
Author = new Author() { Id = 100, Name = "Mikey" },
Published = true
};
我原以为下面的查询会删除单个文档和索引中的所有文档,但没有查询正在删除。
_elasticClient.Delete(article).Found;
_elasticClient.DeleteByQuery<Article>(q => q.Query(t => t.Term(m => m.OnField(f => f.Id).Value(articleId))))
.Found;
_elasticClient.DeleteByQuery<Article>(q => q.MatchAll()).IsValid;
如果我做错了什么,请指正。
这是一个工作示例
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"), "articles");
var client = new ElasticClient(settings);
if (client.IndexExists("articles").Exists)
{
client.DeleteIndex("articles");
}
client.CreateIndex("articles", c => c
.AddMapping<Article>(m => m
.MapFromAttributes()
)
);
var today = DateTime.Now.Date;
var article1 = CreateArticle(1111, today);
var article2 = CreateArticle(2222, today);
var article3 = CreateArticle(3333, today);
var article4 = CreateArticle(4444, today);
var bulkRequest = new BulkDescriptor();
bulkRequest.Index<Article>(i => i.Document(article1));
bulkRequest.Index<Article>(i => i.Document(article2));
bulkRequest.Index<Article>(i => i.Document(article3));
bulkRequest.Index<Article>(i => i.Document(article4));
bulkRequest.Refresh();
client.Bulk(bulkRequest);
var searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search: {0}. Expect 4", searchResponse.Documents.Count());
client.Delete(article1, d => d.Refresh());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 3", searchResponse.Documents.Count());
client.Delete(article2, d => d.Refresh());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 2", searchResponse.Documents.Count());
client.DeleteByQuery<Article>(q => q.MatchAll());
searchResponse = client.Search<Article>(q => q.MatchAll());
Console.WriteLine("Documents from search {0}. Expect 0", searchResponse.Documents.Count());
}
private Article CreateArticle(int id, DateTime articleDate)
{
return new Article()
{
Id = id,
Title = "Title - Test Elastic Search",
Summary = "Summary - Test Elastic Search",
Body = "Body - Test Elastic Search",
ArticleDate = articleDate,
Author = new Author() { Id = 100, Name = "Mikey" },
Published = true
};
}
public class Article
{
public int Id { get; set;}
public string Title{ get; set;}
public string Summary { get; set;}
public string Body { get; set;}
public DateTime ArticleDate { get; set; }
public Author Author { get; set; }
public bool Published { get; set;}
}
public class Author
{
public int Id { get; set; }
public string Name { get; set;}
}
结果
Documents from search: 4. Expect 4
Documents from search 3. Expect 3
Documents from search 2. Expect 2
Documents from search 0. Expect 0
符合预期。
需要记住的一点是,Elasticsearch 是 最终一致的,这意味着被索引的文档不会出现在 search 结果中,直到在 refresh interval 之后(默认情况下,1 秒);同样,对于删除查询,标记为删除的文档将出现在搜索结果中,直到刷新间隔结束。
对具有给定 ID 的给定文档的 GET 请求 将 但是 return 在刷新间隔之前的文档。
如果您需要文档可搜索(或删除后不显示在搜索结果中),您可以在操作后刷新索引,就像我对上面的批量和删除调用所做的那样,使用 .Refresh()
。您可能想在 every 操作之后调用刷新,但是我建议您仅在真正需要时才使用它,因为它会增加集群的开销并调用 all 时间可能会降低性能。
我终于成功了。
通过 fiddler 中的 NEST
发送的删除请求是 DELETE /articlestest/article/_query
而在插件中工作的查询是 DELETE /articlestest/articles/_query
(文档类型名称在代码中拼写错误) 。这就是原因,查询没有通过 NEST
删除文档。糟糕的是,它甚至没有抱怨不存在的文档类型 :( 我花了一段时间才发现这个问题。