weixsong / elasticlunr.js

Based on lunr.js, but more flexible and customized.
http://elasticlunr.com
MIT License
2.04k stars 149 forks source link

Getting an error of TypeError: Cannot call method 'getDocFreq' of undefined #2

Closed Darkle closed 9 years ago

Darkle commented 9 years ago

Hi, I'm getting an odd error when I try to run the example from the elasticlunr documentation in node.js. Running the following code in node:

var http = require('http');

var elasticlunr = require("elasticlunr");

var server = http.createServer();

var index = elasticlunr(function () {
    this.addField('title');
    this.addField('body');
    this.setRef('id');
});

var doc1 = {
    "id": 1,
    "title": "Oracle released its latest database Oracle 12g",
    "body": "Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company and lead to a nice profit report of annual year."
}

var doc2 = {
    "id": 2,
    "title": "Oracle released its profit report of 2015",
    "body": "As expected, Oracle released its profit report of 2015, during the good sales of database and hardware, Oracle's profit of 2015 reached 12.5 Billion."
}

index.addDoc(doc1);
index.addDoc(doc2);

index.search("Oracle database profit", {
  fields: {
      title: {boost: 2},
      body: {boost: 1}
  }
});

server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function(){
  var addr = server.address();
  console.log("Chat server listening at", addr.address + ":" + addr.port);
});

I get the following error message:

/home/ubuntu/workspace/node_modules/elasticlunr/elasticlunr.js:731                                                                         
  var df = this.index[field].getDocFreq(term);                                                                                             
                             ^                                                                                                             
TypeError: Cannot call method 'getDocFreq' of undefined                                                                                    
    at elasticlunr.Index.idf (/home/ubuntu/workspace/node_modules/elasticlunr/elasticlunr.js:731:30)                                       
    at elasticlunr.Index.computeSquaredWeight (/home/ubuntu/workspace/node_modules/elasticlunr/elasticlunr.js:898:22)                      
    at Array.forEach (native)                                                                                                              
    at elasticlunr.Index.computeSquaredWeight (/home/ubuntu/workspace/node_modules/elasticlunr/elasticlunr.js:894:15)                      
    at elasticlunr.Index.search (/home/ubuntu/workspace/node_modules/elasticlunr/elasticlunr.js:779:28)                                    
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:29:7)                                                                          
    at Module._compile (module.js:456:26)                                                                                                  
    at Object.Module._extensions..js (module.js:474:10)                                                                                    
    at Module.load (module.js:356:32)                                                                                                      
    at Function.Module._load (module.js:312:12) 

Any help would be appreciated.

weixsong commented 9 years ago

Hi, @Darkle , thanks very for reporting this issue. I've fixed this issue by v0.7.2.