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)
}
相关推荐
十八旬1 天前
【超简单】后端入门案例-基于SpringBoot+MyBatis-plus+MySQL图书管理系统
java·开发语言·idea·intellij idea·项目实战
u0104058361 天前
电商导购平台的搜索引擎优化:基于Elasticsearch的商品精准推荐系统
elasticsearch·搜索引擎·jenkins
0_0梅伊阁诗人1 天前
Redis
开发语言·笔记·python
wearegogog1231 天前
C#与Twincat 2 实现上位机控制软PLC功能
开发语言·c#
在未来等你1 天前
Elasticsearch面试精讲 Day 16:索引性能优化策略
大数据·分布式·elasticsearch·搜索引擎·面试
军训猫猫头1 天前
12.NModbus4在C#上的部署与使用 C#例子 WPF例子
开发语言·c#·wpf
Eiceblue1 天前
使用 C# 设置 Excel 单元格格式
开发语言·后端·c#·.net·excel
正义的大古1 天前
OpenLayers数据源集成 -- 章节八:天地图集成详解
开发语言·javascript·ecmascript·openlayers
zhangfeng11331 天前
R geo 然后读取数据的时候 make.names(vnames, unique = TRUE): invalid multibyte string 9
开发语言·chrome·r语言·生物信息
Sally璐璐1 天前
Go组合式继承:灵活替代方案
开发语言·后端·golang