MongoDB $文本运算符匹配搜索字符串为子字符串的文档

时间:2016-02-26 17:14:39

标签: mongodb full-text-search mongodb-query

我知道$ text运算符不能与正则表达式一起工作......但我需要一些搜索才能正常工作。

文件:

{ "field1": "some content", "field2:" "another content"}

{ "field1": "yet one more content", "field2": "the final content"}

如果我们按字符串"ye ano"进行搜索,则两个文档都应该在结果中,因为ye出现在第二个文档中,而ano出现在第一个文档中。

由于案例/变音符号的不敏感性,我们非常感谢使用$ text运算符的解决方法。

我也会接受行为不相等但接近的事情。主要关注的是效率,因为我已经有了一个O(n lg n)解决方案,但这对搜索来说非常昂贵......

1 个答案:

答案 0 :(得分:1)

看起来我们需要OR并在两个字段上通过regex执行搜索, 首先创建索引: - )

  

db.math.createIndex({FIELD1:1})

     

db.math.createIndex({FIELD2:1})

然后使用[0-4]在文本字段中搜索第一个符号,如果省略colscan将会出现

db.math.find({$or:[{"field1":{$regex:/ye|ano[0-4]/}},{"field2":{$regex:/ye|ano[0-4]/}}]}
).pretty()
{
        "_id" : ObjectId("56d0c236854cc0de43173fa6"),
        "field1" : "some content",
        "field2" : "another content"
}
{
        "_id" : ObjectId("56d0c24b854cc0de43173fa7"),
        "field1" : "yet one more content",
        "field2" : "the final content"
}

以及更重要的索引用于搜索:

     db.math.find({$or:[{"field1":{$regex:/ye|ano[0-4]/}},{"field2":{$regex:/ye|ano
    [0-4]/}}]}).explain()
    {
            "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "test.math",
                    "indexFilterSet" : false,
                    "parsedQuery" : {
                            "$or" : [
                                    {
                                            "field1" : /ye|ano[0-4]/
                                    },
                                    {
                                            "field2" : /ye|ano[0-4]/
                                    }
                            ]
                    },
                    "winningPlan" : {
                            "stage" : "SUBPLAN",
                            "inputStage" : {
                                    "stage" : "FETCH",
                                    "inputStage" : {
                                            "stage" : "OR",
                                            "inputStages" : [
                                                    {
                                                            "stage" : "IXSCAN",
                                                            "filter" : {
                                                                    "$or" : [
                                                                            {
                                                                                "field1" : /ye|ano[0-4]/
                                                                            }
                                                                    ]
                                                            },
                                                            "keyPattern" : {
                                                                    "field1" : 1,
                                                                    "field2" : 1
                                                            },
                                                            "indexName" : "field1_1_field2_1",
                                                            "isMultiKey" : false,
                                                            "direction" : "forward",

                                                            "indexBounds" : {
                                                                    "field1" : [
                                                                            "[\"\",{})",
                                                                            "[/ye|ano[0-4]/, /ye|ano[0-4]/]"
                                                                    ],
                                                                    "field2" : [
                                                                            "[MinKey, MaxKey]"
                                                                    ]
                                                            }
                                                    },
                                                    {
                                                            "stage" : "IXSCAN",
                                                            "filter" : {
                                                                    "$or" : [
                                                                            {
                                                                                "field2" : /ye|ano[0-4]/
                                                                            }
                                                                    ]
                                                            },
                                                            "keyPattern" : {
                                                                    "field2" : 1
                                                            },
                                                            "indexName" : "field2_1"
    ,
                                                            "isMultiKey" : false,
                                                            "direction" : "forward",

                                                            "indexBounds" : {
                                                                    "field2" : [
                                                                            "[\"\",{})",
                                                                            "[/ye|ano[0-4]/,/ye|ano[0-4]/]"
                                                                    ]
                                                            }
                                                    }
                                            ]
                                    }
                            }
                    },
                    "rejectedPlans" : [ ]
            },
            "serverInfo" : {
                    "host" : "greg",
                    "port" : 27017,
                    "version" : "3.0.8",
                    "gitVersion" : "83d8cc25e00e42856924d84e220fbe4a839e605d"
            },
            "ok" : 1
    }