1. 程式人生 > >Elasticsearch2.3.3版本父子關聯查詢問題

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,也可以把他們關聯成一個大表。結論就是團隊中沒有大牛做良好的設計的弊端終於暴露了,摸著石頭過河害死人呀。