短语
定义
语法
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
使用以下词条来构造查询:
字段 | 类型 | 说明 | 必要性 |
---|---|---|---|
query | 字符串或字符串数组 | 要搜索的一个或多个字符串。 | 是 |
path | 字符串或字符串数组 | 要搜索的一个或多个带索引字段。您还可以指定通配符路径进行搜索。要学习;了解详情,请参阅构建查询路径。 重要提示:要使用 | 是 |
slop | 整型 | query 短语中单词之间允许的距离。较低值允许单词之间的位置距离较小,而较大值允许对单词之间的更多重组和单词之间的距离更大,以满足查询条件。默认值为 0 ,这意味着词语必须与查询位于完全相同的位置才能视为匹配。精确匹配的得分更高。 | no |
score | 对象 | 分配给匹配搜索结果的分数。您可以使用以下选项修改默认分数:
当您查询数组中的值时,Atlas Search 不会根据与查询匹配的数组内的值数量更改匹配结果的分数。无论数组内有多少个匹配项,分数都将与单个匹配项相同。 有关修改默认评分选项的信息,请参阅对结果中的文档进行评分。 | no |
synonyms | 字符串 | Optional |
示例
此页面上的示例使用 sample_mflix
数据库中的 movies
集合。将示例数据集加载到集群后,使用动态映射创建 Atlas Search 索引,并在集群上运行示例查询。要试用同义词示例,还必须在 sample_mflix
数据库中添加synonymous_terms 集合,然后使用 synonyms
映射集合定义索引。
单个短语示例
以下 Atlas 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 ]
多个短语示例
以下 Atlas 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 示例
以下 Atlas Search 示例针对查询字符串 men women
对 title
字段进行搜索。query
中 5
的 slop
值允许文字移动得更灵活以及单词 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
字段中搜索给定查询字符串中的术语。Atlas Search 根据在 sample_mflix.movies
集合的索引的同义词映射定义中指定的 synonymous_terms
同义词源集合中的映射类型返回结果。
以下查询在 plot
字段中的任意位置搜索短语 automobile race
,且两个词之间的距离最多为 5
。
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
同义词,
Atlas Search 在搜索 car race
或 vehicle
race
时返回相似的结果,因为我们将 automobile
、car
和 vehicle
配置为 equivalent
同义词。但是,automobile
contest
的结果不包括带有 race
或 rally
的文档, automobile rally
的结果不包括带有 race
或 contest
的文档,因为我们没有将 contest
或 rally
配置为任何术语的同义词。