정의
구문
phrase 의 구문은 다음과 같습니다:
1 { 2 $search: { 3 "index": <index name>, // optional, defaults to "default" 4 "phrase": { 5 "query": "<search-string>", 6 "path": "<field-to-search>", 7 "score": <options>, 8 "slop": <distance-number>, 9 "synonyms": "<synonyms-mapping-name>" 10 } 11 } 12 }
옵션
phrase 는 다음 용어를 사용하여 쿼리를 구성합니다:
필드 | 유형 | 설명 | 필요성 |
|---|---|---|---|
| 문자열 또는 문자열 배열 | 검색할 문자열 또는 문자열을 입력합니다. | 네 |
| 문자열 또는 문자열 배열 | 인덱싱된 필드 또는 검색할 필드. 검색할 와일드카드 경로를 지정할 수도 있습니다. 핵심: 연산자 사용하려면 속성 또는 기본값 인(으)로 설정하다 에서 | 네 |
| integer |
| no |
| 객체 | 일치하는 검색어 결과에 할당할 점수입니다. 다음 옵션을 사용하여 기본 점수를 수정할 수 있습니다.
배열 값을 쿼리할 때 MongoDB Search는 배열 에서 쿼리 와 일치하는 값의 수에 관계없이 동일한 점수를 할당합니다. 기본 점수를 수정하는 옵션에 대한 자세한 내용은 결과에서 문서 점수 매기기를 참조하세요. | no |
| 문자열 | 옵션 |
채점 동작
기본값 으로 구문 연산자 유사성 알고리즘 사용하여쿼리 와의 관련성에 따라 문서에 점수를 매깁니다. bm25
MongoDB Search 인덱스 정의의 필드에 대한 속성 에 다른 유사성 알고리즘 지정하여 사용되는 유사성 알고리즘 변경할 수 있습니다.similarity.type string string 유형에 대한 MongoDB Search 인덱스 구성하는 방법을 학습 문자열 필드를 인덱싱하는 방법을 참조하세요.
지원되는 유사성 알고리즘에 대해 자세히 학습 점수 세부 정보를 참조하세요.
예시
이 페이지의 예제에서는 sample_mflix 데이터베이스 의 movies 컬렉션 사용합니다. 샘플 데이터 세트를 클러스터 에 로드한 후 동적 매핑을 사용하여 MongoDB Search 인덱스 만들고 클러스터에서 예시 쿼리를 실행. 동의어 예제를 시도하려면 sample_mflix 데이터베이스 에 동의어 컬렉션 추가한 다음 synonyms 매핑 컬렉션 으로 인덱스 정의해야 합니다.
단일 문구 예시
다음 MongoDB Search 예시 title 필드 에서 쿼리 문자열 new york에 대한 기본 검색 수행합니다. 쿼리 에 slop 가 없으므로 slop 값의 기본값은 0이며, 이는 단어의 위치가 쿼리 문자열과 정확히 일치해야 결과에 포함될 수 있음을 의미합니다. 쿼리 다음도 포함됩니다.
기본 예시
다음 쿼리 검색 기준과 일치하는 문서를 반환합니다.
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": "new york" 7 } 8 } 9 }, 10 { $limit: 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'New York, New York', score: 6.786321640014648 } 3 { title: 'New York', score: 6.258549213409424 } 4 { title: 'New York Stories', score: 5.3813982009887695 } 5 { title: 'New York Minute', score: 5.3813982009887695 } 6 { title: 'Synecdoche, New York', score: 5.3813982009887695 } 7 { title: 'New York Doll', score: 5.3813982009887695 } 8 { title: 'Little New York', score: 5.3813982009887695 } 9 { title: 'Escape from New York', score: 4.719893455505371 } 10 { title: 'Naked in New York', score: 4.719893455505371 } 11 { title: 'Autumn in New York', score: 4.719893455505371 } 12 ]
메타데이터 예시
다음 쿼리는 검색 기준에 대한 메타데이터 결과를 반환합니다. 다음 버킷(연도)에서 검색 기준에 맞는 영화의 수를 얻기 위해 $searchMeta 단계를 사용합니다.
2000, 이 버킷에 대한 포괄적인 하한값
2005, 2000 버킷의 상한 제외 및 이 버킷의 하한 포함
2010, 2005 버킷의 상한 제외 및 이 버킷의 하한 포함
2010, 2010 버킷의 배타적 상한선
예시
1 db.movies.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "phrase": { 7 "path": "title", 8 "query": "new york" 9 } 10 }, 11 "facets": { 12 "yearFacet": { 13 "type": "number", 14 "path": "year", 15 "boundaries": [2000, 2005, 2010, 2015 ] 16 } 17 } 18 } 19 } 20 } 21 ])
1 [ 2 { 3 count: { lowerBound: Long('27') }, 4 facet: { 5 yearFacet: { 6 buckets: [ 7 { _id: 2000, count: Long('3') }, 8 { _id: 2005, count: Long('8') }, 9 { _id: 2010, count: Long('4') } 10 ] 11 } 12 } 13 } 14 ]
결과는 2000과 2005 사이에 3개의 영화, 2005와 2010 사이에 8개의 2010 영화, 2010과 2015사이에 4개의 영화가 title에 new york이라는 용어를 포함하고 있음을 보여줍니다.
여러 구문 예시
다음 MongoDB Search 예시 title 필드 에서 쿼리 문자열 the man 및 the
moon에 대한 기본 검색 수행합니다. 쿼리 에 slop 이 없으므로 slop 값의 기본값은 0이며, 이는 단어의 위치가 쿼리 문자열과 정확히 일치해야 결과에 포함될 수 있음을 의미합니다. 쿼리 다음도 포함됩니다.
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": ["the man", "the moon"] 7 } 8 } 9 }, 10 { $limit: 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'The Man in the Moon', score: 4.4830474853515625 }, 3 { title: 'Shoot the Moon', score: 3.252699851989746 }, 4 { title: 'Kick the Moon', score: 3.252699851989746 }, 5 { title: 'The Man', score: 2.8923356533050537 }, 6 { title: 'The Moon and Sixpence', score: 2.8528637886047363 }, 7 { title: 'The Moon Is Blue', score: 2.8528637886047363 }, 8 { title: 'Racing with the Moon', score: 2.8528637886047363 }, 9 { title: 'Mountains of the Moon', score: 2.8528637886047363 }, 10 { title: 'Man on the Moon', score: 2.8528637886047363 }, 11 { title: 'Castaway on the Moon', score: 2.8528637886047363 } 12 ]
Slop 예시
다음 MongoDB Search 예시 title 필드 에서 쿼리 문자열 men women를 검색 합니다. query 에서 slop 값이 5 이면 단어의 이동과 단어 men 와 women 사이의 거리가 더 많이 허용됩니다. 쿼리 다음을 수행할 수 있는 $project 단계가 포함되어 있습니다.
다음을 제외한 모든 필드 제외
title다음과 같은 이름의 필드를 추가합니다.
score
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": "men women", 7 "slop": 5 8 } 9 } 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'Men Without Women', score: 3.367523193359375 }, 3 { title: 'Men Vs Women', score: 3.367523193359375 }, 4 { title: 'Good Men, Good Women', score: 2.8529787063598633 }, 5 { title: 'The War Between Men and Women', score: 2.1851978302001953 }, 6 { title: 'Women Without Men', score: 1.9656763076782227 }, 7 { title: 'Women Vs Men', score: 1.9656763076782227 } 8 ]
동의어 예시
다음 쿼리는 movies 컬렉션 의 plot 필드 에서 지정된 쿼리 문자열의 용어를 검색 . MongoDB Search는 컬렉션 에 대한 인덱스 의synonymous_terms 동의어 매핑 정의에 지정된 동의어 소스 컬렉션 의 매핑 유형에 따라 결과를 sample_mflix.movies 반환합니다.
다음 쿼리는 plot 필드에서 용어 사이의 거리가 최대 5 이내인 automobile race 구문을 검색합니다.
db.movies.aggregate([ { $search: { "phrase": { "path": "plot", "query": "automobile race", "slop": 5, "synonyms": "my_synonyms" } } }, { "$limit": 5 }, { $project: { "_id": 0, "plot": 1, "title": 1, score: { $meta: "searchScore" } } } ])
[ { plot: 'When a popular daredevil proposes an automobile race across three continents, his arch rival vows to beat him, while an ambitious female reporter has her own plans for victory.', title: 'The Great Race', score: 29.569732666015625 }, { plot: 'A wide variety of eccentric competitors participate in a wild and illegal cross-country car race.', title: 'The Cannonball Run', score: 25.50379180908203 }, { plot: 'A mechanic takes his family to a car race and a series of events occur which brings problems, betrayals, violence and the unexpected death of an elderly person.', title: 'National Mechanics', score: 21.538257598876953 }, { plot: "It's time for the annual London to Brighton antique car rally, and Alan McKim and Ambrose Claverhouse are not going to let their friendship stop them from trying to humiliate each other. ...", title: 'Genevieve', score: 20.19266128540039 }, { plot: "A naive drifter runs away from his army father in hopes of making it on the car racing circuit. In Las Vegas, he meets a young scam artist, who develops a crush on him. He is then ...", title: 'Speedway Junky', score: 18.639965057373047 } ]
결과의 문서들은 plot 필드에 다음 텀을 포함하고 있으며, 텀 간의 거리는 최대 5까지 허용됩니다.
automobile,car또는vehicle, 즉synonymous_terms컬렉션에서equivalent동의어로 정의됩니다.race,contest또는rally, 즉 sy``synonymous_terms`` 컬렉션에서explicit동의어로 정의됩니다.
automobile, car 및 vehicle 를 equivalent 동의어로 구성했기 때문에 MongoDB Search는 car race 또는 vehicle
race 에 대한 검색 에 대해 유사한 결과를 반환합니다. 그러나 automobile
contest 의 결과에는 race 또는 rally 이 있는 문서가 포함되지 않고 automobile rally 의 결과에는 contest 를 구성하지 않았기 때문에 race 또는 contest 이 있는 문서가 포함되지 않습니다. 또는 rally 을(를) 용어의 동의어로 사용합니다.