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)
}
相关推荐
aramae3 小时前
模拟实现strcmp()(C语言)
c语言·开发语言·后端
泡海椒4 小时前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
Beyond_System|系统之外5 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55235 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
Bruce_Liuxiaowei8 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
aramae9 小时前
MySQL内置函数(7)
开发语言·笔记·后端·mysql·其他
五彩小白10 小时前
GO语言装饰器语法
golang
码行山野赴时序归途12 小时前
链表家族收官篇:循环链表
c语言·开发语言·数据结构·链表
景熙552312 小时前
单体项目编写思路和落地形式
java·开发语言
计算机毕设定制辅导-无忧学长13 小时前
《基于SpringBoot的未成年人健康知识科普平台的设计与实现》
java·开发语言·vue.js·spring boot·未成年人健康知识科普平台