문서 메뉴

문서 홈MongoDB 실행 및 관리MongoDB Atlas

쿼리 경로 구축

이 페이지의 내용

  • 개요
  • 사용법
  • 예제
  • 단일 필드 검색
  • 다중 필드 검색
  • 대체 분석기 검색
  • 중첩된 필드 검색
  • 와일드카드 필드 검색

path 매개변수는 Atlas Search 연산자가 검색할 필드를 지정하는 데 사용됩니다. 여기에는 다음이 포함될 수 있습니다.

  • 문자열

  • 문자열 배열

  • 멀티 분석기 사양

  • 문자열과 다중 분석기 사양의 조합이 포함된 배열입니다.

참고

모든 연산자가 다양한 유형의 경로를 모두 사용할 수 있는 것은 아닙니다. 지원하는 경로 유형에 대한 자세한 내용은 각 개별 연산자에 대한 문서를 참조하세요.

인덱싱된 필드 하나만 검색하려면 path 매개변수에 따옴표가 붙은 문자열을 사용하세요. 다음 예시에서는 description이라는 필드를 검색합니다.

"path": "description"

인덱싱된 필드를 여러 개 검색하려면 path 매개변수에 인용된 문자열 배열을 사용하세요. 지정된 필드 중 하나와 일치하는 문서가 결과 집합에 포함됩니다. 다음 예에서는 descriptiontype 필드를 검색합니다.

"path": [ "description", "type" ]

참고

multi경로 옵션은 문자열 유형의 필드에만 사용할 수 있습니다.

인덱스 정의에 여러 분석기가 있는 필드가 포함된 경우 사용할 분석기를 지정할 수 있습니다. path 매개변수는 다음 필드가 있는 객체를 취할 수 있습니다.

필드
설명
value
검색할 필드의 이름입니다.
multi
인덱스 정의의 multi 객체에 지정된 대체 분석기의 이름입니다. 자세한 내용은 다중 분석기를 참조하세요.
wildcard

중첩된 필드를 포함하여 검색할 필드 이름의 모든 문자와 일치하는 와일드카드 문자 *가 포함된 객체입니다. 와일드카드 경로:

  • 객체로 정의되어야 합니다.

  • value 또는 multi 옵션을 포함할 수 없습니다.

  • **과 같은 연속된 와일드카드 문자를 여러 개 포함할 수 없습니다.

와일드카드 경로는 다음 연산자에서만 사용할 수 있습니다.

와일드카드 경로도 강조 표시할 수 있습니다.

다음 인덱스 정의에서 namesnotes라는 이름의 필드는 표준 분석기를 사용합니다. comments라는 이름의 필드는 기본 분석기로 standard를 사용하며, lucene.whitespace 분석기를 사용하는 mySecondaryAnalyzer라는 이름의 multi도 지정합니다.

{
"mappings": {
"dynamic": false,
"fields": {
"names": {
"type": "string",
"analyzer": "lucene.standard"
},
"notes": {
"type": "string",
"analyzer": "lucene.standard"
},
"comments": {
"type": "string",
"analyzer": "lucene.standard",
"multi": {
"mySecondaryAnalyzer": {
"analyzer": "lucene.whitespace",
"type": "string"
}
}
}
}
}
}

다음 path 예시에서는 인덱스 정의에서 mySecondaryAnalyzer라는 multi를 사용하여 comments 필드를 검색합니다.

"path": { "value": "comments", "multi": "mySecondaryAnalyzer" }

인덱싱된 필드와 여러 분석기가 있는 필드의 조합을 검색하려면 배열을 사용합니다. 다음 예시에서는 기본 분석기를 사용하여 names, notes 필드를 검색하고, 인덱스 정의에서 mySecondaryAnalyzer라는 multi를 사용하여 comments 필드를 검색합니다.

"path": [ "names", "notes", { "value": "comments", "multi": "mySecondaryAnalyzer" } ]

다음 path 예시에서는 인덱스 정의에서 mySecondaryAnalyzer라는 multi를 사용하여 문자 n 뒤에 임의의 문자가 있는 모든 필드와 comments 필드를 검색합니다.

"path": [{ "wildcard": "n*" }, { "value": "comments", "multi": "mySecondaryAnalyzer" }]

다음 예시에서는 다음 문서가 있는 cars라는 collection을 사용합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Blue four-door sedan, lots of trunk space. Three
to four passengers."
}
{
"_id" : 2,
"type" : "coupe",
"make" : "BMW",
"description" : "Red two-door convertible, driver's-side airbag."
}
{
"_id" : 3,
"type" : "SUV",
"make" : "Ford",
"description" : "Black four-door SUV, three rows of seats."
}

