이 튜토리얼은 필드를 문자열로 변환하고 이를 구체화된 뷰에 저장하여 문자열이 아닌 필드에 대해 문자열별 쿼리를 실행하는 방법을 보여줍니다. 구체화된 뷰를 사용하면 문자열 전용 연산자를 사용하여 변환된 필드를 쿼리하고 원본 데이터를 소스 컬렉션에 그대로 유지할 수 있습니다.
이 튜토리얼에서는 다음 단계를 안내합니다:
시작하기 전에 클러스터 필수 구성 요소에 설명된 요구 사항을 충족하는지 확인하세요.
구체화된 뷰 만들기
This section demonstrates how to create a materialized view named airbnb-mat-view on the sample_airbnb.listingsAndReviews collection. This view stores various numeric and date fields from the source collection as string fields.
구체화된 뷰에서 MongoDB Search 인덱스 생성
To run the queries in the Run Queries on the Converted Fields stage of this tutorial, you need to create MongoDB Search indexes on the converted string fields in your airbnb_mat_view materialized view.
The following JSON definitions define MongoDB Search indexes on the airbnb_mat_view materialized view. You can use dynamic or static mappings to specify which fields you want to index in your collection, depending on the type of queries you want to run. To learn more about field mappings or MongoDB Search index syntax, see Define Field Mappings for a MongoDB Search Index or Index Reference, respectively.
다음 JSON MongoDB Search 인덱스 정의는 동적 매핑을 사용하여 구체화된 뷰의 필드를 인덱스 . 이 인덱스 사용하여 queryString 연산자 사용하여 쿼리를 실행 수 있습니다.
동적으로 인덱싱된 필드에 대해서는 자동 완성 연산자 사용하여 쿼리를 실행 수 없습니다.
{ "mappings": { "dynamic": true } }
다음 JSON MongoDB Search 인덱스 정의는 정적 매핑 을 사용하여 구체화된 뷰의 필드를 자동 완성 유형으로 인덱스. 이 인덱스를 사용하여 자동 완성 연산자로 쿼리를 실행할 수 있습니다.
autocomplete 유형으로 인덱싱된 필드에 대해서는 queryString 연산자 사용하여 쿼리를 실행 수 없습니다.
{ "mappings": { "dynamic": false, "fields": { "accommodatesNumber": [ { "dynamic": true, "type": "document" }, { "minGrams": 1, "type": "autocomplete" } ], "lastScrapedDate": [ { "dynamic": true, "type": "document" }, { "type": "autocomplete" } ], "maximumNumberOfNights": [ { "dynamic": true, "type": "document" }, { "minGrams": 1, "type": "autocomplete" } ] } } }
선호하는 인터페이스를 사용하여 위에 정의된 인덱스를 생성하는 방법을 학습하려면 지원되는 클라이언트를 참조하세요.
변환된 필드에서 쿼리 실행
문자열로 변환된 숫자 및 날짜 필드에 대해 쿼리를 실행할 수 있습니다. 이 튜토리얼에서는 queryString 및 자동 완성 연산자를 사용하여 속성을 검색합니다. 이 쿼리는 다음 파이프라인 단계를 사용합니다.
이 섹션에서는 클러스터 에 연결하고 airbnb_mat_view 컬렉션 의 필드에 대해 연산자 사용하여 샘플 쿼리를 실행 .
Query with queryString
The following procedure runs queryString queries against the converted fields. Use the index that you created with dynamic mappings.
Query with autocomplete
The following procedure runs autocomplete queries against the converted fields. Use the index that you created with static mappings.