在Nest 1.7.1中,Delete或DeleteByQuery无效

时间:2016-04-12 12:22:07

标签: elasticsearch nest

在Nest 1.7.1中删除或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;

如果我做错了,请纠正我。

2 个答案:

答案 0 :(得分:0)

这是一个有效的例子

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 最终是一致的,这意味着索引的文档在 {{3}之前不会出现在 search 结果中}} (默认情况下,1秒);同样,对于删除查询,标记为删除的文档将出现在搜索结果中,直到刷新间隔结束。

对于具有给定标识的给定文档的GET请求将但是在刷新间隔之前返回文档。

如果您需要可搜索的文档(或删除后不显示在搜索结果中),您可以使用.Refresh()在操作后刷新索引,就像上面的批量和删除调用一样。您可能很想在每次操作后调用刷新,但我建议仅在您确实需要时才使用它,因为它会增加群集的开销并且会调用 all 时间可能会降低性能。

答案 1 :(得分:0)

我终于开始工作了。

在fiddler中通过NEST发送的删除请求为DELETE /articlestest/article/_query,插件中的查询为DELETE /articlestest/articles/_query(文档类型名称在代码中拼写错误)。 #39;原因是,查询没有通过NEST删除文件。不好的是,它甚至没有抱怨不存在的文件类型:(我花了一段时间才发现那个问题。