【问题标题】:MongoDB full text search, autocomplete on two fieldsMongoDB全文搜索,两个字段的自动完成
【发布时间】:2020-07-31 05:33:26
【问题描述】:

我正在尝试实现MongoDB atlas search,目标是在 2 个字段上自动完成。

我目前有这个实现:

const searchStep = {
    $search: {
        // Read more about compound here:
        // https://docs.atlas.mongodb.com/reference/atlas-search/compound/
        compound: {
            must: [
                {
                    autocomplete: {
                        query,
                        path: 'name',
                    },
                },
                {
                    autocomplete: {
                        query,
                        path: 'description',
                    },
                },
            ],
        },
    },
}

这似乎不起作用,似乎只有在 both 名称 AND 描述匹配时才起作用。我该如何解决这个问题,所以我查询 名称和描述?

我现在尝试使用 通配符 选项:

{
    wildcard: {
        query,
        path: ['name', 'description'],
        allowAnalyzedField: true,
    }
}

但通配符解决方案似乎不起作用 - 没有返回相关结果...

【问题讨论】:

    标签: mongodb-atlas-search


    【解决方案1】:

    如果您尝试匹配namesubscription,请使用should: 而不是must:

    must 将要求所有子查询匹配,而 should 要求其中只有一个匹配。

    const searchStep = {
        $search: {
            // Read more about compound here:
            // https://docs.atlas.mongodb.com/reference/atlas-search/compound/
            compound: {
                should: [
                    {
                        autocomplete: {
                            query,
                            path: 'name',
                        },
                    },
                    {
                        autocomplete: {
                            query,
                            path: 'description',
                        },
                    },
                ],
            },
        },
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-22
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      相关资源
      最近更新 更多