golang es查询的一些操作,has_child,inner_hit,对索引内父子文档的更新

1.因为业务需要查询父文档以及其下子文档,搞了很久才理清楚。

首先还是Inner_hits,inner_hits只能用在nested,has_child,has_parents查询里面

bash 复制代码
{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": { "comments.text": "awesome" }
      },
      "inner_hits": {}  // 使用inner_hits来返回匹配的comments
    }
  }
}
bash 复制代码
			"has_child": {
			  "type": "dys_static_info.md5",
			  "inner_hits":{},
			  childQuery
			}
		  

如果直接用在开始查询的对象里面,查询会报错

然后这里明确一个概念,has_child是查父文档的,里面加了inner_hits多返回的inner_hits是子文档的内容。同理,has_parent查子文档的,面加了inner_hits多返回的inner_hits是父文档的内容。

2.还是业务需求,一个索引下面有多种类型的文档,他们之间是父子关系,如果我要更新一个文档里面的值无法直接更新,需要使用script.比如我要更新index索引下面doc文档里面的filed1字段和filed2字段,更新条件是filed3字段为123。要这么写·

bash 复制代码
POST /index/_update_by_query
{
  "script": {
    "source": "ctx._source['doc']['filed1'] = params.tags;ctx._source['doc']['filed2'] = params.state;",
    "lang": "painless",
    "params": {
      "tags": [1, 2],
	  "state":"W"
    }
  },
  "query": {
    "term": {
      "doc.filed3.keyword": "123"
    }
  }
}

然后使用golang的github.com/olivere/elastic/v7 得这么写

bash 复制代码
updateQuery := elastic.NewTermQuery("doc.filed3.keyword", "123")
script = elastic.NewScript(`
		ctx._source['doc']['filed1'] = params.tags;
		ctx._source['doc']['filed2'] = params.state;
	`).Params(map[string]interface{}{"tags": TagList, "state": SampleState})
_, err = global.GVA_ES.UpdateByQuery("index").Query(updateQuery).Script(script).Do(context.Background())
	if err != nil {
		return
	}
	response.Ok(c)
}
相关推荐
斯维赤几秒前
Python学习超简单第十一弹:邮件发送
开发语言·python·学习
CoderCodingNo2 分钟前
【信奥业余科普】C++ 的奇妙之旅 | 14:程序的分叉路口——逻辑判断与 if-else 语句
开发语言·c++
The Chosen One98510 分钟前
a进制转b进制的转换总结
开发语言·c++
ECT-OS-JiuHuaShan13 分钟前
哲学的本质,是递归因果
java·开发语言·人工智能·科技·算法·机器学习·数学建模
overmind15 分钟前
oeasy Python 124 序列_字符串_string_str
开发语言·python
一个假的前端男16 分钟前
Flutter 实现 BLE 设备 WiFi 配网流程实践
开发语言·flutter
片酷23 分钟前
【Isaacsim&Isaaclab】安装教程
linux·开发语言·python
Magic@28 分钟前
Redis学习[1] ——基本概念和数据类型
linux·开发语言·数据库·c++·redis·学习
黑不溜秋的29 分钟前
C++ STL reduce 用法
开发语言·c++
倾听一世,繁花盛开29 分钟前
Java语言程序设计——篇十三(1)
java·开发语言·ide·eclipse