2015-12-19 3 views
3

У меня возникли проблемы с созданием индекса для поиска в pouchDB с использованием pouchdb-find. Я продолжаю получать ошибку couldn't find a usable index. try creating an index on: dateCreated. Make sure that only $eq, $gt, $gte, $lt, and $lte are used for the indexed fields. - но я не совсем уверен, что это значит или как ее исправить.Создание полезного индекса в PouchDB с помощью pouchdb-find

Вот мой код:

PouchDB.debug.enable('pouchdb:find'); 

var testDB = new PouchDB('testDB'); 

testDB.destroy('testDB').then(function() { 
    testDB = new PouchDB('testDB'); 
}).then(function() { 
    return testDB.bulkDocs([ 
     {siteTitle : 'Google Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc1'}, 
     {siteTitle : 'Google Mail', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'google.com', _id: 'doc2'}, 
     {siteTitle : 'Yahoo Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'yahoo.com', _id: 'doc3'}, 
     {siteTitle : 'Bing Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'bing.com', _id: 'doc4'}, 
     {siteTitle : 'Baidu Search', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'baidu.com', _id: 'doc5'}, 
     {siteTitle : 'Hacker News', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'ycombinator.com', _id: 'doc6'}, 
     {siteTitle : 'Mozilla foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'mozilla.org', _id: 'doc7'}, 
     {siteTitle : 'Android foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'android.com', _id: 'doc8'}, 
     {siteTitle : 'Wikipedia foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'wikipedia.org', _id: 'doc9'}, 
     {siteTitle : 'Twitter foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'twitter.com', _id: 'doc10'}, 
     {siteTitle : 'Facebook foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'facebook.com', _id: 'doc11'}, 
     {siteTitle : 'Reddit foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'reddit.com', _id: 'doc12'}, 
     {siteTitle : 'The Verge foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'theverge.com', _id: 'doc13'}, 
     {siteTitle : 'Dropbox foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'dropbox.com', _id: 'doc14'}, 
     {siteTitle : 'Microsoft foo', dateCreated: (~~(Math.random() * 100000) + 1), domain: 'microsoft.com', _id: 'doc15'} 
    ]); 
}).then(function() { 
    return testDB.createIndex({ 
     index: { 
      fields: ['dateCreated', 'domain'], 
      name: 'indexFoo' 
     } 
    }); 

}).then(function(result){ 
    console.log(result); 
}).then(function(){ 
    return testDB.getIndexes(); 
}).then(function(result) { 
    console.log(JSON.stringify(result)); 
}).then(function(result){ 
    return testDB.find({ 
    selector: { 
     dateCreated: {'$exists': true} 
    }, 
    use_index: 'indexFoo', 
    sort: [{dateCreated: 'desc'}] 
    }); 
}).then(function(result) { 
    console.log(result); 
}).then(function(result) { 
    console.log(result); 
}).catch(function (err) { 
    console.error(err); 
    console.log(err.message); 
}); 

А вот живая версия: https://jsbin.com/qimulecezi/8/edit?js,console

Насколько я могу сказать, что я создания индекса так же, как это показано здесь: https://github.com/nolanlawson/pouchdb-find#dbcreateindexindex--callback и мое использование find с сортировкой довольно много дословно: https://github.com/nolanlawson/pouchdb-find#sorting

ответ

2

Я думал, что должен опубликовать ответ здесь, поскольку нашел его в другом месте.

nolanlawson ответил на мой вопрос о ИРЦ, он сказал:

$ существует не делать то, что вы думаете, что делает

Вы хотите $ Gt: нуль вместо этого. Извините, это сбивает с толку. Я не проектировал API

0

Я попал в ту же ошибку «непригодный для использования индекс» в PouchDB.

В моем случае я создал индекс, а затем выполнил команду db.find() и ударил эту ошибку.

Чтобы решить эту проблему, я изменил свой код, чтобы выполнить поиск, и с помощью вызова обратного вызова для вызова другого метода выполнить поиск.

function search() { 

     var tmp = new Date().toISOString(); 
     //console.log('new Date() ' + tmp); 

     db.createIndex({ 
      index: { 
       fields: ['title'] 
      } 
     }).then(function (result) { 
      console.log('createIndex ' + result); 

      findIt(); // Run Find 
     }).catch(function (err) { 
      console.log('createIndex ' + err); 
     }); 
    } 

function findIt() 
    { 
     db.find({ 
      selector: { title: { $eq: 'ABC2' } }, 
      sort: ['title'] 
      //,  use_index: 'indexFoo' 
     }).then(function (result) {  
      //console.log('find - result ' + result); 
      console.log(JSON.stringify(result)); 

     // console.log('JSON' + JSON.stringify(result, undefined, 2)); 

      console.log('Title: ' + result.docs[0].title); 

     }).catch(function (err) { 
      console.log('find - err ' + err); 
     }); 
    } 
Смежные вопросы