Docs 菜单

多分析器

您可以在索引定义中使用 multi对象来指定备用分析器,用于对字段建立索引。除了默认分析器以外,在使用备用分析器对字段进行索引时,您可以使用默认分析器或备用分析器搜索集合。本页说明了如何使用 multi索引定义中指定备用分析器 。要了解有关使用备用分析器进行搜索的更多信息,请参阅构建查询路径

multi 路径选项仅适用于字符串类型的字段。

Atlas Search 不支持嵌套多层 multi 对象。

以下示例展示了 sample_mflix.movies 集合中字段的索引定义。如果已将此集合加载到集群上,则可使用 Atlas 用户界面中的可视化编辑器或 JSON 编辑器来创建示例索引。选择首选的配置方法后,选择该数据库和集合以定义索引。

以下示例索引定义使用 standard 分析器指定了 title 字段的索引。此索引定义还指定 lucene.french 分析器作为 title 字段的备用分析器,且名称为 frenchAnalyzer

  1. 单击 Refine Your Index 配置索引。

  2. Field Mappings 部分中,单击 Add Field 打开 Add Field Mapping 窗口。

  3. Field Name 下拉列表中选择 title

  4. 单击 Data Type 下拉列表并选择 String(如果尚未选择)。

  5. 展开 String Properties 并进行以下更改:

    索引分析器

    如果尚未选择,请从下拉菜单中选择 lucene.standard

    Search Analyzer

    如果尚未选择,请从下拉菜单中选择 lucene.standard

    索引选项

    使用默认 offsets

    Store

    使用默认 true

    忽略以上内容

    保留默认设置。

    规范

    使用默认 include

  6. 单击Add Multi Field可对title字段配置另一个分析器。

  7. Multi Field Name字段中输入frenchAnalyzer

  8. Multi Field Properties 进行以下更改:

    索引分析器

    如果尚未选择,请从下拉菜单中选择 lucene.french

    Search Analyzer

    如果尚未选择,请从下拉菜单中选择 lucene.french

    索引选项

    使用默认 offsets

    Store

    使用默认 true

    忽略以上内容

    保留默认设置。

    规范

    使用默认 include

  9. 单击 Add(连接)。

  10. 单击 Save Changes(连接)。

  11. 单击 Create Search Index(连接)。

  1. 将默认索引定义替换为以下索引定义。

    1{
    2 "mappings": {
    3 "dynamic": false,
    4 "fields": {
    5 "title": {
    6 "type": "string",
    7 "analyzer": "lucene.standard",
    8 "multi": {
    9 "frenchAnalyzer": {
    10 "type": "string",
    11 "analyzer": "lucene.french"
    12 }
    13 }
    14 }
    15 }
    16 }
    17}
  2. 单击 Next(连接)。

  3. 单击 Create Search Index(连接)。

以下查询使用名为 frenchAnalyzer 的备用分析器来搜索字符串 liberte

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "liberte",
6 "path": { "value": "title", "multi": "frenchAnalyzer" }
7 }
8 }
9 },
10 {
11 "$project": {
12 "title": 1,
13 "year": 1,
14 "_id": 0
15 }
16 }
17])
[ { title: 'è Nous la Libertè', year: 1931 } ]

结果中的文档是匹配的,因为 frenchAnalyzer 会将索引字符串 è Nous la Libertè 和查询词 liberte 简化为基本词 libert。Atlas Search 不会为使用 standard 分析器的搜索返回此文档,因为它没有删除标注字符。每个分析器均会为此索引和查询生成以下词元(可搜索词):

分析器
Index Tokens
查询标记

标准分析器

è, nous , la , libertè

libertè

French 分析器

è, libert

libert

以下示例索引定义使用 standard 分析器指定 titleplot 字段的索引。此索引定义还指定 lucene.french 作为 titleplot 字段的备用分析器。

  1. 单击 Refine Your Index 配置索引。

  2. Field Mappings 部分中,单击 Add Field 打开 Add Field Mapping 窗口。

  3. Field Name 下拉列表中选择 title

  4. 单击 Data Type 下拉列表并选择 String(如果尚未选择)。

  5. 展开 String Properties 并进行以下更改:

    索引分析器

    如果尚未选择,请从下拉菜单中选择 lucene.standard

    Search Analyzer

    如果尚未选择,请从下拉菜单中选择 lucene.standard

    索引选项

    使用默认 offsets

    Store

    使用默认 true

    忽略以上内容

    保留默认设置。

    规范

    使用默认 include

  6. 单击Add Multi Field可对title字段配置另一个分析器。

  7. Multi Field Name字段中输入frenchAnalyzer

  8. Multi Field Properties 进行以下更改:

    索引分析器

    如果尚未选择,请从下拉菜单中选择 lucene.french

    Search Analyzer

    如果尚未选择,请从下拉菜单中选择 lucene.french

    索引选项

    使用默认 offsets

    Store

    使用默认 true

    忽略以上内容

    保留默认设置。

    规范

    使用默认 include

  9. 单击 Add(连接)。

  10. 点击 Add Field Mapping 打开 Add Field Mapping 窗口。

  11. Field Name 下拉列表中选择 plot

  12. 重复步骤 4 到 9。

  13. 单击 Save Changes(连接)。

  14. 单击 Create Search Index(连接)。

  1. 将默认索引定义替换为以下索引定义。

    1{
    2 "mappings": {
    3 "dynamic": false,
    4 "fields": {
    5 "title": {
    6 "type": "string",
    7 "analyzer": "lucene.standard",
    8 "multi": {
    9 "frenchAnalyzer": {
    10 "type": "string",
    11 "analyzer": "lucene.french"
    12 }
    13 }
    14 },
    15 "plot": {
    16 "type": "string",
    17 "analyzer": "lucene.standard",
    18 "multi": {
    19 "frenchAnalyzer": {
    20 "type": "string",
    21 "analyzer": "lucene.french"
    22 }
    23 }
    24 }
    25 }
    26 }
    27}
  2. 单击 Next(连接)。

  3. 单击 Create Search Index(连接)。

以下查询使用名为 frenchAnalyzer 的备用分析器在 titleplot 字段中搜索字符串 revolution 的匹配项。

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "query": "revolution",
6 "path": ["title", "plot",
7 { "value": "title", "multi": "frenchAnalyzer" },
8 { "value": "plot", "multi": "frenchAnalyzer" }
9 ]
10 }
11 }
12 },
13 {
14 "$limit": 5
15 },
16 {
17 "$project": {
18 "title": 1,
19 "plot": 1,
20 "year": 1,
21 "_id": 0
22 }
23 }
24])
[
{
year: 2012,
plot: 'REVOLUTION is a film about changing the world, going for it, taking a stand, and fighting for something. A true-life adventure following Director, Rob Stewart (SHARKWATER) over four years ...',
title: 'Revolution'
},
{
plot: 'New York trapper Tom Dobb becomes an unwilling participant in the American Revolution after his son Ned is drafted into the Army by the villainous Sergeant Major Peasy. Tom attempts to find...',
title: 'Revolution',
year: 1985
},
{
plot: "Together with five Soviet avant-garde artists, hero of the Russian revolution Polina Schneider travels to Siberia to 'civilize' the native Khanty and Nenets tribes, for whom interaction ...",
title: 'Angels of Revolution',
year: 2014
},
{
plot: 'Two mismatched sets of identical twins - one aristocrat, one peasant - mistakenly exchange identities on the eve of the French Revolution.',
title: 'Start the Revolution Without Me',
year: 1970
},
{
plot: "A history of the French Revolution from the decision of the king to convene the Etats-Generaux in 1789 in order to deal with France's debt problem. The first part of the movie tells the ...",
title: 'La rèvolution franèaise',
year: 1989
}
]

结果中的文档在 titleplot 字段中包含查询术语。每个分析器为结果中每个文档的 title 字段创建以下标记(可搜索术语):

年份

分析器

Index Tokens

查询标记

2012

Standard Analyzer
French Analyzer
revolution
revolution
revolution
revolution

1985

Standard Analyzer
French Analyzer
revolution
revolution
revolution
revolution

2014

Standard Analyzer
French Analyzer
angels, of, revolution
angels, of, revolution
revolution
revolution

1970

Standard Analyzer
French Analyzer
start, the, revolution, without, me
start, the, revolution, without
revolution
revolution

1989

Standard Analyzer
French Analyzer
la, rèvolution, franèaise
revolution, franeais
revolution
revolution

对于这些结果中的第五个文档,frenchAnalyzer 删除了 title 字段中的标注字符,以便将此文档与查询词相匹配。