text
定义
语法
text
通过以下语法实现:
{ $search: { "index": <index name>, // optional, defaults to "default" "text": { "query": "<search-string>", "path": "<field-to-search>", "fuzzy": <options>, "matchCriteria": "any" | "all" "score": <options>, "synonyms": "<synonyms-mapping-name>" } } }
字段
字段 | 类型 | 说明 | 必要性 |
---|---|---|---|
query | 字符串或字符串数组 | 要搜索的一个或多个字符串。如果字符串中有多个词,Atlas Search 还会分别为字符串中的每个词查找匹配项。 | 必需 |
path | 字符串或字符串数组 | 必需 | |
fuzzy | 文档 | 启用模糊搜索。查找与搜索词相似的字符串。不能将 fuzzy 与 synonyms 一起使用。 | Optional |
fuzzy.maxEdits | 整型 | Optional | |
fuzzy.prefixLength | 整型 | 结果中每个术语开头必须完全匹配的字符数。默认值为 0 。 | Optional |
fuzzy.maxExpansions | 整型 | 要生成和搜索的最大变体数量。此限制适用于每个令牌。默认值为 50 。 | Optional |
matchCriteria | 字符串 | 用于匹配查询中术语的条件。值可以是以下值之一:
如果省略,则默认值为 | 可选,但建议使用 |
score | 文档 | 分配给匹配搜索词结果的分数。使用以下选项之一修改分数:
当您查询数组中的值时,Atlas Search 不会根据与查询匹配的数组内的值数量更改匹配结果的分数。无论数组内有多少个匹配项,分数都将与单个匹配项相同。 | Optional |
synonyms | 字符串 | Optional |
示例
此页面上的示例使用 sample_mflix
数据库中的 movies
集合。将示例数据集加载到集群后,使用动态映射创建 Atlas Search 索引,并在集群上运行示例查询。您还必须在索引中定义一个 synonyms
映射集合,如索引定义示例所示,以尝试下面的同义词示例。
基本示例
以下 Atlas Search 示例使用 text
操作符在 movies
集合的 title
字段中搜索 surfer
一词。
例子
以下查询将在 title
字段中搜索 surfer
一词。其中包括一个 $project 阶段,以便:
排除所有字段,但不包括
title
添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "surfer" } } }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
{ "title" : "Soul Surfer", "score" : 4.518949508666992 } { "title" : "Little Surfer Girl", "score" : 3.8856077194213867 } { "title" : "Fantastic 4: Rise of the Silver Surfer", "score" : 2.489800453186035 }
模糊示例
以下查询使用 text
运算符在 movies
集合内的 title
字段中搜索某些词,而这些词位于query
短语 naw
yark
中每个词的一个字符变体内。Atlas Search 会返回不同结果,具体取决于您使用的是默认 fuzzy
选项还是定义了 maxExpansions
、prefixLength
或 maxEdits
字段。
单击以下选项卡,查看使用默认选项和指定选项的查询示例:
例子
以下查询在 title
字段中搜索短语 naw yark
。它使用 fuzzy
默认选项,其中:
maxEdits
允许给定短语中每个术语最多有两个字符变体,以将查询与文档相匹配。maxExpansions
为naw yark
中的每个术语考虑多达五十个相似术语来查找匹配项。prefixLength
已禁用。
该查询还包括一个 $limit 阶段,用于将输出限制为 10 个结果,以及一个 $project 阶段,用于:
排除所有字段,但不包括
title
添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": {} } } }, { $limit: 10 }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
{ "title" : "New York, New York", "score" : 3.6040148735046387 } { "title" : "New York", "score" : 3.323730945587158 } { "title" : "New York Stories", "score" : 2.8579015731811523 } { "title" : "New York Minute", "score" : 2.8579015731811523 } { "title" : "Synecdoche, New York", "score" : 2.8579015731811523 } { "title" : "New York Doll", "score" : 2.8579015731811523 } { "title" : "Little New York", "score" : 2.8579015731811523 } { "title" : "Escape from New York", "score" : 2.506596088409424 } { "title" : "Naked in New York", "score" : 2.506596088409424 } { "title" : "Autumn in New York", "score" : 2.506596088409424 }
例子
以下查询会在 title
字段中搜索与字符串 naw yark
中每个术语相差一个字符以内的术语。它使用:
maxEdits
字段表示只允许使用一种字符变体,以便将查询与文档匹配。maxExpansions
字段表示将查询与文档进行匹配时,必须考虑最多一百个与naw
相似的术语和一百个与yark
相似的术语。
该查询还包括一个 $limit 阶段,用于将输出限制为 10 个结果,以及一个 $project 阶段,用于:
排除所有字段,但不包括
title
添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": { "maxEdits": 1, "maxExpansions": 100, } } } }, { $limit: 10 }, { $project: { "_id": 0, "title": 1, score: { $meta: "searchScore" } } } ])
{ "title" : "New York, New York", "score" : 4.38106107711792 } { "title" : "New York", "score" : 4.040346145629883 } { "title" : "New York Stories", "score" : 3.4740817546844482 } { "title" : "New York Minute", "score" : 3.4740817546844482 } { "title" : "Synecdoche, New York", "score" : 3.4740817546844482 } { "title" : "New York Doll", "score" : 3.4740817546844482 } { "title" : "Little New York", "score" : 3.4740817546844482 } { "title" : "Escape from New York", "score" : 3.047032356262207 } { "title" : "Naked in New York", "score" : 3.047032356262207 } { "title" : "Autumn in New York", "score" : 3.047032356262207 }
例子
以下查询会在 title
字段中搜索与字符串 naw yark
中每个术语相差一个字符以内的术语。它使用:
maxEdits
字段表示只允许使用一种字符变体来将查询与文档相匹配。prefixLength
字段表示不得更改字符串naw yark
中每个术语的前两个字符以将查询与文档匹配。
该查询还包括一个 $limit 阶段,用于将输出限制为 8 个结果,以及一个 $project 阶段,用于:
排除
_id
和title
之外的所有字段添加字段
score
db.movies.aggregate([ { $search: { "text": { "path": "title", "query": "naw yark", "fuzzy": { "maxEdits": 1, "prefixLength": 2, } } } }, { $limit: 8 }, { $project: { "_id": 1, "title": 1, score: { $meta: "searchScore" } } } ])
{ "_id" : ObjectId("573a1396f29313caabce5646", "title" : "The Longest Yard", "score" : 2.914206027984619 } { "_id" : ObjectId("573a13aff29313caabd31ed8", "title" : "The Longest Yard", "score" : 2.914206027984619 } { "_id" : ObjectId("573a13b7f29313caabd4ad8b", "title" : "Stomp the Yard", "score" : 2.914206027984619 } { "_id" : ObjectId("573a13eaf29313caabdcf410", "title" : "Naz & Maalik", "score" : 2.5460386276245117 } { "_id" : ObjectId("573a1393f29313caabcddbed", "title" : "La nao capitana", "score" : 2.1892051696777344 } { "_id" : ObjectId("573a1399f29313caabcee781", "title" : "Kabhi Haan Kabhi Naa", "score" : 1.9200985431671143 } { "_id" : ObjectId("573a13a2f29313caabd0b815", "title" : "Kaho Naa... Pyaar Hai", "score" : 1.9200985431671143 } { "_id" : ObjectId("573a13a7f29313caabd1b5c0", "title" : "Oysters at Nam Kee's", "score" : 1.9200985431671143 }
匹配 all
以下查询使用 text
操作符在 movies
集合中的 plot
字段中搜索查询字符串 automobile race
中的所有术语。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "automobile race", "matchCriteria": "all" } } }, { "$limit": 20 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: 'A young driver, Speed Racer, aspires to be champion of the racing world with the help of his family and his high-tech Mach 5 automobile.', title: 'Speed Racer', score: 6.122188568115234 }, { plot: 'A gorgeous young automobile fanatic--and front to the hottest unsigned band on the West coast--finds herself caught up in illegal drag-racing competitions organized by exotic car fanatics.', title: 'Redline', score: 5.497724533081055 }, { 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: 5.282209396362305 } ]
Atlas Search 返回在 plot
字段中的任何位置包含 automobile
和 race
的文档。
同义词示例
以下示例使用 text
操作符搜索 sample_mflix.movies
命名空间中的 plot
字段。Atlas Search 根据在 sample_mflix.movies
集合的索引的同义词映射定义中指定的 synonymous_terms
同义词源集合中的映射类型返回结果。
any
equivalent
使用 映射匹配
以下查询在 plot
字段中搜索短语 attire
。它还使用集合索引中名为 mySynonyms
的同义词映射来搜索配置为单词 dress
的同义词的单词。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "attire", "synonyms": "my_synonyms", "matchCriteria": "any" } } }, { "$limit": 5 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: 'The Sanguiwon are responsible for the attire worn by royalty. Dol-Seok (Han Suk-Kyu) is the best master artisan in charge of royal attire. He views set rules as paramount to his job. ...', title: 'The Royal Tailor', score: 15.825017929077148 }, { plot: "During Christmas' holidays, the children of a village split in two gang to play a snowball war. But that half-tone war scattered some bitterness and make more difficult the mutual attirance...", title: 'La guerre des tuques', score: 11.863945007324219 }, { plot: 'The Dress is a tale filled with sex, violence, comedy and drama as it follows the life of a dress. Conceived under a cloud of frustration and despair, the dress serves as the hub in a great...', title: 'The Dress', score: 5.061710834503174 }, { plot: "Boardroom and dressing-room intrigues spill on to the field at the Australian Rules football club.", title: 'The Club', score: 3.991994857788086 }, { plot: 'Deserting soldier dresses as a woman to escape detection; liking the female role he goes to a dance with another soldier and is exposed.', title: 'The Triple Echo', score: 3.5169785022735596 } ]
Atlas Search 会返回包含术语 attire
、apparel
和 dress
的文档,因为我们将所有这些术语配置为同义词来源集合 synonymous_terms
中的 equivalent
同义词,这意味着所有这些术语都是彼此的同义词。因此,在 dress
和 attire
上进行搜索时,Atlas Search 会返回类似文档。
any
explicit
使用 映射匹配
以下查询在 plot
字段中搜索短语 boat
race
。它还使用集合索引中名为 my_synonyms
的同义词映射来搜索配置为单词 boat
和 race
的同义词的单词。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "boat race", "synonyms": "my_synonyms", "matchCriteria": "any" } } }, { "$limit": 10 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: 'A man is picked up by a fishing boat, bullet-riddled and suffering from amnesia, before racing to elude assassins and regain his memory.', title: 'The Bourne Identity', score: 15.405073165893555 }, { plot: 'A few friends builds a sailing boat together. They plan trips and during the completion of the boat marriages crumble, new relations blossom and the boat burns. They rebuild it and the ...', title: 's/y Glèdjen', score: 13.40434741973877 }, { plot: 'Famous motor-racing champion Joe Greer returns to his hometown to compete in a local race. He discovers his younger brother has aspirations to become a racing champion and during the race ...', title: 'The Crowd Roars', score: 10.466073036193848 }, { plot: 'The claustrophobic world of a WWII German U-boat; boredom, filth, and sheer terror.', title: 'Das Boot', score: 10.304922103881836 }, { plot: 'Several survivors of a torpedoed ship find themselves in the same boat with one of the men who sunk it.', title: 'Lifeboat', score: 9.776729583740234 }, { plot: 'A fishing-boat crew takes on a dangerous commission to smuggle a group of illegal immigrants from China to Korea.', title: 'Haemoo', score: 9.532430648803711 }, { plot: 'A land baron tries to reconnect with his two daughters after his wife is seriously injured in a boating accident.', title: 'The Descendants', score: 9.300044059753418 }, { plot: 'A weekend boating party turns into a nightmare for a group of young Londoners when they stumble upon a terrifying secret hidden in the reeds.', title: 'The Reeds', score: 9.078716278076172 }, { plot: 'Documentary telling the true story of the sinking of the liner Laconia by a German U-boat in 1942 through the eyes of six survivors.', title: 'The Sinking of the Laconia', score: 9.078716278076172 }, { plot: 'Sabotage efforts damage an international air race.', title: 'Those Magnificent Men in Their Flying Machines or How I Flew from London to Paris in 25 hours 11 minutes', score: 8.983794212341309 } ]
Atlas Search 会返回包含术语 boat
、vessel,
或 sail
、race
、rally
或 contest
的文档,因为我们将 boat
配置为 explicit
同义词或 vessel
和 sail
,并将 race
配置为 rally
和 contest
的 explicit
同义词。Atlas Search 不会返回有关 vessel
和 sail
或 rally
和 contest
的任何查询结果,因为我们没有将这些单词配置为将任何其他词语视为同义词。
使用同义词匹配all
以下查询使用 text
操作符在 movies
集合中的 plot
字段中搜索查询字符串 automobile race
中的所有词语。
Atlas Search 根据在 sample_mflix.movies
集合的索引的同义词映射定义中指定的同义词源集合 synonymous_terms
中的映射类型返回结果。
db.movies.aggregate([ { "$search": { "text": { "path": "plot", "query": "automobile race", "matchCriteria": "all", "synonyms": "my_synonyms" } } }, { "$limit": 20 }, { "$project": { "_id": 0, "plot": 1, "title": 1, "score": { "$meta": "searchScore" } } } ])
[ { plot: 'A young driver, Speed Racer, aspires to be champion of the racing world with the help of his family and his high-tech Mach 5 automobile.', title: 'Speed Racer', score: 17.354578018188477 }, { plot: 'A gorgeous young automobile fanatic--and front to the hottest unsigned band on the West coast--finds herself caught up in illegal drag-racing competitions organized by exotic car fanatics.', title: 'Redline', score: 16.085742950439453 }, { 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: 15.087226867675781 }, { plot: 'A race car driver becomes a champion with a Volkswagen Beetle with a mind of its own.', title: 'The Love Bug', score: 5.1777167320251465 }, { plot: 'A wide variety of eccentric competitors participate in a wild and illegal cross-country car race.', title: 'The Cannonball Run', score: 5.041530609130859 }, { plot: 'The original characters from the first Cannonball movie race across the country once more in various cars and trucks.', title: 'Cannonball Run II', score: 4.67281436920166 }, { plot: 'After a tied 1st place in a local stunt race, two drivers start a contest to decide who of them will own the prize, a dune buggy. But when a mobster destroys the car, they are determined to get it back.', title: "Watch Out, We're Mad", score: 4.500225067138672 }, { plot: 'Four teenagers are killed in a car accident. Two of the teenagers refuse to go with "The Grim Reaper" and a race between life and death ensues!', title: 'Soultaker', score: 4.455573081970215 }, { plot: "The driver races to locate a kidnapped victim locked in the trunk of an abandoned car somewhere on the water's edge. Linked to her only by cell phone, the driver narrows in on her location in a desperate race against time and tide.", title: 'Hostage', score: 4.429774284362793 }, { plot: 'Race car driver Lucky Jackson goes to Las Vegas to earn money to pay for a new engine for his motor car. Working as a waiter, he still finds the time to court young Rusty Martin.', title: 'Viva Las Vegas', score: 4.379656791687012 }, { plot: 'Barry Pepper portrays legendary race car drive Dale Earnhardt, who died in 2001 during the last lap of the Daytona 500.', title: '3: The Dale Earnhardt Story', score: 4.3543548583984375 }, { plot: 'Bounty hunters from the future transport a doomed race-car driver to 2009 New York, where his mind will be replaced with that of a dead billionaire.', title: 'Freejack', score: 4.257633209228516 }, { 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: 4.257633209228516 }, { plot: 'A hot-shot race-car named Lightning McQueen gets waylaid in Radiator Springs, where he finds the true meaning of friendship and family.', title: 'Cars', score: 4.257633209228516 }, { plot: 'Star race car Lightning McQueen and his pal Mater head overseas to compete in the World Grand Prix race. But the road to the championship becomes rocky as Mater gets caught up in an intriguing adventure of his own: international espionage.', title: 'Cars 2', score: 4.231379985809326 }, { plot: "An illegal race that takes place over the United States and nothing will stop this bunch of racers except for the occasional cop or a damsel in distress. Jackie Chan's car is not in this ...", title: 'Cannonball Fever', score: 4.1651153564453125 }, { plot: 'Roy is mad about cars, and runs Stallion Parts while attending to his yellow Mustang, far away from his daughter, which comes on holiday, while he is to attend a race. Soon he is challenged in a illegal race through the length of Norway.', title: 'Bèrning', score: 4.050384521484375 }, { 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: 3.9916391372680664 }, { plot: "After a young man's premonition of a deadly race-car crash helps saves the lives of his peers, Death sets out to collect those who evaded their end.", title: 'The Final Destination', score: 3.9916391372680664 }, { plot: "A fast-paced comedy about a young Belgian car nut and hairdresser's apprentice, his girlfriend, and their legal and illegal attempts to get a Porsche under him for his nearing debut race.", title: 'The Departure', score: 3.832036256790161 } ]
Atlas Search 会返回包含查询词 automobile
、car
或 vehicle
以及 race
、context
或 rally
的文档,因为我们将 automobile
、vehicle
和 car
配置为 equivalent
同义词,并且我们将 rally
和 contest
配置为 synonymous_terms
集合中 race
的 explicit
同义词。