Compare commits
10 Commits
e6ada45c3f
...
115197007d
Author | SHA1 | Date |
---|---|---|
Nicolas Wavrant | 115197007d | |
Nolan Lawson | ec2ce7b75f | |
Nolan Lawson | 78ccf2f7d6 | |
spicemix | 71704536de | |
Nolan Lawson | 66eed68e7c | |
Nolan Lawson | ec5e28fadb | |
Nolan Lawson | 747d533569 | |
Gerben | 360d2fd7d2 | |
Nolan Lawson | 372f4228ed | |
Nolan Lawson | a75ec1c9b5 |
|
@ -131,7 +131,7 @@ pouch.search({
|
|||
|
||||
### Document structure
|
||||
|
||||
Your document fields can be strings or arrays of strings. Use dots to separate deeply nested fields.
|
||||
Your document fields can be strings or arrays of strings. Use dots to separate deeply nested fields. Searching deeply inside arrays is supported.
|
||||
|
||||
```js
|
||||
var doc = {
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
17
lib/index.js
17
lib/index.js
|
@ -11,6 +11,7 @@ var utils = require('./pouch-utils');
|
|||
var lunr = require('lunr');
|
||||
var uniq = require('uniq');
|
||||
var Promise = utils.Promise;
|
||||
var stringify = require('json-stable-stringify');
|
||||
|
||||
var indexes = {};
|
||||
|
||||
|
@ -39,7 +40,11 @@ function getText(fieldBoost, doc) {
|
|||
} else { // "Enhance."
|
||||
text = doc;
|
||||
for (var i = 0, len = fieldBoost.deepField.length; i < len; i++) {
|
||||
text = text && text[fieldBoost.deepField[i]];
|
||||
if (Array.isArray(text)) {
|
||||
text = text.map(handleNestedObjectArrayItem(fieldBoost, fieldBoost.deepField.slice(i)));
|
||||
} else {
|
||||
text = text && text[fieldBoost.deepField[i]];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (text) {
|
||||
|
@ -52,6 +57,14 @@ function getText(fieldBoost, doc) {
|
|||
return text;
|
||||
}
|
||||
|
||||
function handleNestedObjectArrayItem(fieldBoost, deepField) {
|
||||
return function (one) {
|
||||
return getText(utils.extend({}, fieldBoost, {
|
||||
deepField: deepField
|
||||
}), one);
|
||||
};
|
||||
}
|
||||
|
||||
// map function that gets passed to map/reduce
|
||||
// emits two types of key/values - one for each token
|
||||
// and one for the field-len-norm
|
||||
|
@ -148,7 +161,7 @@ exports.search = utils.toPromise(function (opts, callback) {
|
|||
indexParams.filter = filter.toString();
|
||||
}
|
||||
|
||||
var persistedIndexName = 'search-' + utils.MD5(JSON.stringify(indexParams));
|
||||
var persistedIndexName = 'search-' + utils.MD5(stringify(indexParams));
|
||||
|
||||
var mapFun = createMapFunction(fieldBoosts, index, filter, pouch);
|
||||
|
||||
|
|
|
@ -77,11 +77,11 @@ exports.inherits = require('inherits');
|
|||
exports.Promise = Promise;
|
||||
|
||||
var crypto = require('crypto');
|
||||
var md5 = require('md5-jkmyers');
|
||||
var md5 = require('md5.js');
|
||||
exports.MD5 = function (string) {
|
||||
/* istanbul ignore if */
|
||||
if (process.browser) {
|
||||
return md5(string);
|
||||
return new md5().update(string).digest('hex');
|
||||
}
|
||||
return crypto.createHash('md5').update(string).digest('hex');
|
||||
};
|
||||
|
|
15
package.json
15
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "pouchdb-quick-search",
|
||||
"version": "1.1.3",
|
||||
"version": "1.3.0",
|
||||
"description": "PouchDB Quick Search - persisted full-text search for PouchDB",
|
||||
"main": "lib/index.js",
|
||||
"repository": {
|
||||
|
@ -21,11 +21,11 @@
|
|||
"url": "https://github.com/nolanlawson/pouchdb-quick-search/issues"
|
||||
},
|
||||
"scripts": {
|
||||
"test-node": "TEST_DB=testdb istanbul test ./node_modules/mocha/bin/_mocha test/test.js",
|
||||
"test-node": "istanbul test ./node_modules/mocha/bin/_mocha test/test.js",
|
||||
"test-browser": "./bin/test-browser.js",
|
||||
"jshint": "jshint -c .jshintrc lib/*.js test/test.js",
|
||||
"test": "npm run jshint && ./bin/run-test.sh",
|
||||
"build": "mkdir -p dist && browserify . -t es3ify -o dist/pouchdb.quick-search.js && npm run min",
|
||||
"jshint": "jshint -c .jshintrc lib test/test.js",
|
||||
"test": "npm run jshint && bash ./bin/run-test.sh",
|
||||
"build": "mkdirp dist && browserify . -p bundle-collapser/plugin -t es3ify -s PouchQuickSearch > dist/pouchdb.quick-search.js && npm run min",
|
||||
"min": "uglifyjs dist/pouchdb.quick-search.js -mc > dist/pouchdb.quick-search.min.js",
|
||||
"dev": "browserify test/test.js > test/test-bundle.js && npm run dev-server",
|
||||
"dev-server": "./bin/dev-server.js",
|
||||
|
@ -35,9 +35,10 @@
|
|||
"argsarray": "0.0.1",
|
||||
"es3ify": "^0.1.3",
|
||||
"inherits": "~2.0.1",
|
||||
"json-stable-stringify": "^1.0.1",
|
||||
"lie": "^2.6.0",
|
||||
"lunr": "0.7.1",
|
||||
"md5-jkmyers": "0.0.1",
|
||||
"md5.js": "^1.3.5",
|
||||
"pouchdb-extend": "^0.1.0",
|
||||
"pouchdb-mapreduce-no-ddocs": "^2.3.2",
|
||||
"pouchdb-promise": "5.4.4",
|
||||
|
@ -46,11 +47,13 @@
|
|||
"devDependencies": {
|
||||
"bluebird": "^1.0.7",
|
||||
"browserify": "^12.0.2",
|
||||
"bundle-collapser": "^1.2.1",
|
||||
"chai": "^3.5.0",
|
||||
"chai-as-promised": "^5.3.0",
|
||||
"http-server": "~0.5.5",
|
||||
"istanbul": "^0.2.7",
|
||||
"jshint": "~2.8.0",
|
||||
"mkdirp": "^0.5.1",
|
||||
"mocha": "^2.4.5",
|
||||
"phantomjs-prebuilt": "^2.1.7",
|
||||
"pouchdb-memory": "^1.1.0",
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
'use strict';
|
||||
|
||||
var docs = [
|
||||
{
|
||||
_id: '1',
|
||||
list: ['much', 'text', 'goes', 'in this array, you see']
|
||||
},
|
||||
{
|
||||
_id: '2',
|
||||
nested: {
|
||||
array: [{
|
||||
aField: 'something'
|
||||
}]
|
||||
}
|
||||
},
|
||||
{
|
||||
_id: '3',
|
||||
aNumber : 1
|
||||
},
|
||||
{
|
||||
_id: '4',
|
||||
invalid: null
|
||||
},
|
||||
{
|
||||
_id: '5',
|
||||
invalid: {}
|
||||
},
|
||||
{
|
||||
_id: '7',
|
||||
nested: {
|
||||
foo: null
|
||||
}
|
||||
},
|
||||
{
|
||||
_id: '8',
|
||||
nested: {
|
||||
array: null
|
||||
}
|
||||
},
|
||||
{
|
||||
_id: '9',
|
||||
nested: {
|
||||
array: []
|
||||
}
|
||||
},
|
||||
{
|
||||
_id: '10',
|
||||
nested: {
|
||||
array: [{
|
||||
aField: 'something else'
|
||||
},{
|
||||
aField: 'something different'
|
||||
},{
|
||||
aField: 'foobar'
|
||||
}]
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
module.exports = docs;
|
15
test/test.js
15
test/test.js
|
@ -28,7 +28,7 @@ var dbs;
|
|||
if (process.browser) {
|
||||
dbs = 'testdb' + Math.random();
|
||||
} else {
|
||||
dbs = process.env.TEST_DB;
|
||||
dbs = process.env.TEST_DB || 'testdb';
|
||||
}
|
||||
|
||||
dbs.split(',').forEach(function (db) {
|
||||
|
@ -44,6 +44,7 @@ var docs5 = require('./docs/test-docs-5');
|
|||
var docs6 = require('./docs/test-docs-6');
|
||||
var docs7 = require('./docs/test-docs-7');
|
||||
var docs8 = require('./docs/test-docs-8');
|
||||
var docs9 = require('./docs/test-docs-9');
|
||||
|
||||
function tests(dbName, dbType) {
|
||||
|
||||
|
@ -564,6 +565,18 @@ function tests(dbName, dbType) {
|
|||
ids.should.deep.equal(['2']);
|
||||
});
|
||||
});
|
||||
it('allows searching from an array of nested objects', function () {
|
||||
return db.bulkDocs({docs: docs9}).then(function () {
|
||||
var opts = {
|
||||
fields: ['nested.array.aField'],
|
||||
query: 'something'
|
||||
};
|
||||
return db.search(opts);
|
||||
}).then(function (res) {
|
||||
var ids = res.rows.map(function (x) { return x.id; }).sort().reverse();
|
||||
ids.should.deep.equal(['2', '10']);
|
||||
});
|
||||
});
|
||||
it('allows searching string arrays', function () {
|
||||
return db.bulkDocs({docs: docs5}).then(function () {
|
||||
var opts = {
|
||||
|
|
Loading…
Reference in New Issue