在MongoDB中对非预期文档运行文本搜索

在MongoDB中对非预期文档运行文本搜索,mongodb,mongodb-query,aggregation-framework,Mongodb,Mongodb Query,Aggregation Framework,我有一个类似这样的收藏: { "strings" : [ "Hello world", "Error connecting to server", "Dbus error" ], "file" : "test.c" } { "strings" : [ "Could not configure supporting library.", "Assertion failed",

我有一个类似这样的收藏:

{
    "strings" : [
        "Hello world",
        "Error connecting to server",
        "Dbus error"
    ],
    "file" : "test.c"
}
{
    "strings" : [
        "Could not configure supporting library.",
        "Assertion failed",
        "Error in server response"
    ],
    "file" : "run.c"
}
...
大约有25万个这样的文档,每个文档都有自己的字符串数组和唯一的文件名。在查询时,我有一个字符串,它可以是其中一个文档字符串的子字符串,也可以包含某些文档中的一些字符串(即,它永远不会完全匹配任何文档中的任何字符串)。 示例:

Hello world hi there             // Should match test.c
Error connecting                 // test.c
Error connecting to server: 107  // test.c
Assertion failed: Dbus error     // Should match both test.c and run.c
我需要检索其“strings”字段包含与我的查询字符串最匹配的字符串的文档

我尝试了索引文本搜索:

db.testcoll.find( { $text: { $search: "Could not configure supporting library." } } , {"file": 1, "_id": 0, score: {$meta: "textScore"}}).sort({score: {$meta: "textScore"}})
但是,问题是,一个文档可能有几个字符串与我的查询字符串部分匹配,因此与其他文档相比,具有最接近匹配字符串的文档的匹配分数仍然较低

我尝试在聚合中展开“strings”字段,以便每个文档只有一个字符串,我希望最接近的匹配字符串获得最高分数。但是,如果是第一个管道阶段,mongodb只允许
$match
具有
$text
,因此我不能在它之前进行展开步骤

db.testcoll.aggregate([{$project: {file: 1, strings: 1}}, {$unwind: "$strings"}, {$match: { $text: { $search: "Could not configure supporting library." } }}])
输出:

2019-05-27T11:36:56.671+0530 E QUERY    [js] Error: command failed: {
    "ok" : 0,
    "errmsg" : "$match with $text is only allowed as the first pipeline stage",
    "code" : 17313,
    "codeName" : "Location17313"
} : aggregate failed
是否有方法对“展开”返回的文档执行文本搜索?如果没有,有没有办法在mongodb中执行这种文本搜索? 提前感谢您的帮助