Search code examples
c#elasticsearchnest

c# nest for elasticsearch: how to convert query that updates particular field of elastic search document to nest?


We have a document that has a fields "sortScore" and "id". We want to update "sortScore" of particular documents which can be uniquely identified by "id".

I have created elastic search query for v 5.4.1 and this updates the "sortScore" perfectly:

POST /stock/1364740/_update
{
    "script" : {
        "inline": "ctx._source.sortScore += params.count",
        "lang": "painless",
        "params" : {
            "count" : 5
        }
    }
}

What would be the corresponding c# nest query for this?


Solution

  • Your query doesn't quite look correct; the URI has an index name, "stock", and an id, "1364740", but doesn't appear to have a type name.

    An Update request would look like the following with NEST

    var updateResponse = client.Update<object>(1364740, u => u
        .Index("stock")
        .Script(s => s
            .Inline("ctx._source.sortScore += params.count")
            .Lang("painless")
            .Params(d => d
                .Add("count", 5)
            )
        )
    );
    

    which produces the request JSON

    POST http://localhost:9200/stock/object/1364740/_update 
    {
      "script": {
        "params": {
          "count": 5
        },
        "lang": "painless",
        "inline": "ctx._source.sortScore += params.count"
      }
    }
    

    Notice that the request has a type name object in the URI, which has been inferred from the object generic type parameter.