Elasticsearch Nest是否支持按查询更新

时间:2018-04-27 20:05:12

标签: elasticsearch nest

我想在高级客户端上使用UpdateByQuery方法,但找不到Nest的任何文档。如果我想提出一个CURL请求但是NEST没有任何内容,那么它们有很好的文档。 https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html 如果有人和他们的例子使用它或者可以共享文件,他们发现这将是很棒的!

1 个答案:

答案 0 :(得分:5)

NEST支持

Update By Query API。这是一个例子adapted from the integration tests。计划使用NEST Documentation索引和更新API:)

private static void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));

    var settings = new ConnectionSettings(pool)
        .DefaultMappingFor<Test>(m => m
            .IndexName("tests")
            .TypeName("test")
        );

    var client = new ElasticClient(settings);
    var index = IndexName.From<Test>();

    if (client.IndexExists(index).Exists)
        client.DeleteIndex(index);

    client.CreateIndex(index, c => c
        .Mappings(m => m
            .Map<Test>(map => map
                .Properties(props => props
                    .Text(s => s.Name(p => p.Text))
                    .Keyword(s => s.Name(p => p.Flag))
                )
            )
        )
    );

    client.Bulk(b => b
        .IndexMany(new[] {
            new Test { Text = "words words", Flag = "bar" },
            new Test { Text = "words words", Flag = "foo" }
        })
        .Refresh(Refresh.WaitFor)
    );

    client.Count<Test>(s => s
        .Query(q => q
            .Match(m => m
                .Field(p => p.Flag)
                .Query("foo")
            )
        )
    );

    client.UpdateByQuery<Test>(u => u
        .Query(q => q
            .Term(f => f.Flag, "bar")
        )
        .Script("ctx._source.flag = 'foo'")
        .Conflicts(Conflicts.Proceed)
        .Refresh(true)
    );

    client.Count<Test>(s => s
        .Query(q => q
            .Match(m => m
                .Field(p => p.Flag)
                .Query("foo")
            )
        )
    );
}

public class Test
{
    public string Text { get; set; }
    public string Flag { get; set; }
}

观察第一个Count API调用的计数是1,在Update By Query API调用之后的第二个Count API调用中,它是2。