在 Mapping 里面,将 dynamic 参数设置成 strict 可以拒绝索引包含未知字段的文档。 此条 Tips 由 medcl 贡献。

has_child 查询,如何查询几个字表中,其中一个字段相同结果的父记录

Elasticsearch | 作者 myst | 发布于2018年07月02日 | 阅读数:3294

基础数据:
{"index":{"_id":"dagrxx1"}}
{"dagrxx":{"grid":"1","xm":"张三","xb":"1","csrq":"1982-11-18"},"rhmjoin":{"name":"dagrxx"}}
{"index":{"_id":"dagrxx2"}}
{"dagrxx":{"grid":"2","xm":"李四","xb":"1","csrq":"1992-1-10"},"rhmjoin":{"name":"dagrxx"}}
{"index":{"_id":"gxysfjl1","_routing":"dagrxx1"}}
{"gxysfjl":{"sfid":"1","grid":"1","zz":"1","sg":"165","sfrq":"2018-7-1"},"rhmjoin":{"name":"gxysfjl","parent": "dagrxx1"}}
{"index":{"_id":"gxysfjl2","_routing":"dagrxx1"}}
{"gxysfjl":{"sfid":"2","grid":"1","zz":"2","sg":"155","sfrq":"2018-3-1"},"rhmjoin":{"name":"gxysfjl","parent": "dagrxx1"}}
{"index":{"_id":"tnbsfjl1","_routing":"dagrxx1"}}
{"tnbsfjl":{"sfid":"1","grid":"1","zz":"1","sg":"164","sfrq":"2018-7-1"},"rhmjoin":{"name":"tnbsfjl","parent": "dagrxx1"}}
{"index":{"_id":"gxysfjl3","_routing":"dagrxx2"}}
{"gxysfjl":{"sfid":"3","grid":"2","zz":"1","sg":"175","sfrq":"2018-7-1"},"rhmjoin":{"name":"gxysfjl","parent": "dagrxx2"}}
 
我想查出 gxysfjl.sfrq == tnbsfjl.sfrq && gxysfjl.zz == "1" && tnbsfjl.zz == "1" 的父记录
 
目前能写到的程度如下:
 
{
"query": {
"bool": {
"filter": [{
"has_child": {
"type": "gxysfjl",
"query": {
"bool": {
"filter": [{
"term": {
"gxysfjl.zz": "1"
}
}]
}
}
}
}, {
"has_child": {
"type": "tnbsfjl",
"query": {
"bool": {
"filter": [{
"term": {
"tnbsfjl.zz": "1"
}
}]
}
}
}
}]
}
}
}

 
gxysfjl.sfrq == tnbsfjl.sfrq  这个条件不知道怎么加。
 
已邀请:

laoyang360 - 《一本书讲透Elasticsearch》作者,Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:http://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自:

借助script试试

myst

赞同来自:

 
"has_child": { "type": ["gxysfjl"], "query": { "script": { "script": { "lang": "painless", "source": "doc['gxysfjl.zz'].value == '1'" } } } }

这样可以查出,但是
"has_child": { "type": ["gxysfjl","tnbsfjl"], "query": { "script": { "script": { "lang": "painless", "source": "doc['gxysfjl.zz'].value == '1'" } } } }
这样就查不出,但不报错
 

要回复问题请先登录注册