Skip to content

Commit

Permalink
added options to exclude certain base documents
Browse files Browse the repository at this point in the history
  • Loading branch information
Crenshinibon committed Sep 27, 2013
1 parent b4eed44 commit 477ad44
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 33 deletions.
28 changes: 21 additions & 7 deletions packages/spomet/client.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Spomet.Search
sort: search.get 'search-sort'
offset: search.get 'search-offset'
limit: search.get 'search-limit'
excludes: search.get 'excludes'
search.subHandle = Meteor.subscribe 'search-results', opts

setCurrentPhrase: (phrase) =>
Expand Down Expand Up @@ -90,14 +91,25 @@ class Spomet.Search
isSearching: () =>
@get 'searching'

getIndexNames: () =>
@get 'index-names'

setIndexNames: (indexNames) =>
@set 'index-names', indexNames

getExcludes: () =>
@get 'excludes'

setExcludes: (excludes) =>
@set 'excludes', excludes

find: (phrase) =>
if phrase? and phrase.length > 0
@clearSearch phrase
@createIntermediaryResults phrase

search = @
Meteor.call 'spometFind', phrase, () ->
Meteor.call 'spometFind', phrase, @getIndexNames(), () ->
search.setSearching null


Expand Down Expand Up @@ -142,10 +154,12 @@ class Spomet.Search
results: () =>
phrase = @getCurrentPhrase()
if phrase?
[selector, opts] = Spomet.buildSearchQuery phrase,
@getSort(),
@getOffset(),
@getLimit()

Spomet.Searches.find selector, opts
opts =
phrase: phrase
sort: @getSort()
offset: @getOffset()
limit: @getLimit()
excludes: @getExcludes()
[selector, qOpts] = Spomet.buildSearchQuery opts
Spomet.Searches.find selector, qOpts

9 changes: 4 additions & 5 deletions packages/spomet/indexes/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,11 @@ Index.remove = (docId, indexName, remToken) ->
$inc: {documentsCount: -1}


Index.find = (phrase, callback, options) ->
unless options?.indexes?
unless options? then options = {}
options.indexes = Spomet.options.indexes
Index.find = (phrase, callback, indexes) ->
unless indexes?
indexes = Spomet.options.indexes

tokenizers = options.indexes.map (index) -> new index.Tokenizer
tokenizers = indexes.map (index) -> new index.Tokenizer
phrase.split('').forEach (c, i) ->
tokenizers.forEach (t) ->
t.parseCharacter c, i
Expand Down
22 changes: 15 additions & 7 deletions packages/spomet/server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,22 @@ updateSearchDoc = (current, phraseHash, doc, hits, score) ->
subDocs: subDocs
interim: false

Spomet.find = (phrase) ->
Spomet.find = (phrase, options) ->
phraseHash = Spomet.phraseHash phrase
cur = Spomet.Searches.find {phraseHash: phraseHash, interim: false}
unless cur.count() is 0
Spomet.Searches.remove {phraseHash: phraseHash, interim: true}
{phrase: phrase, hash: phraseHash, cached: true}
else
docs = {}
Spomet.Index.find phrase, (docId, hits, score) ->
findCallback = (docId, hits, score) ->
unless docs[docId]?
docs[docId] = Spomet.Documents.collection.findOne {docId: docId}

current = createSearchDoc phraseHash, docs[docId]
updateSearchDoc current, phraseHash, docs[docId], hits, score

Spomet.Index.find phrase, findCallback, options
{phrase: phrase, hash: phraseHash, cached: false}

cleanupSearches = () ->
Expand Down Expand Up @@ -91,8 +93,13 @@ Spomet.reset = () ->
Index.reset()

Meteor.methods
spometFind: (phrase) ->
Spomet.find phrase
spometFind: (phrase, indexNames) ->
indexes = Spomet.options.indexes
if indexNames?
indexes = Spomet.options.indexes.filter (e) ->
e.name in indexNames

Spomet.find phrase, indexes
spometAdd: (findable) ->
Spomet.add findable
spometRemove: (findable) ->
Expand All @@ -116,7 +123,7 @@ Meteor.publish 'documents', () ->
'findable.version': 1

#should be extended
stopWords = ['there','this','that','them','then','and','the','any','all','other','und','ich','wir','sie','als']
stopWords = ['there','not','this','that','them','then','and','the','any','all','other','und','ich','wir','sie','als']
Meteor.publish 'common-terms', () ->
Spomet.CommonTerms.find {tlength: {$gt: 2}, token: {$nin: stopWords}},
sort:
Expand All @@ -133,5 +140,6 @@ Meteor.publish 'common-terms', () ->

Meteor.publish 'search-results', (opts) ->
if opts?.phrase?
[selector, queryOpts] = Spomet.buildSearchQuery opts.phrase, opts.sort, opts.offset, opts.limit
Spomet.Searches.find selector, queryOpts
[selector, queryOpts] = Spomet.buildSearchQuery opts
Spomet.Searches.find selector, queryOpts

34 changes: 20 additions & 14 deletions packages/spomet/shared.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,31 @@ Spomet.options =
Spomet.phraseHash = (phrase) ->
CryptoJS.MD5(phrase).toString()

Spomet.buildSearchQuery = (phrase, sort, offset, limit) ->
phraseHash = CryptoJS.MD5(phrase).toString()
Spomet.buildSearchQuery = (options) ->
phraseHash = CryptoJS.MD5(options.phrase).toString()
selector = {phraseHash: phraseHash}

unless sort?
sort = Spomet.options.sort
if options.excludes?
selector[base] = {$nin: options.excludes}

opts = {}
opts.sort = {}
opts.sort[sort.field] = sort.direction
unless options.sort?
options.sort = Spomet.options.sort

if offset?
if sort.direction is -1
selector[sort.field] = {$lte: offset}
else if sort.direction is 1
selector[sort.field] = {$gte: offset}
qOpts = {}
qOpts.sort = {}
qOpts.sort[options.sort.field] = options.sort.direction

unless limit? then opts.limit = Spomet.options.resultsCount
[selector, opts]
if options.offset?
if options.sort.direction is -1
selector[options.sort.field] = {$lte: options.offset}
else if options.sort.direction is 1
selector[options.sort.field] = {$gte: options.offset}

if options.limit?
qOpts.limit = options.limit
else
qOpts.limit = Spomet.options.resultsCount
[selector, qOpts]

class Spomet.Findable
constructor: (@text, @path, @base, @type, @version) ->
Expand Down

0 comments on commit 477ad44

Please sign in to comment.