elasticsearch 通过查询修改、删除

通过查询修改

update-by-query

nest

var list = new List<string> { "1", "2" };
client.UpdateByQuery<PostComment>(s => s
    .Index("post_comments")
    .Script(s=>s.Source("ctx._source.content='*'"))
    .Query(q =>
        q.Terms(t => t
            .Field(t => t.PostId)
            .Terms(list)
            )
        )
    );

http

POST http://127.0.0.1:9200/post_comments/_update_by_query
Content-Type: application/json

{
  "query": {
    "terms": {
      "postId": [
        "1",
        "2"
      ]
    }
  },
  "script": {
    "source": "ctx._source.content='*'"
  }
}

通过查询删除

delete-by-query

nest

var list = new List<string> { "1", "2" };
client.DeleteByQuery<PostComment>(s => s
    .Index("post_comments")
    .Query(q =>
        q.Terms(t => t
            .Field(t => t.PostId)
            .Terms(list)
            )
        )
    );

http

POST http://127.0.0.1:9200/post_comments/_delete_by_query
Content-Type: application/json

{"query":{"terms":{"postId":["1","2"]}}}

脚本参数

nest

client.UpdateByQuery<PostComment>(u => u
    .Index("post_comments")
    .Query(q => q
        .Term(t => t
            .Field(f => f.PostId)
            .Value("1")
            )
        )
    .Script(s => s
        .Source($"ctx._source.content = params.content")
        .Params(new Dictionary<string, object> {
            {"content", "你好!" }
        })
        )
    );

http

POST http://localhost:9200/post_comments/_update_by_query HTTP/1.1
Content-Type: application/json

{
  "query": { "term": { "postId": { "value": "1" } } },
  "script": {
    "source": "ctx._source.content = params.content",
    "params": { "content": "你好!" }
  }
}
posted @ 2020-10-28 15:11  slowstart  阅读(963)  评论(0编辑  收藏  举报