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)
}
相关推荐
༾冬瓜大侠༿19 小时前
C++内存和模板
java·开发语言·c++
半路_出家ren19 小时前
Python操作MySQL(详细版)
运维·开发语言·数据库·python·mysql·网络安全·wireshark
共享家952719 小时前
MYSQL-内外连接
开发语言·数据库·mysql
古城小栈19 小时前
Go 语言 Flight Recorder:低开销性能分析工具实战
数据库·golang
ppo_wu19 小时前
介绍下CompletableFuture的用法
java·开发语言
吴佳浩 Alben19 小时前
Python入门指南(七) - YOLO检测API进阶实战
开发语言·python·yolo
沐知全栈开发1 天前
HTML5 浏览器支持
开发语言
wasp5201 天前
AgentScope Java 核心架构深度解析
java·开发语言·人工智能·架构·agentscope
WHOVENLY1 天前
【javaScript】- 笔试题合集(长期更新,建议收藏,目前已更新至31题)
开发语言·前端·javascript
小高Baby@1 天前
Go语言中判断map 中是否包含某个key 的方法
golang