Elastic doc의 중첩 된 구조에서 객체를 제거하고 싶습니다. 이것은 내 Elastic doc이 'submissions'인덱스에서 어떻게 보이는지입니다. 조건에 따라 모든 문서에서 개체를 제거하고 싶습니다.
{
"took": 21,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 11,
"max_score": 1,
"hits": [
{
"_index": "submissions",
"_type": "_doc",
"_id": "15_12069",
"_score": 1,
"_source": {
"id": "15_12069",
"account_id": 2,
"survey_id": 15,
"submission_id": 12069,
"answers": [
{
"question_id": 142, //
"skipped": false, //<------ remove object with question_id: 142
"answer_txt": "product" //
},
{
"question_id": 153,
"skipped": false,
"answer_txt": "happy"
}
]
}
},
{
"_index": "submissions",
"_type": "_doc",
"_id": "15_12073",
"_score": 1,
"_source": {
"id": "15_12073",
"account_id": 2,
"survey_id": 15,
"submission_id": 12073,
"answers": [
{
"question_id": 142, //
"skipped": false, //<------ remove object with question_id: 142
"answer_txt": "coherent" //
},
{
"question_id": 153,
"skipped": false,
"answer_txt": "cool"
}
]
}
}
]
}
}
나는 updateByQuery api (_update_by_query) 및 ctx._source.remove with query를 시도하고 싶었습니다.
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match": {
"account_id": 2
}
},
{
"match": {
"survey_id": 15
}
}
]
}
},
{
"nested": {
"path": "answers",
"query": {
"bool": {
"must": [
{
"match": {
"answers.question_id": 142
}
}
]
}
}
}
}
]
}
}
}
이것에 대한 통찰력이나 더 나은 접근 방식이 있습니까?
다음과 같은 방법으로 Update By Query API를 사용할 수 있습니다.
인덱스 데이터, 매핑 및 쿼리로 작업 예제 추가
인덱스 매핑 :
{
"mappings": {
"properties": {
"answers": {
"type": "nested"
}
}
}
}
인덱스 데이터 :
{
"id": "15_12069",
"account_id": 2,
"survey_id": 15,
"submission_id": 12069,
"answers": [
{
"question_id": 142,
"skipped": false,
"answer_txt": "product"
},
{
"question_id": 153,
"skipped": false,
"answer_txt": "happy"
}
]
}
{
"id": "15_12073",
"account_id": 2,
"survey_id": 16,
"submission_id": 12073,
"answers": [
{
"question_id": 142,
"skipped": false,
"answer_txt": "coherent"
},
{
"question_id": 153,
"skipped": false,
"answer_txt": "cool"
}
]
}
질문:
POST /index/_update_by_query
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"match": {
"account_id": 2
}
},
{
"match": {
"survey_id": 15
}
}
]
}
},
{
"nested": {
"path": "answers",
"query": {
"bool": {
"must": [
{
"match": {
"answers.question_id": 142
}
}
]
}
}
}
}
]
}
},
"script": {
"source": "ctx._source.answers.removeIf(question_id -> question_id.question_id == params.remove_id);",
"params": {
"remove_id": 142
}
}
}
위의 질의를 수행 한 후 질의의 모든 조건, 즉 "account_id": 2
AND "survey_id": 15
AND 를 만족 "answers.question_id": 142
하는 문서는 해당 문서 객체에서 question_id: 142
제거됩니다.
따라서 첫 번째 문서 (위에 인덱싱 된대로)에서 포함 된 문서 "answers.question_id": 142
가 제거되고 이제 문서에 아래 데이터가 포함됩니다 (쿼리 실행 후).
{
"_index": "64898361",
"_type": "_doc",
"_id": "1",
"_version": 8,
"_seq_no": 13,
"_primary_term": 1,
"found": true,
"_source": {
"survey_id": 15,
"submission_id": 12069,
"account_id": 2,
"answers": [
{
"answer_txt": "happy",
"question_id": 153,
"skipped": false
}
],
"id": "15_12069"
}
}
두 번째 문서는 모든 쿼리 조건을 충족하지 않으므로 변경되지 않습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다