Skip to content

Commit

Permalink
Move old-implementation specific search code
Browse files Browse the repository at this point in the history
- query-builder maybe can be re-used, but may be unnecessary; we'll see
  • Loading branch information
poltak committed Feb 23, 2018
1 parent ef4fbb3 commit 89c4f37
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 77 deletions.
77 changes: 2 additions & 75 deletions src/search/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import QueryBuilder from './query-builder'
import mapResultsToPouchDocs from './map-search-to-pouch'
import * as oldBackend from './search-index-old/api'
import * as newBackend from './search-index-new'

Expand Down Expand Up @@ -99,79 +97,8 @@ export async function grabExistingKeys(...args) {
// Searching & suggesting
//

export async function search({
query,
startDate,
endDate,
tags = [],
domains = [],
skip = 0,
limit = 10,
getTotalCount = false,
showOnlyBookmarks = false,
mapResultsFunc = mapResultsToPouchDocs,
}) {
query = query.trim() // Don't count whitespace searches

// Create SI query
const indexQuery = new QueryBuilder()
.searchTerm(query)
.filterTime({ startDate, endDate }, 'bookmark/')
.filterTime({ startDate, endDate }, 'visit/')
.filterTags(tags)
.filterDomains(domains)
.skipUntil(skip)
.limitUntil(limit)
.bookmarksFilter(showOnlyBookmarks)
.get()

// If there is only Bad Terms don't continue
if (indexQuery.isBadTerm) {
return {
docs: [],
resultsExhausted: true,
totalCount: getTotalCount ? 0 : undefined,
isBadTerm: true,
}
}

console.log('DEBUG: query', indexQuery)

// Get index results, filtering out any unexpectedly structured results
const {
results,
totalCount,
} = await (await getBackend()).search(indexQuery, {
count: getTotalCount,
})

// console.log('got results', results)

// Short-circuit if no results
if (!results.length) {
return {
docs: [],
resultsExhausted: true,
totalCount,
isBadTerm: false,
}
}

// Match the index results to data docs available in Pouch, consolidating meta docs
const docs = await mapResultsFunc(results, {
startDate,
endDate,
showOnlyBookmarks,
})

console.log('DEBUG: final UI results', docs)

return {
docs,
resultsExhausted: docs.length < limit,
totalCount,
isBadTerm: false,
}
export async function search(...args) {
return await (await getBackend()).search(...args)
}

export async function suggest(...args) {
Expand Down
75 changes: 74 additions & 1 deletion src/search/search-index-old/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import index from './index'
import mapResultsToPouchDocs from './map-search-to-pouch'
import QueryBuilder from '../query-builder'

export function hasData() {
return new Promise((resolve, reject) => {
Expand All @@ -18,6 +20,78 @@ export function hasData() {
})
}

export async function search({
query,
startDate,
endDate,
tags = [],
domains = [],
skip = 0,
limit = 10,
getTotalCount = false,
showOnlyBookmarks = false,
mapResultsFunc = mapResultsToPouchDocs,
}) {
query = query.trim() // Don't count whitespace searches

// Create SI query
const indexQuery = new QueryBuilder()
.searchTerm(query)
.filterTime({ startDate, endDate }, 'bookmark/')
.filterTime({ startDate, endDate }, 'visit/')
.filterTags(tags)
.filterDomains(domains)
.skipUntil(skip)
.limitUntil(limit)
.bookmarksFilter(showOnlyBookmarks)
.get()

// If there is only Bad Terms don't continue
if (indexQuery.isBadTerm) {
return {
docs: [],
resultsExhausted: true,
totalCount: getTotalCount ? 0 : undefined,
isBadTerm: true,
}
}

console.log('DEBUG: query', indexQuery)

// Get index results, filtering out any unexpectedly structured results
const { results, totalCount } = await index.searchConcurrent(indexQuery, {
count: getTotalCount,
})

// console.log('got results', results)

// Short-circuit if no results
if (!results.length) {
return {
docs: [],
resultsExhausted: true,
totalCount,
isBadTerm: false,
}
}

// Match the index results to data docs available in Pouch, consolidating meta docs
const docs = await mapResultsFunc(results, {
startDate,
endDate,
showOnlyBookmarks,
})

console.log('DEBUG: final UI results', docs)

return {
docs,
resultsExhausted: docs.length < limit,
totalCount,
isBadTerm: false,
}
}

export {
addPageConcurrent as addPage,
addPageTermsConcurrent as addPageTerms,
Expand All @@ -32,6 +106,5 @@ export {
removeBookmarkByUrl,
} from './bookmarks'
export { default as suggest } from './suggest'
export { searchConcurrent as search } from './search'
export { initSingleLookup, grabExistingKeys } from './util'
export { indexQueue } from './'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import reduce from 'lodash/fp/reduce'

import db, { bulkGetResultsToArray } from '../pouchdb'
import db, { bulkGetResultsToArray } from '../../pouchdb'
import { removeKeyType } from './util'

/**
Expand Down

0 comments on commit 89c4f37

Please sign in to comment.