Can a $text search perform a partial match

后端 未结 2 1323
逝去的感伤
逝去的感伤 2021-01-12 04:29

Ok, so I\'m very confused by this behavior. It seems inconsistent and strange, especially since I\'ve read that Mongo isn\'t supposed to support partial search terms in full

相关标签:
2条回答
  • 2021-01-12 04:38

    You can do partial searching in mongoose database using mongoose external library called mongoose-fuzzy-search where the search text is broken in various anagrams. for more information visit this link

    User.fuzzySearch('jo').sort({ age: -1 }).exec(function (err, users) {
          console.error(err);
          console.log(users);
    });
    
    0 讨论(0)
  • 2021-01-12 04:56

    MongoDB $text searches do not support partial matching. MongoDB allows text search queries on string content with support for case insensitivity, delimiters, stop words and stemming. And the terms in your search string are, by default, OR'ed.

    Taking your (very useful :) examples one by one:

    SINGLE TERM, PARTIAL

    // returns nothing because there is no world word with the value `Crai` in your
    // text index and there is no whole word for which `Crai` is a recognised stem
    db.submissions.find({"$text":{"$search":"\"Crai\""}})
    

    MULTIPLE TERMS, COMPLETE

    // returns the document because it contains all of these words
    // note in the text index Dr. Bob is not a single entry since "." is a delimiter
    db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bob\""}})
    

    MULTIPLE TERMS, ONE PARTIAL

    // returns the document because it contains the whole word "Craig" and it 
    // contains the whole word "Dr" 
    db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})
    

    MULTIPLE TERMS, BOTH PARTIAL

    // returns the document because it contains the whole word "Dr"
    db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})
    

    Bear in mind that the $search string is ...

    A string of terms that MongoDB parses and uses to query the text index. MongoDB performs a logical OR search of the terms unless specified as a phrase.

    So, if at least one term in your $search string matches then MongoDB matches that document.

    To verify this behaviour, if you edit your document changing Dr. Bob to DrBob then the following queries will return no documents:

    db.submissions.find({"$text":{"$search":"\"Craig\" \"Dr. Bo\""}})
    db.submissions.find({"$text":{"$search":"\"Crai\" \"Dr. Bo\""}})
    

    These now return no matches because Dr is no longer a whole word in your text index because it is not followed by the . delimiter.

    0 讨论(0)
提交回复
热议问题