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": "你好!" }
  }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM