Elasticsearch2.3.3版本父子关联查询问题

  • 业务场景

有两个父子type,his(订单)和white(白名单),要根据条件取white表中的cwId没有出现在his表的记录(大概是用来过滤那些没有发送过短信的客户信息,然后给他们发信息)。

{
  "mappings": {
    "tbl_cash_apply_order_his": {
      "_parent": {
        "type": "tbl_cash_white"
      },
      "_routing": {
        "required": true
      },
      "properties": {
        "aoApplNo": {
          "type": "string"
        },
        "aoCwId": {
          "type": "string"
        },
        "aoOrderStatus": {
          "type": "string"
        },
        "aoUpdateTime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    },
    "tbl_cash_white": {
      "_parent": {
        "type": "tbl_cash_batch"
      },
      "_routing": {
        "required": true
      },
      "properties": {
        "cwAlotStatus": {
          "type": "string"
        },
        "cwBatchNo": {
          "type": "string"
        },
        "cwCreditLimit": {
          "type": "string"
        },
        "cwId": {
          "type": "string"
        },
        "cwIdNo": {
          "type": "string"
        },
        "cwName": {
          "type": "string"
        },
        "cwPhoneNo": {
          "type": "string"
        },
        "cwSerialNo": {
          "type": "double"
        },
        "cwUpdateTime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    },
    "tbl_cash_batch": {
      "properties": {
        "btId": {
          "type": "string"
        },
        "btNo": {
          "type": "string"
        },
        "btUpdateTime": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        },
        "btUsefulBegin": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        },
        "btUsefulEnd": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
  }
}


  • 问题描述
在测试环境测试的时候由于是单机环境,设置的1节点1分片
"settings": {
    "number_of_shards" :   1,
    "number_of_replicas" : 0
}
然后一直没有出过问题,但是在产线部署的是3节点5分片,于是出现问题了
curl -XGET '106.75.33.233:9200/smsservice/tbl_cash_white/_search/?pretty' -H 'Content-Type: application/json' -d'
	{
		"query": {
				"bool": {
					"must_not": {
							"has_child" : {
								"type" : "tbl_cash_apply_order_his",
								"query" : {"match_all": {}}
							}
					},
					"must": {
							"query" : {
								"match_phrase" : {"cwBatchNo": "POS_2nd_20171124"}
							}
					},
					"must": {
							"query" : {
								"range" : {
									"cwSerialNo": {"gte": 30287,"lte": 30287}
								}
							}
					}
				}
		}
	}
'
命中了一个记录


可是拿这条记录中的cwId去his表查询的手发现居然能查到


仔细查看DSL和逻辑都没问题啊,怎么回事呢?

观察发现这两条有关联的记录routing值是不一样的,也就是他们有可能落在不同的分区上,是不是由于这个引起的呢?

加入分区参数(-XGET '106.75.33.233:9200/smsservice/tbl_cash_white/_search/?pretty&preference=_shards:4')发现这两条有父子联系的type记录确实位于不同的分区上,可见Elasticsearch的坑还是多啊。

  • 解决方案
其实找到问题的话,解决方案也就容易的多了,可以设置这两个type用同一个字段作为routing,也可以把他们关联成一个大表。结论就是团队中没有大牛做良好的设计的弊端终于暴露了,摸着石头过河害死人呀。


猜你喜欢

转载自blog.csdn.net/Lakers_KobeBryant/article/details/79114875
今日推荐