在Nest 1.7.1删除或DeleteByQuery没有任何作用 [英] In Nest 1.7.1 Delete or DeleteByQuery nothing works

查看:463
本文介绍了在Nest 1.7.1删除或DeleteByQuery没有任何作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Nest 1.7.1中删除或DeleteByQuery对我来说无效。



我正在尝试删除以下文件:

 文章article1 =新的文章()
{
Id = 1111,
标题=标题 - 测试弹性搜索,
摘要=摘要 - 测试弹性搜索,
体=Body-Test Elastic Search,
ArticleDate = _dateToday,
作者=新作者(){Id = 100,Name =Mikey},
};

文章article2 = new Article()
{
Id = 2222,
Title =Title - Test Elastic Search,
Summary =Summary - Test Elastic Search,
Body =Body - Test Elastic Search,
ArticleDate = _dateToday,
作者=新作者(){Id = 100,Name =Mikey},
发布= true
};

我期望下面的查询会删除索引中的单个文档和所有文档,但不删除查询。



_elasticClient.Delete(article)发现;

  _elasticClient.DeleteByQuery< Article>(q => q.Query(t => t.Term(m => m.OnField(f => f.Id) .Value(articleId))))
发现;


_elasticClient.DeleteByQuery< Article>(q => q.MatchAll())。

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

解决方案

这是一个工作示例

  void Main()
{
var settings = new ConnectionSettings(new Uri(http:// localhost:9200),articles);
var client = new ElasticClient(settings);

if(client.IndexExists(articles)。存在)
{
client.DeleteIndex(articles);
}

client.CreateIndex(articles,c => c
.AddMapping< Article>(m => m
.MapFromAttributes()

);

var today = DateTime.Now.Date;

var article1 = CreateArticle(1111,今天);
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());

}

私人文章CreateArticle(int id,DateTime articleDate)
{
返回新文章()
{
Id = id,
Title =Title - Test Elastic Search,
Summary =Summary - Test Elastic Search,
Body =Body - Test Elastic Search,
ArticleDate = articleDate,
作者= new作者(){Id = 100,Name =Mikey},
发布= true
};
}

public class Article
{
public int Id {get; set;}
public string标题{get; set;}
public string Summary {get; set;}
public string Body {get; set;}
public DateTime ArticleDate {get;组; }
public作者Author {get;组; }
public bool发布{get; set;}
}

public class Author
{
public int Id {get;组; }
public string Name {get; set;}
}

结果

 搜索文件:4.预期4 
搜索文件3.预期3
搜索文件2.预期2
搜索文档0.预期0



需要记住的是,弹性搜索最终一致意味着索引的文档不会出现在搜索结果,直到 刷新间隔 (默认为1秒);同样,使用删除查询,标记为删除的文档将显示在搜索结果中,直到刷新间隔过去。



给定文档的GET请求,然后在刷新间隔之前返回文档。



如果您需要可搜索的文档(或删除后不显示在搜索结果中) ,您可以使用 .Refresh()在上述操作后刷新索引,就像使用批量和删除调用一样。您可能会尝试在每个操作后调用刷新,但是我建议仅在您真正需要添加集群的开销时使用它,并且调用所有时间可能会降低性能。


In Nest 1.7.1 Delete or DeleteByQuery nothing works for me.

I am trying to delete below documents:

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
            };

I was expecting below queries would delete single document and all documents in an index but no query is deleting.

_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;

Please correct me if i am doing anything wrong.

解决方案

Here's a working example

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;}
}

results in

Documents from search: 4. Expect 4
Documents from search 3. Expect 3
Documents from search 2. Expect 2
Documents from search 0. Expect 0

as expected.

Something to bear in mind is that Elasticsearch is eventually consistent meaning that a document that is indexed does not appear in search results until after a refresh interval (by default, 1 second); Likewise, with a delete query, a document marked for deletion will appear in search results until the refresh interval has elapsed.

A GET request on a given document with a given id will however return the document before the refresh interval.

If you need documents to be searchable (or to not show in search results after deletion), you can refresh the index after an operation as I did with the bulk and delete calls above, using .Refresh(). You might be tempted to call refresh after every operation, however I would recommend using it only when you really need to as it adds overhead to the cluster and called all the time would likely degrade performance.

这篇关于在Nest 1.7.1删除或DeleteByQuery没有任何作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