1 update api:
people/person/2/_update
{
"doc": {
"Lastname": "海峡2"
}
}
2 script:
这时候当API不能满足要求时,Elasticsearch允许你使用脚本实现自己的逻辑。脚本支持非常多的API,例如搜索、排序、聚合和文档更新。脚本可以通过请求的一部分、检索特殊的.scripts
索引或者从磁盘加载方式执行。
需要开启脚本支持,es配置文件中要加入:
script.inline: on
script.indexed: on
script.file: on
根据ID修改:people/person/2/_update
1)脚本能够使用update
API改变_source
字段的内容,它在脚本内部以ctx._source
表示
{
"script": "ctx._source.Lastname='川普'"
}
2)当我们试图更新一个不存在的文档,更新将失败,在这种情况下,我们可以使用upsert
参数定义文档来使其不存在时被创建
{
"script" : "ctx._source.Lastname='川普'",
"upsert": {
"views": 1
}
}
删除:{ "script": " ctx.op= ctx._source.Lastname='川普' ?'delete':'none'"}
根据条件修改:people/person/_update_by_query
讲firstname中包含川字的文档的:lastname改为希拉里,Firstname修改为四川普通话
{
"query": {
"term": {
"Firstname": "川"
}
},
"script": {
"inline": "ctx._source.Lastname='希拉里';ctx._source.Firstname='四川普通话'"
}
}
nest:
//UpdateByQueryDescriptor
Func<UpdateByQueryDescriptor<Person>, IUpdateByQueryRequest> fun = u => u.Query(q => q.Term("Firstname", "川")).Script(s => s.Inline("ctx._source.Lastname='希拉里1';ctx._source.Firstname='四川普通话1'")); client.UpdateByQuery<Person>("people", "person", fun);
继续参考:
继续参考:
/// <summary>
/// 根据查询条件进行更新
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="indexName"></param>
/// <param name="typeName"></param>
/// <param name="script"></param>
/// <returns></returns>
public static IUpdateByQueryResponse UpdateByQuery<T>(string indexName, string typeName, string script, Func<QueryContainerDescriptor<T>, QueryContainer> querySelector) where T : class
{
IUpdateByQueryResponse updateResponses = null;
if (!string.IsNullOrEmpty(script))
{
try
{
updateResponses = ESHelper.GetInstance()
.GetElasticClient()
.UpdateByQuery<T>(indexName, typeName, u => u.Query(querySelector)
.Script(s => s.Inline(script)
.Lang("groovy"))
.Refresh()
.Conflicts(Elasticsearch.Net.Conflicts.Proceed)
.WaitForCompletion(false));
}
catch (Exception ex)
{
LogInfoWriter.GetInstance("error").Error("方法:UpdateByQuery 批处理异常:" + ex.ToString());
}
}
return updateResponses;
}
/// <summary>
/// 根据查询条件进行删除
/// </summary>
/// <param name="indexName"></param>
/// <param name="typeName"></param>
/// <param name="qc"></param>
/// <returns></returns>
[CoreCat]
public static IDeleteByQueryResponse DeleteByQuery(string indexName, string typeName, QueryContainer qc)
{
IDeleteByQueryResponse response = null;
try
{
IDeleteByQueryRequest deleteQuery = new DeleteByQueryRequest(indexName, typeName) { Query = qc };
response = ESHelper.GetInstance().GetElasticClient().DeleteByQuery(deleteQuery);
}
catch (Exception ex)
{
LogInfoWriter.GetInstance("error").Error("方法:DeleteByQuery 批处理异常:" + ex.ToString());
}
return response;
}
3 DeleteByQuery命令
需要安装插件 DeleteByQuery 并重启es: sudo bin/plugin install delete-by-query 根据条件删除文档: DELETE: productindex/product/_query body: { "query": { "match": { "sysNo": "3269 3271" } } } 删除某个类型下的所有文档 DELETE: productindex/product/_query body: { "query": { "match_all": { } } } NETS命令: IDeleteByQueryRequest deleteQuery = new DeleteByQueryRequest(indexName, typeName) { Query = qc }; IDeleteByQueryResponse response = ESHelper.GetInstance().GetElasticClient().DeleteByQuery(deleteQuery);
bool query
语法:
{ "size": 20, "query": { "bool": { "must": [ { "term": { "customerSysNo": 7308257 } }, { "term": { "isShow": true } } ], "must_not": { "term": { "sOID": 1003219103 } } } } }
nest语法:
List<QueryContainer> mustnotqcs = new List<QueryContainer>(); List<QueryContainer> mustqcs = new List<QueryContainer>(); //mustnotqcs mustnotqcs.Add(new TermQuery { Field = "sOID", Value =1003219103 }); //mustqcs mustqcs.Add(new TermQuery { Field = "customerSysNo", Value = 7308257 }); mustqcs.Add(new TermQuery { Field = "isShow", Value = "true" }); BoolQuery qcbool = new BoolQuery() { Must = mustqcs, MustNot = mustnotqcs };