Skip to content
Browse files

added sort

  • Loading branch information...
1 parent e21293f commit 3e7bec7b432b4c9ebbb5e85bde5a800c5fecdf21 @karussell committed Oct 31, 2010
Showing with 117 additions and 58 deletions.
  1. +71 −48 web/js/spec/JSiiSpec.js
  2. +35 −2 web/js/src/JSii.js
  3. +10 −7 web/js/src/Server.js
  4. +1 −1 web/js/todo.txt
View
119 web/js/spec/JSiiSpec.js
@@ -7,12 +7,12 @@ describe("JSii", function() {
it("should do simple search", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- text : "blup"
- },{
- id : 2,
- text : "blup blap"
- }]);
+ id : 1,
+ text : "blup"
+ },{
+ id : 2,
+ text : "blup blap"
+ }]);
var resp = jsii.search('blup');
expect(resp.total).toBe(2);
@@ -38,18 +38,18 @@ describe("JSii", function() {
it("should do pagination", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- text : "blup"
- },{
- id : 2,
- text : "blup blap"
- },{
- id : 3,
- text : "blup blap three"
- },{
- id : 4,
- text : "blup blap five"
- }]);
+ id : 1,
+ text : "blup"
+ },{
+ id : 2,
+ text : "blup blap"
+ },{
+ id : 3,
+ text : "blup blap three"
+ },{
+ id : 4,
+ text : "blup blap five"
+ }]);
var resp = jsii.search('blup', 0, 2);
expect(resp.total).toBe(4);
@@ -93,12 +93,12 @@ describe("JSii", function() {
it("get all docs via * and paging", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- text : "test blap"
- },{
- id : 2,
- text : "test blap blup"
- }]);
+ id : 1,
+ text : "test blap"
+ },{
+ id : 2,
+ text : "test blap blup"
+ }]);
var res = jsii.search("*");
expect(res.total).toBe(2);
@@ -111,12 +111,12 @@ describe("JSii", function() {
it("should do a search with correct score", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- text : "test blap"
- },{
- id : 2,
- text : "test blap blup"
- }]);
+ id : 1,
+ text : "test blap"
+ },{
+ id : 2,
+ text : "test blap blup"
+ }]);
var res = jsii.search("test blap");
expect(res.total).toBe(2);
@@ -131,14 +131,14 @@ describe("JSii", function() {
it("should do filter query", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- user : "a",
- text : "b"
- },{
- id : 2,
- user : "b",
- text : "a"
- }]);
+ id : 1,
+ user : "a",
+ text : "b"
+ },{
+ id : 2,
+ user : "b",
+ text : "a"
+ }]);
var res = jsii.search("user:b");
expect(res.total).toBe(1);
@@ -148,16 +148,16 @@ describe("JSii", function() {
it("should weight terms via boost", function() {
var jsii = new JSii();
jsii.feedDocs([{
- id : 1,
- user : "a",
- text : "b"
- },{
- id : 2,
- user : "b",
- text : "a"
- }]);
-
- //TODO
+ id : 1,
+ user : "a",
+ text : "b"
+ },{
+ id : 2,
+ user : "b",
+ text : "a"
+ }]);
+
+ //TODO
});
it("should do query parsing", function() {
@@ -194,4 +194,27 @@ describe("JSii", function() {
var jsii = new JSii();
jsii.search("hello");
});
+
+ it("should create correct sort method", function() {
+ var jsii = new JSii();
+
+ var array = [{
+ id: 1
+ }, {
+ id: 6
+ }, {
+ id:0
+ }, {
+ id:7
+ }];
+ var res = array.sort(jsii.createSortMethod("id asc"));
+ expect(res[0].id).toBe(0);
+ expect(res[1].id).toBe(1);
+ expect(res[2].id).toBe(6);
+
+ res = array.sort(jsii.createSortMethod("id desc"));
+ expect(res[0].id).toBe(7);
+ expect(res[1].id).toBe(6);
+ expect(res[2].id).toBe(1);
+ });
});
View
37 web/js/src/JSii.js
@@ -94,7 +94,10 @@ JSii.prototype.feedDocs = function(newdocs) {
* docs: [doc1, doc2]
* }
*/
-JSii.prototype.search = function(query, start, rows) {
+JSii.prototype.search = function(query, start, rows, sortFunction) {
+ if(sortFunction == undefined)
+ sortFunction = this.sort;
+
if(start === undefined)
start = 0;
if(rows == undefined)
@@ -158,7 +161,7 @@ JSii.prototype.search = function(query, start, rows) {
this.setScore(resDocs, allTerms);
}
- resDocs.sort(this.sort);
+ resDocs.sort(sortFunction);
var totalDocs = resDocs.length;
// for pagination
var end = Math.min(start + rows, totalDocs);
@@ -208,6 +211,36 @@ JSii.prototype.sort = function(doc1, doc2) {
return 0;
};
+JSii.prototype.createSortMethod = function(sortString) {
+ if(sortString == undefined)
+ return this.sort;
+
+ var field = sortString.split(' ')[0];
+ var asc_desc= sortString.split(' ')[1];
+
+ if(asc_desc == "asc") {
+ return function(doc1, doc2) {
+ var v1 = doc1[field];
+ var v2 = doc2[field];
+ if(v1 > v2)
+ return 1;
+ else if(v1 < v2)
+ return -1;
+ return 0;
+ };
+ } else {
+ return function(doc1, doc2) {
+ var v1 = doc1[field];
+ var v2 = doc2[field];
+ if(v1 > v2)
+ return -1;
+ else if(v1 < v2)
+ return 1;
+ return 0;
+ };
+ }
+}
+
JSii.prototype.createEmptyResult = function() {
return {
total : 0,
View
17 web/js/src/Server.js
@@ -46,8 +46,8 @@ fs.open("src/pw.txt", "r", 0666, function(err, fd){
var options = {};
options.rows = 1000;
- options.fq = "lang:en";
- for(var i = 0; i < 10000; i+=options.rows) {
+ //options.fq = "lang:en";
+ for(var i = 0; i < 100000; i+=options.rows) {
options.start = i;
client.query(queryStr, options, callback);
}
@@ -92,13 +92,15 @@ function query(request, response) {
});
var params = url.parse(request.url, true).query;
- if(params == undefined || params.q == undefined)
- response.write('{info: "No q specified"}');
- else {
+ if(params == undefined || params.q == undefined) {
+ var err = "No param q specified";
+ response.write('{"responseHeader": {"status": 1, "QTime": 0, "error": "'+err+'"}, "response":{"numFound":0}}');
+ } else {
if(params.start == undefined)
params.start = 0;
var start = new Date().getTime();
- var result = engine.search(params.q, params.start, params.rows);
+ var sortMethod = engine.createSortMethod(params.sort);
+ var result = engine.search(params.q, params.start, params.rows, sortMethod);
console.log('RAM:' + process.memoryUsage().heapUsed / 1024 / 1024 + ' MB');
var time = new Date().getTime() - start;
response.write('{"responseHeader": {"status":0, "QTime": '+time);
@@ -127,6 +129,7 @@ function show404(req, res) {
res.writeHead(404, {
'Content-Type': 'text/plain'
});
- res.write('Use select?q=query to query the in-memory index or use update/ to feed it!');
+ var err = "Use select?q=query to query the in-memory index or use update/ to feed it!";
+ res.write('{"responseHeader": {"status": 1, "QTime": 0, "error": "'+err+'"}, "response":{"numFound":0}}');
res.end();
}
View
2 web/js/todo.txt
@@ -1,4 +1,4 @@
-sort against date
+sort against id (date)
measure RAM usage + qtime when load testing
regular refresh via F5 -> only first result differs

0 comments on commit 3e7bec7

Please sign in to comment.
Something went wrong with that request. Please try again.