Russell McDonnell
Russell McDonnell

Reputation: 150

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!

Upvotes: 5

Views: 6409

Answers (1)

Russ Cam
Russ Cam

Reputation: 125488

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.

Upvotes: 8

Related Questions