2012-11-24 5 views
1

좋아요 SO 사용자. CouchDB를 배우고 사용하려고합니다. 나는 XML 파일에서 한 줄에 문서로로드 StackExchange 데이터 내보내기, 그래서 소파의 문서는 다음과 같이 기본적으로 보이는 :couchdb에서 집계 문서를 작성하는 가장 좋은 방법은 무엇입니까?

//This is a representation of a question: 
{ 
"Id" : "1", 
"PostTypeId" : "1", 
"Body" : "..." 
} 

//This is a representation of an answer 
{ 
"Id" : "1234", 
"ParentId" : "1", 
"PostTypeId" : "2" 
"Body" : "..." 
} 

(이 문서의 수입은 기본적으로 모든 속성을 처리한다는 사실을 무시하십시오 텍스트가 나는 등 실수, bools을 사용하는 것이 더 나은 공간/처리 효율을 얻을 수 있다는 것을 이해) 내가하고 싶은 무엇

는 하나의 집합 문서에이지도하는 것입니다.

가 여기 내지도의 :

function(doc) { 
    if(doc.PostTypeId === "2"){ 
     emit(doc.ParentId, doc); 
    } 
    else{ 
     emit(doc.Id, doc); 
    } 
} 

그리고 여기입니다 줄이거 :

function(keys, values, rereduce){ 
    var retval = {question: null, answers : []}; 

    if(rereduce){ 
     for(var i in values){ 
      var current = values[i]; 
      retval.answers = retval.answers.concat(current.answers); 
      if(retval.question === null && current.question !== null){ 
       retval.question = current.question; 
      } 
     } 
    } 
    else{ 
     for(var i in values){ 
      var current = values[i];    

      if(current.PostTypeId === "2"){ 
       retval.push(current); 
      } 
      else{ 
       retval.question = current; 
      } 
     } 
    } 
    return retval; 
} 

이론적으로는이 같은 문서를 얻을 것이다 :

{ 
    "question" : {...}, 
    "answers" : [answer1, answer2, answer3] 
} 

을하지만 그 대신 내가 표준 오류 "충분히 빠르게 감소하지 않는다"얻고있다.

Map-Reduce를 잘못 사용하고 있습니까? CouchDb에서이를 수행하는 방법에 대한 잘 정립 된 패턴이 있습니까?

(또한 내가 질문은 "부모"및 답변 만이 아니라 IDS은 "어린이"입니다입니다 전체 문서와 응답을하려는 있습니다.)

답변

2

그래서, " 내가 뭘 하려는지를 올바르게 수행하는 방법은 내 디자인 문서의 일부로 "목록"을 추가하는 것입니다. (그리고 내가 성취하고자하는 목표는 "문서 대조"라고 불리우는 것 같습니다).

어쨌든지도를 원하는대로 구성하고 동일한 기능의 "목록"과 결합 할 수 있습니다.

내가 내 감소 (단지이지도 기능) 제거, 위의 문제를 해결하기 위해, 다음, 다음과 같은 기능 추가 : 그래서,

{ 
    "_id": "_design/posts", 
    "_rev": "11-8103b7f3bd2552a19704710058113b32", 
    "language": "javascript", 
    "views": { 
     "by_question_id": { 
      "map": "function(doc) { 
       if(doc.PostTypeId === \"2\"){ 
        emit(doc.ParentId, doc); 
       } 
       else{ 
        emit(doc.Id, doc); 
       } 
      }" 
     } 
    }, 
    "lists": { 
     "aggregated": "function(head, req){ 
         start({\"headers\": {\"Content-Type\": \"text/json\"}}); 
         var currentRow = null; 
         var currentObj = null; 
         var retval = []; 
         while(currentRow = getRow()){ 
          if(currentObj === null || currentRow.key !== currentObj.key){ 
           currentObj = {key: currentRow.key, question : null, answers : []}; 
           retval.push(currentObj); 
          } 
          if(currentRow.value.PostTypeId === \"2\"){ 
           currentObj.answers.push(currentRow.value); 
          } 
          else{ 
           currentObj.question = currentRow.value; 
          } 
         } 
         send(toJSON(retval)); 
        }" 
    } 
} 

을 당신이로드 일부 요소를 한 후, 당신은 할 수 있습니다 다음과 같이 액세스하십시오.

http://localhost:5984/<db>/_design/posts/_list/aggregated/by_question_id?<standard view limiters> 

이 기능을 사용하면 시간을 절약 할 수 있기를 바랍니다.

관련 문제