Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Elasticsearch Nest support Update By Query

I want to use the UpdateByQuery method on the high level client but can't find any documentation for Nest. They have great documentation if I wanted to make a CURL request but nothing for NEST. https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update-by-query.html If anyone has and example of them using it or can share documentation they have found that would be awesome!

like image 415
Russell McDonnell Avatar asked Mar 07 '23 08:03

Russell McDonnell


1 Answers

Update By Query API is supported in NEST. Here's an example adapted from the integration tests. NEST Documentation for Index and Update APIs is planned :)

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

Observe that the count from the first Count API call is 1, and on the second Count API call after the Update By Query API call, it's 2.

like image 140
Russ Cam Avatar answered Mar 10 '23 10:03

Russ Cam