mongodb文本索引不起作用

时间:2017-08-10 10:10:54

标签: mongodb full-text-indexing

mongo3.4.6

嗨导出,我是mongodb的新手,我在索引方面遇到了一些问题。下面我在recordInfo集合中的RequestId(string)上创建一个文本索引:

  

db.getCollection(" RecordInfo&#34)上的createIndex({"的requestId":"文本"})。

但是当我尝试在下面查询时:

  

db.getCollection(" RecordInfo&#34)找到。({"的requestId":" 4513456313212313212aaaa"})解释(" executionStats"。 ),你可以看到它没有使用索引:

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 1,
    "executionTimeMillis" : 17,
    "totalKeysExamined" : 0,
    "totalDocsExamined" : 9998,
    "executionStages" : {
        "stage" : "COLLSCAN",
        "filter" : {
            "RequestId" : {
                "$eq" : "4513456313212313212aaaa"
            }
        },
        "nReturned" : 1,
        "executionTimeMillisEstimate" : 21,
        "works" : 10000,
        "advanced" : 1,
        "needTime" : 9998,
        "needYield" : 0,
        "saveState" : 78,
        "restoreState" : 78,
        "isEOF" : 1,
        "invalidates" : 0,
        "direction" : "forward",
        "docsExamined" : 9998
    }
},

然后我尝试使用$ text:

  

db.getCollection(" RecordInfo&#34)找到({$文本:{$搜索:" 4513456313212313212aaaa"}})。解释(" executionStats&#34)   它给了我下面,我认为非常好:

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 1,
    "executionTimeMillis" : 1,
    "totalKeysExamined" : 1,
    "totalDocsExamined" : 1,
    "executionStages" : {
        "stage" : "TEXT",
        "nReturned" : 1,
        "executionTimeMillisEstimate" : 0,
        "works" : 5,
        "advanced" : 1,
        "needTime" : 3,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 1,
        "invalidates" : 0,
        "indexPrefix" : {
        },
        "indexName" : "RequestId_text",
        "parsedTextQuery" : {
            "terms" : [
                "4513456313212313212aaaa"
            ],
            "negatedTerms" : [ ],
            "phrases" : [ ],
            "negatedPhrases" : [ ]
        },
        "textIndexVersion" : 3,
        "inputStage" : {
            "stage" : "TEXT_MATCH",
            "nReturned" : 1,
            "executionTimeMillisEstimate" : 0,
            "works" : 5,
            "advanced" : 1,
            "needTime" : 3,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "docsRejected" : 0,
            "inputStage" : {
                "stage" : "TEXT_OR",
                "nReturned" : 1,
                "executionTimeMillisEstimate" : 0,
                "works" : 5,
                "advanced" : 1,
                "needTime" : 3,
                "needYield" : 0,
                "saveState" : 0,
                "restoreState" : 0,
                "isEOF" : 1,
                "invalidates" : 0,
                "docsExamined" : 1,
                "inputStage" : {
                    "stage" : "IXSCAN",
                    "nReturned" : 1,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 2,
                    "advanced" : 1,
                    "needTime" : 0,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "keyPattern" : {
                        "_fts" : "text",
                        "_ftsx" : 1
                    },
                    "indexName" : "RequestId_text",
                    "isMultiKey" : true,
                    "isUnique" : false,
                    "isSparse" : false,
                    "isPartial" : false,
                    "indexVersion" : 2,
                    "direction" : "backward",
                    "indexBounds" : {

                    },
                    "keysExamined" : 1,
                    "seeks" : 1,
                    "dupsTested" : 1,
                    "dupsDropped" : 0,
                    "seenInvalidated" : 0
                }
            }
        }
    }
},

但为什么mongo默认不使用索引,所以我使用提示强制使用下面的索引查询:

  

db.getCollection(" RecordInfo&#34)找到。({"的requestId":" 4513456313212313212aaaa"})暗示(" RequestId_text" ).explain(" executionStats"),它给了我这些,它不是那么好:

"executionStats" : {
    "executionSuccess" : true,
    "nReturned" : 1,
    "executionTimeMillis" : 93,
    "totalKeysExamined" : 49378,
    "totalDocsExamined" : 9998,
    "executionStages" : {
        "stage" : "FETCH",
        "filter" : {
            "RequestId" : {
                "$eq" : "4513456313212313212aaaa"
            }
        },
        "nReturned" : 1,
        "executionTimeMillisEstimate" : 91,
        "works" : 49379,
        "advanced" : 1,
        "needTime" : 49377,
        "needYield" : 0,
        "saveState" : 386,
        "restoreState" : 386,
        "isEOF" : 1,
        "invalidates" : 0,
        "docsExamined" : 9998,
        "alreadyHasObj" : 0,
        "inputStage" : {
            "stage" : "IXSCAN",
            "nReturned" : 9998,
            "executionTimeMillisEstimate" : 50,
            "works" : 49379,
            "advanced" : 9998,
            "needTime" : 39380,
            "needYield" : 0,
            "saveState" : 386,
            "restoreState" : 386,
            "isEOF" : 1,
            "invalidates" : 0,
            "keyPattern" : {
                "_fts" : "text",
                "_ftsx" : 1
            },
            "indexName" : "RequestId_text",
            "isMultiKey" : true,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 2,
            "direction" : "forward",
            "indexBounds" : {
                "_fts" : [
                    "[MinKey, MaxKey]"
                ],
                "_ftsx" : [
                    "[MinKey, MaxKey]"
                ]
            },
            "keysExamined" : 49378,
            "seeks" : 1,
            "dupsTested" : 49378,
            "dupsDropped" : 39380,
            "seenInvalidated" : 0
        }
    }
},

总结一下,我想使用索引或任何方法来查询快速字符串列的RequestId字段。但是,如果我强制使用索引,它似乎不适合我只会让我的查询更糟。但如果我以另一种方式使用索引,如下所示,我可以看到一个非常好的查询。但是我无法在这个字段上指明我想以这种方式查询。

  

db.getCollection(" RecordInfo&#34)找到({$文本:{$搜索:" 4513456313212313212aaaa"}})。解释(" executionStats&#34)

1 个答案:

答案 0 :(得分:0)

文本搜索仅适用于@ManagedBean(name="productManager") @SessionScoped public class ProductManager implements Serializable { private static final long serialVersionUID = 5874124596321L; private final Logger logger = Logger.getLogger("xaris.web.ProductManager"); private List<Nebulizer> nebulizers; private Nebulizer nebulizer; private String nebulizerModelName; @EJB private RequestBean request; public List<Nebulizer> getNebulizers() { try { this.nebulizers = request.getNebulizers(); }catch(Exception ex) { logger.warning("Could not get nebulizers"); } return nebulizers; } public String getNebulizerModelName() { try { nebulizerModelName = request.getNebulizerModelName(); } catch(Exception ex) { ogger.warning("Could not get specific model name."); } switch(nebulizerModelName) { case "Flo Eolo": nebulizerModelName = "flo_eolo"; break; case "Flo Miko": nebulizerModelName = "flo_miko"; break; default: nebulizerModelName = ""; break; } return nebulizerModelName; } } 查询运算符。

当您执行$text时,您正在运行查询以查找该字段的完全匹配。

从问题来看,您正在寻找的是一个常规索引,您可以使用db.getCollection("RecordInfo").find({"RequestId":"4513456313212313212aaaa"})创建

这将使您使用db.getCollection("RecordInfo").createIndex({"RequestId": 1})的查询失败(因为没有文本索引),但会使常规使用索引并避免使用$text