From eb824c3822f3d5d1c097001a9e096480b23f298c Mon Sep 17 00:00:00 2001 From: Hexagon Date: Sun, 22 Nov 2015 21:47:58 +0100 Subject: [PATCH] Version bump, minor refactor --- lib/rankers.js | 70 +++++++++++++++++++++++++------------------------- package.json | 2 +- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/lib/rankers.js b/lib/rankers.js index bb781f6..812364a 100644 --- a/lib/rankers.js +++ b/lib/rankers.js @@ -62,7 +62,23 @@ function standard( options ) { directMatches, partialMatches, - docExists; + currentDocument, + + // Helper function to create or get a document result object + getResultObj = function (docId) { + var doc; + if ( !(doc = documentResults[docId]) ) { + doc = documentResults[docId] = { + documentId: docId, + totalWeight: 0, + partialMatches: 0, + directMatches: 0, + directMatchedExpressions: {}, + matchedExpressions: {} + }; + } + return doc; + }; // Find all documents for ( var key = 0; key < resultSet.expressions.length; key++ ) { @@ -72,49 +88,33 @@ function standard( options ) { directMatches = currentExpression.hits.direct; partialMatches = currentExpression.hits.partial; + // Create result object + if (directMatches !== undefined ) { for( i = 0; i < directMatches.length; i+=3 ) { - currentMatchWeight = options.directHit * options.fields[directMatches[i+1]]; - if ( docExists = documentResults[directMatches[i]] ) { - docExists.totalWeight += currentMatchWeight; - docExists.directMatches += directMatches[i+2]; - docExists.directMatchedExpressions[key] = true; - docExists.matchedExpressions[key] = true; - } else { - documentResults[directMatches[i]] = { - documentId: directMatches[i], - totalWeight: currentMatchWeight, - directMatches: directMatches[i+2], - partialMatches: 0, - directMatchedExpressions: {}, - matchedExpressions: {}, - }; - documentResults[directMatches[i]].directMatchedExpressions[key] = true; - documentResults[directMatches[i]].matchedExpressions[key] = true; - } + + currentDocument = getResultObj (directMatches[i]); + + currentDocument.totalWeight += options.directHit * options.fields[directMatches[i+1]]; + currentDocument.directMatches += directMatches[i+2]; + currentDocument.directMatchedExpressions[key] = true; + currentDocument.matchedExpressions[key] = true; + } } if (partialMatches !== undefined ) { for( i = 0; i < partialMatches.length; i+=3 ) { - currentMatchWeight = options.partialHit * options.fields[partialMatches[i+1]]; - if ( docExists = documentResults[partialMatches[i]] ) { - docExists.totalWeight += currentMatchWeight; - docExists.partialMatches += partialMatches[i+2]; - docExists.matchedExpressions[key] = true; - } else { - documentResults[partialMatches[i]] = { - documentId: partialMatches[i], - totalWeight: currentMatchWeight, - partialMatches: partialMatches[i+2], - directMatches: 0, - directMatchedExpressions: {}, - matchedExpressions: {} - }; - documentResults[partialMatches[i]].matchedExpressions[key] = true; - } + + currentDocument = getResultObj (partialMatches[i]); + + currentDocument.totalWeight += options.partialHit * options.fields[partialMatches[i+1]]; + currentDocument.partialMatches += partialMatches[i+2]; + currentDocument.matchedExpressions[key] = true; + } } + }; // Convert document results from object to array (to be sortable) diff --git a/package.json b/package.json index 68e2725..5f7c235 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "thinker-fts", - "version": "1.0.3", + "version": "1.0.4", "description": "Javascript/Node.js in-memory full text search engine.", "author": "Hexagon ", "contributors": [{