1. Cloudant가 제공하는 Index 방식
1
• Primary Index
• 각 Document를 식별할 수 있는 고유한 ID(RDB의 Primary Key)에 대한 Index
• 기본적으로 생성 됨
• Secondary Index란?
• ID 이외의 다른 속성에 대한 Index
• RDB의 경우 Primary Key가 아닌 필드에 대해 CREATE INDEX 명령으로 생성
• 일반적인 NoSQL DB에서는 Primary Index만 제공
• Search Index란?
• Full Text 또는 Keyword 검색 (예: „서울‟ AND „미술관‟)
• Naver/Google 등에서 제공하는 검색 방식
2. Secondary Index
2
• _id를 기준으로 한 Primary Unique Index를 보완
• _id 이외의 다른 필드 또는 필드들의 조합을 대상으로 함
• Key 값은 중복 허용
• 예: author, subject, [author, subject]
• 생성 방법
• 일반 Design Document
• Map/Reduce를 사용 (Map의 emit 함수)
데이터
Map 함수
인덱스
조회 결과
3. View – Secondary Index의 결과
3
• Map의 emit 함수 실행 결과가 실시간 처리 되어 View 형태로 저장
(원본 DB의 별도)
• 원본 데이터 변경 시 전체 재 계산이 아닌 변경분만 재 계산 (Incremental
Map/Reduce)
• 클러스터(Cloudant, BigCouch)에서는 여러 노드에 분산되어 처리 및
저장 됨
• 동일 데이터(Database)에 대한 복수 View 생성 가능
• Reduce를 통해 데이터 통계(RDB의 sum & group by에 해당) 생성
(사용 여부는 선택적)
4. Map/Reduce
4
• 주로 JavaScript로 작성 되며, 다른 언어도 지원 가능
• 일반적인 Map/Reduce와 마찬가지로…
• Map 함수에서 key, value 쌍을 생성
• Reduce 함수에서 데이터(key, value 쌍의 배열)에 대한 계산, 통계 작업
• 병렬 처리 – 클러스터에서는 여러 노드에서 분산 처리 및 결과 취합
5. Map/Reduce
5
• Incremental Map/Reduce
• Real time: DB의 데이터에 변경이 생기면 바로 실시간 재 계산 (View)
• Incremental: 데이터 변경 시, 전체를 재 계산 하지 않고 필요한 부분만 재 계산
하는 방법
• http://eagain.net/articles/incremental-mapreduce/
• Chained Map/Reduce
• 복수의 Map/Reduce 작업을 이어서 하는 것 (Real time, Incremental 특성 유지)
• Map/Reduce 결과를 2차 DB로 동기화하여 거기서 다시 Map/Reduce를 실행
• http://examples.cloudant.com/sales/_design/sales/index.html
6. Search Index
6
• 일상적인 언어 습관에 가까운 검색 방식
• 키워드 검색을 위한 내장 index 함수를 이용하여 인덱스를 생성
• Designed Document에 저장되는 index 함수 예제
• Index(param1, param2, param3)
• param1: Apache Lucene 필드
• param2: 전문 인덱스 데이터
• param3: 옵션, JSON
• {"store": "yes", "index": "analyzed_no_norms"}
• Store: 검색 결과 포함 여부
• Index: 인덱싱 방식
function(doc){
index("name", doc.name, {"store": "yes", "index": "analyzed_no_norms"});
}
7. Search API: index 작성 방법 예제
7
function(doc){
index("default", doc._id);
if(doc.min_length){
index("min_length", doc.min_length, {"store": "yes"});
}
if(doc.diet){
index("diet", doc.diet, {"store": "yes"});
}
if (doc.latin_name){
index("latin_name", doc.latin_name, {"store": "yes"});
}
if (doc.class){
index("class", doc.class, {"store": "yes"});
}
}
8. Search API
8
• 형식
http://<userid>.cloudant.com/<database>/_design/<design_document>/_search/<in
dex_name>
• 매뉴얼: http://docs.cloudant.com/api/search.html
• Query String
• Parameter
• q: 검색 쿼리
• limit: 검색 결과의 최대 허용 문서
• include_docs: 검색 결과 문서에 원본 문서를 모두 추가할 것인가를 설정
• Stale: 성능 튜닝 포인트, 최신의 인덱스를 적용해야 함을 강제함
• sort: 정렬 컬럼 및 오름차순 내림차순 설정,
• Sort=“age” 나이 컬럼 오름차순
• Sort=“-age” 나이 컬럼 내림차순
• ort=["-age", "height"] 나이 컬럼 내림차순, 키 컬럼 오름차순
9. Search API 기본 문법 (Lucene 문법)
9
• 검색 쿼리는 q 파라미터에 설정
• 예제
https://[username].cloudant.com/animaldb/_design/views101/_search/animals?q=class:bird
• 매뉴얼: http://lucene.apache.org/core/3_6_0/queryparsersyntax.html
• 논리연산자: AND, +, OR, NOT, - (Apache Lucene 문법 지원)
• 문자열 검색
• class:bird class 컬럼에 bird 문자열인 모든 문서
• class:bird* class 컬럼에 bird 문자열을 포함하는 모든 문서
• l*: 모든 컬럼 중 L로 시작하는 단어를 포함하는 모든 문서
• 숫자&날짜 검색
• min_number:[1 TO 3] min_number 컬럼의 범위가 1~3인 모든 문서
• min_number:[1 TO Infinity] min_number 컬럼의 범위가 1이상인 모든 문서
• min_number:[-Infinity TO 1] min_number 컬럼의 범위가 3이하인 모든 문서