Created
November 27, 2015 08:04
-
-
Save davibe/8986a865a96005fa2467 to your computer and use it in GitHub Desktop.
tests with mongodb full text search and mongoose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mongoose = require 'mongoose' | |
Schema = mongoose.Schema | |
Q = require 'q-extended' | |
Q.genrun -> | |
url = "mongodb://localhost/test" | |
yield Q.ninvoke(mongoose, "connect", url) | |
NoSchema = new Schema({}, { strict: false }) | |
NoSchema.index({ key: 'text'}); | |
Test = mongoose.model('Test', NoSchema) | |
yield Test.collection.dropAllIndexes(); | |
yield Test.ensureIndexes() | |
yield Test.remove({}) | |
yield Test.create({ key: "dog" }) | |
yield Test.create({ key: "dog cat" }) | |
yield Test.create({ key: "dog cat cat" }) | |
yield Test.create({ key: "dog cats cat" }) | |
# Test 1 | |
console.log 'Searching for: "cat"' | |
result = yield Test.find( | |
$text: | |
$search: "cat" | |
, | |
$score: | |
$meta: 'textScore' | |
_id: 0 | |
__v: 0 | |
).exec() | |
console.log result | |
# Notes | |
# 1. The score is higher when the word matches multiple times | |
# 2. There is no difference with singular/plural | |
# We could avoid 1. by de-duplicating terms but we don't have powerful | |
# singular/plural equivalence lookup as mongodb has | |
# Test 2 | |
console.log 'Searching for: "dog"' | |
result = yield Test.find( | |
$text: | |
$search: "dog" | |
, | |
$score: | |
$meta: 'textScore' | |
_id: 0 | |
__v: 0 | |
).exec() | |
console.log result | |
# Notes | |
# The score is higher if there are few 'extraneous' words | |
# It looks like we can't avoid this | |
yield mongoose.disconnect() | |
.catch (e) -> console.error e, e.stack.split('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment