Skip to content

Commit

Permalink
WIP demo and FACETs no longer include empty buckets by default
Browse files Browse the repository at this point in the history
  • Loading branch information
fergiemcdowall committed Oct 26, 2023
1 parent f5f6220 commit 503f488
Show file tree
Hide file tree
Showing 14 changed files with 171 additions and 68 deletions.
8 changes: 4 additions & 4 deletions demo/src/search-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ autocomplete({
},
container: '#searchbox',
placeholder: 'Search for pictures of nature',
getSources({ query }) {
getSources ({ query }) {
return [
{
sourceId: 'dictionary',
getItems({ query }) {
getItems ({ query }) {
return si.DICTIONARY(query).then(res => [
...res.map(item => ({
label: item,
Expand All @@ -129,12 +129,12 @@ autocomplete({
{ label: 'clear', value: '' }
])
},
getItemUrl({ item }) {
getItemUrl ({ item }) {
console.log('fetching ... ' + item.label)
return '?q=' + item.label
},
templates: {
item({ item, html }) {
item ({ item, html }) {
return html`<a href="?q=${item.value}">${item.label}</a>`
}
}
Expand Down
6 changes: 1 addition & 5 deletions demo2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@
}
</style>

<script src="./lib/search-index.js"></script>
<script src="./lib/stopwords.js"></script>
<script src="./lib/ui.js"></script>
<script src="./lib/autocomplete.js"></script>
<script src="./src/app.js"></script>
<script src="./src/app.js" type="module"></script>
<title>Search-index demo</title>
</head>
<body>
Expand Down
2 changes: 1 addition & 1 deletion demo2/lib/autocomplete.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const autocomplete = async (inp, DICTIONARY, search) => {
export const autocomplete = async (inp, DICTIONARY, search) => {
/*the autocomplete function takes two arguments,
the text field element and an array of possible autocompleted values:*/
var currentFocus
Expand Down
121 changes: 110 additions & 11 deletions demo2/lib/stopwords.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,112 @@
// eslint-disable-next-line no-unused-vars
const stopwords = [
'about', 'after', 'all', 'also', 'am', 'an', 'and', 'another', 'any', 'are', 'as', 'at', 'be',
'because', 'been', 'before', 'being', 'between', 'both', 'but', 'by', 'came', 'can',
'come', 'could', 'did', 'do', 'each', 'for', 'from', 'get', 'got', 'has', 'had',
'he', 'have', 'her', 'here', 'him', 'himself', 'his', 'how', 'if', 'in', 'into',
'is', 'it', 'like', 'make', 'many', 'me', 'might', 'more', 'most', 'much', 'must',
'my', 'never', 'now', 'of', 'on', 'only', 'or', 'other', 'our', 'out', 'over',
'said', 'same', 'see', 'should', 'since', 'some', 'still', 'such', 'take', 'than',
'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those',
'through', 'to', 'too', 'under', 'up', 'very', 'was', 'way', 'we', 'well', 'were',
'what', 'where', 'which', 'while', 'who', 'with', 'would', 'you', 'your', 'a', 'i'
export const stopwords = [
'about',
'after',
'all',
'also',
'am',
'an',
'and',
'another',
'any',
'are',
'as',
'at',
'be',
'because',
'been',
'before',
'being',
'between',
'both',
'but',
'by',
'came',
'can',
'come',
'could',
'did',
'do',
'each',
'for',
'from',
'get',
'got',
'has',
'had',
'he',
'have',
'her',
'here',
'him',
'himself',
'his',
'how',
'if',
'in',
'into',
'is',
'it',
'like',
'make',
'many',
'me',
'might',
'more',
'most',
'much',
'must',
'my',
'never',
'now',
'of',
'on',
'only',
'or',
'other',
'our',
'out',
'over',
'said',
'same',
'see',
'should',
'since',
'some',
'still',
'such',
'take',
'than',
'that',
'the',
'their',
'them',
'then',
'there',
'these',
'they',
'this',
'those',
'through',
'to',
'too',
'under',
'up',
'very',
'was',
'way',
'we',
'well',
'were',
'what',
'where',
'which',
'while',
'who',
'with',
'would',
'you',
'your',
'a',
'i'
]
21 changes: 17 additions & 4 deletions demo2/lib/ui.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// should be eventually bundled in with search-index
import { autocomplete } from '/lib/autocomplete.js'

const ui = ({
export const ui = ({
index = null,
count = {},
hits = {},
Expand Down Expand Up @@ -37,6 +38,9 @@ const ui = ({

refiners = refiners.map(refiner => ({
el: document.getElementById(refiner.elementId),
sort: (a, b) => {
a.VALUE.localeCompare(b.VALUE)
},
...refiner
}))

Expand All @@ -56,21 +60,25 @@ const ui = ({
]

const searchQuery = () => {
// debugger
return [
const q = [
[
...searchInput.el.value.split(/\s+/).filter(item => item),
...activeFilters
],
{
FACETS: [
{
FIELD: ['month', 'year']
FIELD: ['month']
},
{
FIELD: ['year']
}
],
DOCUMENTS: true
}
]
console.log(JSON.stringify(q, null, 2))
return q
}

const search = () =>
Expand All @@ -82,6 +90,10 @@ const ui = ({
// query: q,
result // TODO: should this be nested like this?
}))
.then(res => {
console.log(res)
return res
})
.then(displaySearch)

const displaySearch = res => {
Expand All @@ -100,6 +112,7 @@ const ui = ({
refinerTitleTemplate(refiner.title) +
facets
.filter(item => item.FIELD == refiner.field)
.sort(refiner.sort)
.reduce(
(acc, { VALUE, _id }) =>
acc +
Expand Down
31 changes: 22 additions & 9 deletions demo2/src/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
const si = new SearchIndex.SearchIndex({
import { stopwords } from '/lib/stopwords.js'
import { ui } from '/lib/ui.js'
import { SearchIndex } from '/lib/search-index-esm-5.0.0-rc1.js'

const si = new SearchIndex({
name: 'mySearchIndex',
stopwords
})

fetch('data/EarthPorn-top-search-index.json')
.then(res => res.json())
.then(si.IMPORT)
.catch(e => console.error('problem with import'))

window.onload = function () {
Promise.all([
fetch('data/EarthPorn-top-search-index.json')
.then(res => res.json())
.then(si.IMPORT),
new Promise(res => (window.onload = () => res()))
]).then(() =>
ui({
index: si,
count: {
Expand All @@ -26,8 +30,17 @@ window.onload = function () {
},
refiners: [
{ elementId: 'year-refiner', title: 'YEAR', field: 'year' },
{ elementId: 'month-refiner', title: 'MONTH', field: 'month' }
{
elementId: 'month-refiner',
title: 'MONTH',
field: 'month',
sort: (a, b) => {
const monthNumber = month =>
new Date(Date.parse(month + ' 1, 2012')).getMonth() + 1
return monthNumber(a.VALUE) - monthNumber(b.VALUE)
}
}
],
searchInput: { elementId: 'searchbox' }
})
}
)
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"world-bank-dataset": "^1.0.0"
},
"dependencies": {
"fergies-inverted-index": "13.0.0-rc.4",
"fergies-inverted-index": "13.0.0-rc.5",
"lru-cache": "10.0.0",
"ngraminator": "3.0.2",
"p-queue": "7.3.4",
Expand Down
4 changes: 2 additions & 2 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { Writer } from './write.js'
import { validateVersion } from './util.js'

export class Main {
constructor(ops = {}) {
constructor (ops = {}) {
ops = {
cacheLength: 1000,
caseSensitive: false,
docExistsSpace: 'DOC_RAW',
idGenerator: (function* generateId() {
idGenerator: (function * generateId () {
let i = 0
while (true) {
yield Date.now() + '-' + i++
Expand Down
2 changes: 1 addition & 1 deletion src/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class Reader {
options.BUCKETS
? this.#ii.BUCKETS(...options.BUCKETS).then(bkts =>
Object.assign(result, {
BUCKETS: this.#ii.AGGREGATION_FILTER(bkts, result.RESULT)
BUCKETS: this.#ii.AGGREGATION_FILTER(bkts, result.RESULT, false)
})
)
: result
Expand Down
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const packageVersion = "5.0.0-rc1"
export const packageVersion = '5.0.0-rc1'
2 changes: 0 additions & 2 deletions test/src/QUERY-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ test('can add data', t => {
brand: 'Volvo'
}
]

t.plan(1)
global[indexName].PUT(data).then(t.pass)
})
Expand Down Expand Up @@ -196,7 +195,6 @@ test('QUERY with FACETS', t => {
RESULT_LENGTH: 2,
FACETS: [
{ FIELD: 'make', VALUE: 'bmw', _id: [7] },
{ FIELD: 'make', VALUE: 'tesla', _id: [] },
{ FIELD: 'make', VALUE: 'volvo', _id: [8] }
]
})
Expand Down
Loading

0 comments on commit 503f488

Please sign in to comment.