Use index even for non-logical operators
When selecting an index for a query don't reject the index if the field
found in the selector is non-logical. Rather use the index with the
default start key. Using the index will automatically
narrow down the list of documents so that is better than using
_all_docs. This fix helps pouchdb-find match CouchDB Mango on index selection.
diff --git a/packages/node_modules/pouchdb-find/src/adapters/local/find/query-planner.js b/packages/node_modules/pouchdb-find/src/adapters/local/find/query-planner.js
index be2dd72..0072725 100644
--- a/packages/node_modules/pouchdb-find/src/adapters/local/find/query-planner.js
+++ b/packages/node_modules/pouchdb-find/src/adapters/local/find/query-planner.js
@@ -147,14 +147,6 @@
return true;
}
- var hasLogicalOperator = Object.keys(matcher).some(function (matcherKey) {
- return !(isNonLogicalMatcher(matcherKey));
- });
-
- if (!hasLogicalOperator) {
- return false;
- }
-
var isInvalidNe = Object.keys(matcher).length === 1 &&
getKey(matcher) === '$ne';
@@ -276,6 +268,10 @@
inclusive_start: false
};
}
+
+ return {
+ startkey: COLLATE_LO
+ };
}
function getSingleFieldCoreQueryPlan(selector, index) {
@@ -293,7 +289,6 @@
if (isNonLogicalMatcher(userOperator)) {
inMemoryFields.push(field);
- return;
}
var userValue = matcher[userOperator];
diff --git a/tests/find/test-suite-1/test.array.js b/tests/find/test-suite-1/test.array.js
index a6268b7..db4041f 100644
--- a/tests/find/test-suite-1/test.array.js
+++ b/tests/find/test-suite-1/test.array.js
@@ -24,14 +24,16 @@
describe('$in', function () {
it('should return docs match single value in array', function () {
- var db = context.db;
- return db.find({
+ var selector = {
selector: {
favorites: {
$in: ["Mario"]
}
},
- }).then(function (resp) {
+ };
+ var db = context.db;
+ return db.find(selector)
+ .then(function (resp) {
var docs = resp.docs.map(function (doc) {
delete doc._rev;
return doc;
@@ -41,20 +43,17 @@
{ name: 'James', _id: 'james', favorites: ['Mario', 'Pokemon'], age: 20},
{ name: 'William', _id: 'william', favorites: ['Mario'], age: 23 }
]);
+
+ return db.explain(selector);
+ })
+ .then(function (resp) {
+ resp.index.name.should.deep.equal('_all_docs');
});
});
-
- it('should use default index due to non-logical operators', function () {
+
+ it('should use name index', function () {
var db = context.db;
- var index = {
- "index": {
- "fields": ["name", "age"]
- },
- "type": "json"
- };
- return db.createIndex(index)
- .then(function () {
- return db.find({
+ var selector = {
selector: {
name: {
$in: ['James', 'Link']
@@ -63,7 +62,11 @@
$gt: 21
}
},
- });
+ };
+ return db.explain(selector)
+ .then(function (resp) {
+ resp.index.name.should.deep.equal('name-index');
+ return db.find(selector);
}).then(function (resp) {
var docs = resp.docs.map(function (doc) {
delete doc._rev;