near 연산자 제한 사항
MongoDB Search 인덱스 있더라도 near 연산자 사용하여 배열 에 저장된 숫자 또는 날짜 값을 쿼리 할 수 없습니다. 범위 배열 내의 인덱싱된 숫자 또는 날짜 값을 쿼리 데만 사용할 수 있습니다.
정의
nearnear연산자는 숫자, 날짜 및 GeoJSON 포인트 값을 쿼리하고 점수를 매기는 기능을 지원합니다. 이 연산자를 사용하여 검색을 수행할 수 있습니다:BSON
int32,int64및double데이터 유형의 숫자 필드.ISODate 형식의 BSON
date유형의 날짜 필드입니다.위도 및 경도 좌표를 사용하여 정의된 지리적 위치 필드입니다.
near연산자 사용하여 숫자나 날짜에 가까운 결과를 찾을 수 있습니다.near연산자 숫자 또는 날짜에 대한 근접성을 기준으로 MongoDB 검색 결과에 점수를 매깁니다.
구문
near 의 구문은 다음과 같습니다:
{ $search: { "index": <index name>, // optional, defaults to "default" "near": { "path": "<field-to-search>", "origin": <date-or-number>, "pivot": <pivot-distance>, "score": <score-options> } } }
옵션
near 는 다음 용어를 사용하여 쿼리를 구성합니다:
필드 | 유형 | 설명 | 필요성 | |||
|---|---|---|---|---|---|---|
| 날짜, 번호, 또는 지역 | 근처에서 검색할 번호, 날짜 또는 지리적 점입니다. 결과의 근접성이 측정되는 원점입니다.
| 네 | |||
| 문자열 또는 문자열 배열 | 인덱싱된 필드 또는 검색할 필드. | 네 | |||
| 숫자 | MongoDB Search 결과 문서의 점수를 계산하는 데 사용하는 값입니다. 점수는 다음 공식을 사용하여 계산됩니다. 여기서 인덱스 필드 값이
| 네 | |||
| 객체 | 일치하는 검색어 결과에 할당할 점수입니다. 다음 옵션을 사용하여 기본 점수를 수정할 수 있습니다.
쿼리에서 | no |
채점 동작
MongoDB Search score 은(는) MongoDB Search 결과가 origin에 얼마나 근접한지를 측정한 값입니다. score 는 0 에서 1 사이로 조정되며, 1 는 정확히 일치하는 일치 항목이고 0 는 원거리 일치 항목입니다. origin 에서의 MongoDB Search 결과의 거리가 pivot를 사용하여 계산한 원점으로부터의 거리와 같을 때 점수는 0.5 입니다.
near 연산자는 다음 거리 감소 함수를 사용하여 문서 점수를 계산합니다.
pivot score = ------------------ pivot + distance
여기서 각 텀 다음과 같이 정의됩니다.
요인 | 설명 | |
|---|---|---|
|
| |
|
where:
|
쿼리에서 score 옵션을 사용하여 기본 점수를 수정할 수 있습니다. 옵션에 대해 자세히 알아보려면 점수 수정을 참조하세요.
예시
The number and date examples use the movies collection in the sample_mflix database. The GeoJSON point example uses the listingsAndReviews collection in the sample_airbnb database.
If you load the sample data on your cluster, you can create the static indexes using the index definitions in the examples below or the dynamic index and run the example queries on your cluster.
팁
If you've already loaded the sample dataset, refer to the MongoDB Search Quick Start tutorial to create an index definition and run MongoDB Search queries.
번호 예시
다음 예에서는 near 연산자를 사용하여 숫자 필드를 쿼리합니다. 다음 쿼리들은 movies 컬렉션의 모든 필드를 동적으로 인덱싱하는 runtimes이라는 인덱스를 사용합니다. 이 쿼리는 movies 컬렉션에서 runtime 필드 값이 279에 가까운 문서를 검색합니다.
기본 예시
다음 쿼리 검색 기준과 일치하는 모든 문서를 반환합니다. 여기에는 출력을 7 개의 결과로 제한하는 $limit 단계와 $project 단계가 포함되어 있습니다.
title및runtime을 제외한 모든 필드를 제외합니다.다음과 같은 이름의 필드를 추가합니다.
score
score는 pivot를 사용하여 계산됩니다.
1 db.movies.aggregate([ 2 { 3 $search: { 4 "index": "runtimes", 5 "near": { 6 "path": "year", 7 "origin": 2000, 8 "pivot": 2 9 } 10 } 11 }, 12 { 13 $limit: 7 14 }, 15 { 16 $project: { 17 "_id": 0, 18 "title": 1, 19 "runtime": 1, 20 score: { $meta: "searchScore" } 21 } 22 } 23 ])
[ { runtime: 279, title: 'The Kingdom', score: 1 }, { runtime: 279, title: 'The Kingdom', score: 1 }, { runtime: 279, title: 'The Jinx: The Life and Deaths of Robert Durst', score: 1 }, { runtime: 281, title: 'Les Misèrables', score: 0.5 }, { runtime: 277, title: 'Tokyo Trial', score: 0.5 }, { runtime: 283, title: 'Scenes from a Marriage', score: 0.3333333432674408 }, { runtime: 274, title: 'The Crimson Petal and the White', score: 0.2857142984867096 } ]
MongoDB Search 결과에서 영화 The Kingdom 와 The Jinx: The Life and Deaths of Robert Durst 는 runtime 필드 값 279 가 정확히 일치하기 때문에 1.0 점을 받습니다. 영화 Les Misèrables 와 Tokyo Trial 는 runtime 필드 값이 279에서 2 단위 떨어져 있기 때문에 0.5 점을 받습니다.
메타데이터 예시
다음 쿼리는 검색 기준에 대한 메타데이터 결과를 반환합니다. 즉, $searchMeta 단계를 사용하여 검색 기준과 일치하는 다음 버킷(연도)의 영화 수를 가져옵니다.
2000, 이 버킷에 대한 포괄적인 하한값
2005, 2000 버킷의 상한 제외 및 이 버킷의 하한 포함
2010, 2005 버킷의 상한 제외 및 이 버킷의 하한 포함
2010, 2010 버킷의 배타적 상한선
1 db.movies.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "near": { 7 "path": "runtime", 8 "origin": 279, 9 "pivot": 2 10 } 11 }, 12 "facets": { 13 "yearFacet": { 14 "type": "number", 15 "path": "year", 16 "boundaries": [2000, 2005, 2010, 2015 ] 17 } 18 } 19 } 20 } 21 } 22 ])
[ { count: { lowerBound: Long('20910') }, facet: { yearFacet: { buckets: [ { _id: 2000, count: Long('3058') }, { _id: 2005, count: Long('4012') }, { _id: 2010, count: Long('4669') } ] } } } ]
날짜 예시
다음 예에서는 near 연산자를 사용하여 날짜 필드를 쿼리합니다.
예시
releaseddate라고 명명된 다음 인덱스 정의는 movies 컬렉션에서만 released 필드의 인덱스를 생성합니다.
1 { 2 "mappings": { 3 "dynamic": false, 4 "fields": { 5 "released": { 6 "type": "date" 7 } 8 } 9 } 10 }
다음 쿼리는 13, 1 9월 근처에 개봉된 영화를 검색합니다. 여기에는 출력 결과를 3 개로 제한하는 $limit 단계와 $project 단계가 포함되어 있습니다.
title및released을 제외한 모든 필드를 제외합니다.다음과 같은 이름의 필드를 추가합니다.
score
결과의 score는 pivot을 사용하여 계산됩니다.
참고
pivot 여기서는 밀리초 단위로 측정되며, 7,776,000,000 ms는 약 3개월에 해당합니다.
1 db.movies.aggregate([ 2 { 3 $search: { 4 "index": "releaseddate", 5 "near": { 6 "path": "released", 7 "origin": ISODate("1915-09-13T00:00:00.000+00:00"), 8 "pivot": 7776000000 9 } 10 } 11 }, 12 { 13 $limit: 3 14 }, 15 { 16 $project: { 17 "_id": 0, 18 "title": 1, 19 "released": 1, 20 score: { $meta: "searchScore" } 21 } 22 } 23 ])
위의 쿼리는 다음과 같은 검색 결과를 반환합니다.
{ "title" : "Regeneration", "released" : ISODate("1915-09-13T00:00:00Z"), "score" : 1 } { "title" : "The Cheat", "released" : ISODate("1915-12-13T00:00:00Z"), "score" : 0.49723756313323975 } { "title" : "Hell's Hinges", "released" : ISODate("1916-03-05T00:00:00Z"), "score" : 0.34090909361839294 }
위의 MongoDB Search 결과에서 1915-09-13 의 released 필드 값이 정확히 일치하기 때문에 영화 Regeneration 는 1 점을 받습니다. 1915-12-13에 개봉된 영화 The Cheat는 origin 로부터의 released 필드 값 거리가 1915-09-13로부터 약 7,776,000,000 밀리초이기 때문에 약 0.5 점을 받습니다.
GeoJSON 포인트 예시
다음 예에서는 near 연산자를 사용하여 sample_airbnb.listingsAndReviews 컬렉션의 GeoJSON 점 객체를 쿼리합니다. 다음 인덱스 정의는 listingsAndReviews 컬렉션의 address.location 및 property_type 필드를 인덱싱합니다.
예시
1 { 2 "mappings": { 3 "fields": { 4 "address": { 5 "fields": { 6 "location": { 7 "type": "geo" 8 } 9 }, 10 "type": "document" 11 }, 12 "property_type": { 13 "type": "string" 14 } 15 } 16 } 17 }
기본 예시
다음 예에서는 near 연산자를 사용하여 sample_airbnb.listingsAndReviews 컬렉션의 address.location 필드를 쿼리합니다.
예시
The following query searches for properties in Portugal. It includes a $limit stage to limit the output to 3 results and a $project stage to:
name및address을 제외한 모든 필드를 제외합니다.다음과 같은 이름의 필드를 추가합니다.
score
결과의 score는 pivot을 사용하여 계산됩니다. 여기서 pivot은 미터 단위로 측정되며 1000미터는 1킬로미터에 해당합니다.
1 db.listingsAndReviews.aggregate([ 2 { 3 "$search": { 4 "near": { 5 "origin": { 6 "type": "Point", 7 "coordinates": [-8.61308, 41.1413] 8 }, 9 "pivot": 1000, 10 "path": "address.location" 11 } 12 } 13 }, 14 { 15 $limit: 3 16 }, 17 { 18 $project: { 19 "_id": 0, 20 "name": 1, 21 "address": 1, 22 score: { $meta: "searchScore" } 23 } 24 } 25 ])
위의 쿼리는 다음과 같은 검색 결과를 반환합니다.
1 { 2 "name" : "Ribeira Charming Duplex", 3 "address" : { 4 "street" : "Porto, Porto, Portugal", 5 "suburb" : "", 6 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória", 7 "market" : "Porto", 8 "country" : "Portugal", 9 "country_code" : "PT", 10 "location" : { 11 "type" : "Point", 12 "coordinates" : [ -8.61308, 41.1413 ], 13 "is_location_exact" : false 14 } 15 }, 16 "score" : 1 17 } 18 { 19 "name" : "DB RIBEIRA - Grey Apartment", 20 "address" : { 21 "street" : "Porto, Porto, Portugal", 22 "suburb" : "", 23 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória", 24 "market" : "Porto", 25 "country" : "Portugal", 26 "country_code" : "PT", 27 "location" : { 28 "type" : "Point", 29 "coordinates" : [ -8.61294, 41.14126 ], 30 "is_location_exact" : true 31 } 32 }, 33 "score" : 0.9876177310943604 34 } 35 { 36 "name" : "Ribeira 24 (4)", 37 "address" : { 38 "street" : "Porto, Porto, Portugal", 39 "suburb" : "", 40 "government_area" : "Cedofeita, Ildefonso, Sé, Miragaia, Nicolau, Vitória", 41 "market" : "Porto", 42 "country" : "Portugal", 43 "country_code" : "PT", 44 "location" : { 45 "type" : "Point", 46 "coordinates" : [ -8.61318, 41.14107 ], 47 "is_location_exact" : false 48 } 49 }, 50 "score" : 0.973789632320404 51 }
결과는 지정된 좌표에서 더 멀리 있는 속성의 점수가 낮다는 것을 보여줍니다.
컴파운드 예시
다음 예에서는 compound 연산자를 사용하여 sample_airbnb.listingsAndReviews 컬렉션의 property_type 및 address.location 필드를 쿼리합니다.
예시
The following query searches for apartments in Hong Kong near a specified GeoJSON point. The query uses must to specify the search condition, which must be met, and should to specify preference for location. It includes a $limit stage to limit the output to 3 results and a $project stage to:
property_type및address을 제외한 모든 필드를 제외합니다.다음과 같은 이름의 필드를 추가합니다.
score
score는 pivot을 사용하여 계산됩니다. 여기서 pivot은 미터 단위로 측정되며 1000미터는 1킬로미터에 해당합니다.
1 db.listingsAndReviews.aggregate([ 2 { 3 $search: { 4 "compound": { 5 "must": { 6 "text": { 7 "query": "Apartment", 8 "path": "property_type" 9 } 10 }, 11 "should": { 12 "near": { 13 "origin": { 14 "type": "Point", 15 "coordinates": [114.15027, 22.28158] 16 }, 17 "pivot": 1000, 18 "path": "address.location" 19 } 20 } 21 } 22 } 23 }, 24 { 25 $limit: 3 26 }, 27 { 28 $project: { 29 "_id": 0, 30 "property_type": 1, 31 "address": 1, 32 score: { $meta: "searchScore" } 33 } 34 } 35 ])
위의 쿼리는 다음과 같은 검색 결과를 반환합니다.
1 { 2 "property_type" : "Apartment", 3 "address" : { 4 "street" : "Hong Kong, Hong Kong Island, Hong Kong", 5 "suburb" : "Central & Western District", 6 "government_area" : "Central & Western", 7 "market" : "Hong Kong", 8 "country" : "Hong Kong", 9 "country_code" : "HK", 10 "location" : { 11 "type" : "Point", 12 "coordinates" : [ 114.15027, 22.28158 ], 13 "is_location_exact" : true 14 } 15 }, 16 "score" : 1.177286982536316 17 } 18 { 19 "property_type" : "Apartment", 20 "address" : { 21 "street" : "Hong Kong, Hong Kong Island, Hong Kong", 22 "suburb" : "Central & Western District", 23 "government_area" : "Central & Western", 24 "market" : "Hong Kong", 25 "country" : "Hong Kong", 26 "country_code" : "HK", 27 "location" : { 28 "type" : "Point", 29 "coordinates" : [ 114.15082, 22.28161 ], 30 "is_location_exact" : true 31 } 32 }, 33 "score" : 1.1236450672149658 34 } 35 { 36 "property_type" : "Apartment", 37 "address" : { 38 "street" : "Hong Kong, 39 Hong Kong Island, Hong Kong", 40 "suburb" : "Mid-Levels", 41 "government_area" : "Central & Western", 42 "market" : "Hong Kong", 43 "country" : "Hong Kong", 44 "country_code" : "HK", 45 "location" : { 46 "type" : "Point", 47 "coordinates" : [ 114.15007, 22.28215 ], 48 "is_location_exact" : true 49 } 50 }, 51 "score" : 1.114811897277832 52 }