Skip to content

Commit

Permalink
fixed setting counts #2
Browse files Browse the repository at this point in the history
  • Loading branch information
phillro committed Jan 2, 2013
1 parent bf0376c commit 175f60e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion example/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ cli.main(function (args, options) {

var Ex = mongoose.model('Example', ExampleSchema);

Ex.search({'nested.n1':'nested'}, {}, {}, function (err, res) {
Ex.search({'nested.n1':'nested'},{"name":"This is a sample document name"}, {}, {}, function (err, res) {
if (err) {
console.log(err);
} else {
Expand Down
23 changes: 9 additions & 14 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ module.exports = exports = function fulltext(schema, options) {
fields[fname] = fields[fname] ? fields[fname] : [];
fields[fname].push(f)
}
if (typeof schemaTree[f] == 'object') {
// var tmp = getIndexFields(schemaTree[f]);
// fields = extend(fields, tmp);
}
}
return fields;
}

//var cleanTree = getCleanTree(schema.tree, schema.paths, '')
//indexFields2 = getIndexFields(cleanTree);


for (var path in schema.paths) {
if(schema.paths[path].options&&schema.paths[path].options.fulltext){
var searchField = schema.paths[path].options.searchfield ? schema.paths[path].options.searchfield : path;
Expand All @@ -51,14 +43,14 @@ module.exports = exports = function fulltext(schema, options) {

var fieldDef = {};
var indexPaths = [];

for (var f in indexFields) {
fieldDef[f] = {'ngrams':[
{'ngram':[
fieldDef[f] = {
'counts':{'type':mongoose.Schema.Types.Mixed},
'ngrams':[{'ngram':[
{type:String, index:true, sparse:true}
]}
]}
indexPaths.push([indexfieldname, f, 'ngrams', 'ngram'].join('.'))
indexPaths.push([indexfieldname, f, 'ngrams', 'ngram'].join('.'));
}
var postingDoc = {};
postingDoc[indexfieldname] = fieldDef;
Expand Down Expand Up @@ -102,10 +94,13 @@ module.exports = exports = function fulltext(schema, options) {
cb();
})

schema.statics.search = function (searchQuery, fields, options, cb) {
schema.statics.search = function (searchQuery, mongoQuery, fields, options, cb) {
var options = options || {};
var field = Object.keys(searchQuery)[0];
var query = search.getMongoQuery(searchQuery[field], field);
var query = search.getMongoQuery(searchQuery[field], field, mongoQuery);

console.log(JSON.stringify(query));

this.find(query, fields, options, function (err, docs) {
if (err) {
cb(err);
Expand Down
12 changes: 10 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ Search.prototype.addIndex = function (document, words, field) {
document[this.key] = {};
}
//document[this.key][field] = this.getIndexObj(words);
document.set(this.key+'.'+field,this.getIndexObj(words));
var indexObj = this.getIndexObj(words)
document.set(this.key+'.'+field,indexObj);
document.markModified(this.key);

// document.markModified(this.key+'.'+field+'.counts');
return document;
}

Expand All @@ -40,7 +43,8 @@ Search.prototype.ngrams = function (tokens) {
return ngrams;
}

Search.prototype.getMongoQuery = function (str, field) {
Search.prototype.getMongoQuery = function (str, field,mongoQuery) {
var joinQuery=false;
var searchField = this.key + '.' + field + '.ngrams.ngram';
var tokens = str.tokenizeAndPhoneticize();
var ngrams = this.ngrams(tokens);
Expand All @@ -58,6 +62,10 @@ Search.prototype.getMongoQuery = function (str, field) {
}
query['$or'].push(f);
}
if(mongoQuery){
mongoQuery['$or']=query['$or']
query=mongoQuery;
}
return query;
}

Expand Down

0 comments on commit 175f60e

Please sign in to comment.