정적 필드 매핑을 사용하면 collection 내의 개별 필드를 인덱싱하고 검색하는 방법을 지정할 수 있습니다.

cars collection에 대한 인덱스 정의는 다음과 같습니다.

{
"mappings": {
"dynamic": false,
"fields": {
"make": {
"type": "string",
"analyzer": "lucene.standard"
},
"description": {
"type": "string",
"analyzer": "lucene.standard",
"multi": {
"simpleAnalyzer": {
"analyzer": "lucene.simple",
"type": "string"
}
}
}
}
}
}

위의 인덱스 정의는 make 필드를 표준 분석기로 인덱싱하도록 지정합니다. description 필드는 기본적으로 standard 분석기를 사용하지만, multi 매개변수와 함께 simpleAnalyzer를 지정하여 간단한 분석기를 사용할 수도 있습니다.

다음은 make 필드에서 Ford 문자열을 검색하는 예시입니다.

db.cars.aggregate([
{
$search: {
"text": {
"query": "Ford",
"path": "make"
}
}
}
])

위 예시에서는 _id: 3이 포함된 문서가 반환됩니다.

다음 예시에서는 path 매개변수의 필드 배열을 사용하여 make 또는 description 필드에서 blue 문자열을 검색합니다.

db.cars.aggregate([
{
$search: {
"text": {
"query": "blue",
"path": [ "make", "description" ]
}
}
}
])

앞의 쿼리는 다음과 같은 결과를 반환합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Blue four-door sedan, lots of trunk space. Three to four
passengers."
}

다음 예제에서는 간단한 분석기를 사용하는 인덱스 정의에서 simpleAnalyzer라는 이름의 multi를 사용합니다.

쿼리는 description 필드에서 문자열 driver을 검색합니다.

db.cars.aggregate([
{
$search: {
"text": {
"query": "driver",
"path": { "value": "description", "multi": "simpleAnalyzer" }
}
}
}
])

앞의 쿼리는 다음과 같은 결과를 반환합니다.

{
"_id" : 2,
"type" : "coupe",
"make" : "BMW",
"description" : "Red two-door convertible, driver's-side airbag."
}

단순 분석는 driver's side airbag[driver s side airbag]으로 인덱싱하므로 driver에서 일치합니다.

반면, 기본 표준 분석기driver's side airbag[driver's side airbag]으로 인덱싱하므로 driver'sside에서는 일치하지만 driver에서는 일치하지 않습니다.

cars 컬렉션에 대한 인덱스 정의의 multi 객체가 다음과 같다고 가정합니다.

"multi": {
"simpleAnalyzer": {
"analyzer": "lucene.whitespace",
"type": "string"
}
}

다음 예시에서는 공백 분석기를 사용하는 인덱스 정의에서 simpleAnalyzer라는 이름의 multi를 사용합니다.

db.cars.aggregate([
{
$search: {
"text": {
"query": "Three",
"path": { "value": "description", "multi": "simpleAnalyzer" }
}
}
}
])

앞의 쿼리는 다음과 같은 결과를 반환합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Blue four-door sedan, lots of trunk space. Three to
four passengers."
}

Three 용어에 대한 위 쿼리의 경우, 공백 분석기는 대소문자를 구분하기 때문에 Atlas Search는 Three 용어와 일치하는 문서만 반환하고 three은(는) 반환하지 않습니다. 반면 기본값 표준 분석기는 대소문자를 구분하지 않으며 쿼리의 용어와 일치하는 모든 문서를 collection에 나열된 순서대로 반환합니다.

그러면 다음 쿼리를 살펴보겠습니다.

db.cars.aggregate([
{
$search: {
"compound": {
"should": [
{
"text": {
"path": "description",
"query": "Three"
}
},
{
"text": {
"query": "Three",
"path": { "value" : "description", "multi" : "simpleAnalyzer" },
score: { boost: { value: 2 }}
}
}
]
}
}
},
{
$project: {
"_id": 0,
"type": 1,
"description": 1,
"score": { "$meta": "searchScore" }
}
}
])

앞의 쿼리는 다음과 같은 결과를 반환합니다.

{
"type" : "sedan",
"description" : "Blue four-door sedan, lots of trunk space. Three to four passengers seats.",
"score" : 1.1092689037322998
}
{
"type" : "SUV",
"description" : "Black four-door SUV, three rows of seats.",
"score" : 0.17812025547027588
}

Atlas Search는 위 쿼리에 대해 Threethree가 모두 포함된 문서를 반환합니다. Three의 결과 점수가 더 높은 이유는 three의 문서는 기본 표준 분석기를 사용하여 일치시킨 반면, Three의 문서는 지정된 simpleAnalyzer와 기본 표준 분석기 모두에서 일치시켰기 때문입니다.

다음 예에서는 다음 문서와 함께 posts라는 컬렉션을 사용합니다.

{
"_id": 1,
"username": "pinto",
"post": {
"date": "12-03-2018",
"forum": "Tofu Recipes",
"body": "Spicy Garlic Tofu cooks up crispy in 10 minutes or less.
Serve with broccoli and rice for a delicious vegetarian meal."
}
}
{
"_id": 2,
"username": "paloma",
"post": {
"date": "12-08-2018",
"forum": "Tofu Recipes",
"body": "Crispy Tofu in Shiitake Broth has flavors of citrus and
umami. Great as an appetizer or entree."
}
}

동적 필드 매핑을 사용하면 필요에 따라 컬렉션의 모든 필드를 인덱싱할 수 있습니다.

posts collection에 대한 인덱스 정의는 다음과 같습니다.

{
"mappings": {
"dynamic": true
}
}

다음 복합 쿼리post.body 필드에서 broccoli 문자열을 검색하고 필드에 cauliflower 문자열이 포함되지 않도록 지정합니다.

db.posts.aggregate([
{
$search: {
"compound": {
"must": {
"text": {
"query": "broccoli",
"path": "post.body"
}
},
"mustNot": {
"text": {
"query": "cauliflower",
"path": "post.body"
}
}
}
}
}
])

앞의 쿼리는 _id: 1이(가) 포함된 문서를 반환합니다. 여기서 posts.body 필드에는 문자열 broccoli이(가) 포함되어 있습니다.

다음 예시에서는 다음 문서가 포함된 cars라는 collection을 사용합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Four-door sedan, lots of trunk space. Three to four passengers.",
"warehouse" : [
{
"inventory" : 3,
"color" : "red"
}
]
}
{
"_id" : 2,
"type" : "coupe",
"make" : "BMW",
"description" : "Two-door convertible, driver's-side airbag.",
"warehouse" : [
{
"inventory" : 5,
"color" : "black"
}
]
}
{
"_id" : 3,
"type" : "SUV",
"make" : "Ford",
"description" : "Four-door SUV, three rows of seats.",
"warehouse" : [
{
"inventory" : 7,
"color" : "white"
},
{
"inventory" : 3,
"color" : "red"
}
]
}

cars collection에 대한 인덱스 정의는 다음과 같습니다.

{
"mappings": {
"dynamic": true
}
}

다음 쿼리는 red 문자열에 대해 와일드카드 문자 *를 사용하여 지정된 필드를 검색합니다.

다음 쿼리모든 필드에서 문자열 red를 검색합니다.

db.cars.aggregate([
{
"$search": {
"phrase": {
"path": { "wildcard": "*" },
"query": "red"
}
}
}
])

쿼리는 다음과 같은 결과를 반환합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Four-door sedan, lots of trunk space. Three to four passengers.",
"warehouse" : [
{ "inventory" : 3, "color" : "red" }
]
}
{
"_id" : 3,
"type" : "SUV",
"make" : "Ford",
"description" : "Four-door SUV, three rows of seats.",
"warehouse" : [
{ "inventory" : 7, "color" : "white" },
{ "inventory" : 3, "color" : "red" }
]
}

다음 쿼리warehouse 필드 내에 중첩된 필드에서 red 문자열을 검색합니다.

db.cars.aggregate([
{
"$search": {
"text": {
"path": { "wildcard": "warehouse.*" },
"query": "red"
}
}
}
])

쿼리는 다음과 같은 결과를 반환합니다.

{
"_id" : 1,
"type" : "sedan",
"make" : "Toyota",
"description" : "Four-door sedan, lots of trunk space. Three to four passengers.",
"warehouse" : [
{ "inventory" : 3, "color" : "red" }
]
}
{
"_id" : 3,
"type" : "SUV",
"make" : "Ford",
"description" : "Four-door SUV, three rows of seats.",
"warehouse" : [
{ "inventory" : 7, "color" : "white" },
{ "inventory" : 3, "color" : "red" }
]
}
← 와일드카드