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

1.因为业务需要查询父文档以及其下子文档,搞了很久才理清楚。
首先还是Inner_hits,inner_hits只能用在nested,has_child,has_parents查询里面

{
  "query": {
    "nested": {
      "path": "comments",
      "query": {
        "match": { "comments.text": "awesome" }
      },
      "inner_hits": {}  // 使用inner_hits来返回匹配的comments
    }
  }
}

			"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。要这么写·

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 得这么写

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)
}
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值