업데이트 답 : 아래
임베디드 중첩 된 형태의 문서 "차일"와 부모 - 자식 중첩 된 형식의 문서의 구조입니다.
{
"parent_id": 1,
"parent_name": "ABC",
"parent_number": 123,
"parent_addr": "123 6th St. Melbourne, FL 32904"
"childs": [
{
"child_id": 1,
"child_name": "PQR",
"child_number": 456,
"child_age": 10
},
{
"child_id": 2,
"child_name": "XYZ",
"child_number": 789,
"child_age": 12
},
{
"child_id": 3,
"child_name": "QWE",
"child_number": 234,
"child_age": 16
}
]
}
매핑은 다음과 같습니다
RDMS에서 PUT parent/
{
"parent": {
"mappings": {
"parent": {
"properties": {
"parent_id": {
"type": "long"
},
"parent_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"parent_number": {
"type": "long"
},
"parent_addr": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"child_tickets": {
"type": "nested",
"properties": {
"child_id": {
"type": "long"
},
"child_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"child_number": {
"type": "long"
},
"child_age": {
"type": "long"
}
}
}
}
}
}
}
}
, 이들 엔티티 (부모, 자녀) 모두 부모 사이에 많은 관계로 하나와 두 개의 서로 다른 테이블을하다 -> 어린이. 부모의 ID는 하위 행의 외래 키입니다. (id는 두 테이블에 모두 필요함)
이제 Elasticsearch에서 부모 문서를 색인화하려면 id를 색인화해야합니다 (이 경우 parent_id). 색인 부모 문서 쿼리 (PARENT_ID 내가 얘기하고 인덱스를 ID (_id와 문서가 된 ID입니다) = 1) : 이제
POST parent/parent/1
{
"parent_id": 1,
"parent_name": "ABC",
"parent_number": 123,
"parent_addr": "123 6th St. Melbourne, FL 32904"
}
, 부모에게 자녀 (들)을 추가. 이를 위해서는 하위 ID와 상위 ID가 있어야하는 하위 문서가 필요합니다. 하위를 추가하려면 상위 ID가 있어야합니다.다음은 새 하위 항목을 추가하거나 이미있는 하위 항목을 업데이트하는 업데이트 쿼리입니다.
POST parent/parent/1/_update
{
"script":{
"lang":"painless",
"inline":"if (!ctx._source.containsKey(\"childs\")) {
ctx._source.childs = [];
ctx._source.childs.add(params.child);
} else {
int flag=0;
for(int i=0;i<ctx._source.childs.size();i++){
if(ctx._source.childs[i].child_id==params.child.child_id){
ctx._source.childs[i]=params.child;
flag++;
}
}
if(flag==0){
ctx._source.childs.add(params.child);
}
}",
"params":{
"child":{
"child_id": 1,
"child_name": "PQR",
"child_number": 456,
"child_age": 10
}
}
}
}
사진을 찍어주세요. 건배!
다른 것이 필요한 경우 알려주십시오.
감사합니다. Hatim, 반복하지 않고 중첩 된 객체를 저장하기 위해 스크립트를 공유 할 수 있습니까? – mkalsi
@mkalsi 예 & 스크립트로 내 답변을 업데이트했습니다. 확인해 봐. –
2 천만 개의 문서를 색인 할 계획이므로 데이터 세트가 매우 큰 경우 인라인 스크립트의 성능은 어떻게됩니까? – mkalsi