-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from migrants-and-the-state/feature/init-searc…
…h-14 Feature/init search 14
- Loading branch information
Showing
11 changed files
with
2,851 additions
and
3,455 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
const afileFieldMap = { | ||
id: { | ||
text: 'A-Number', | ||
keypath: 'id' | ||
}, | ||
page_count: { | ||
text: 'Page Count', | ||
keypath: 'page_count' | ||
}, | ||
last_name_nara: { | ||
text: 'Last Name (NARA)', | ||
keypath: 'fields.last_name.nara' | ||
}, | ||
first_name_nara: { | ||
text: 'First Name (NARA)', | ||
keypath: 'fields.first_name.nara' | ||
}, | ||
dob_nara: { | ||
text: 'Date of Birth (NARA)', | ||
keypath: 'fields.dob.nara' | ||
}, | ||
doe_nara: { | ||
text: 'Date of Entry (NARA)', | ||
keypath: 'fields.doe.nara' | ||
}, | ||
poe_nara: { | ||
text: 'Port of Entry (NARA)', | ||
keypath: 'fields.poe.nara' | ||
}, | ||
cob_nara: { | ||
text: 'Country of Birth (NARA)', | ||
keypath: 'fields.cob.nara' | ||
}, | ||
sex_nara: { | ||
text: 'Sex (NARA)', | ||
keypath: 'fields.sex.nara' | ||
}, | ||
sex_ms: { | ||
text: 'Sex (LLM)', | ||
keypath: 'fields.sex.ms_sex_llm_v1' | ||
}, | ||
form_titles_llm: { | ||
text: 'Form Titles (LLM)', | ||
keypath: 'fields.form_titles.ms_form_titles_llm_v1' | ||
}, | ||
countries_nlp: { | ||
text: 'Countries (LLM)', | ||
keypath: 'fields.countries.ms_countries_nlp_v1' | ||
} | ||
}; | ||
|
||
const pageFieldMap = { | ||
id: { | ||
text: 'Page ID', | ||
keypath: 'id' | ||
}, | ||
anumber: { | ||
text: 'A-Number', | ||
keypath: 'anumber' | ||
}, | ||
full_text: { | ||
text: 'Full Text (OCR)', | ||
keypath: 'full_text' | ||
}, | ||
sex_ms: { | ||
text: 'Sex (LLM)', | ||
keypath: 'fields.sex.ms_sex_llm_v1' | ||
}, | ||
doctype_ms: { | ||
text: 'Document Type (LLM)', | ||
keypath: 'fields.doctype.ms_doctype_llm_v1' | ||
}, | ||
form_title_llm: { | ||
text: 'Form Title (LLM)', | ||
keypath: 'fields.form_title.ms_form_title_llm_v1' | ||
}, | ||
countries_nlp: { | ||
text: 'Countries (NLP)', | ||
keypath: 'fields.countries.ms_countries_nlp_v1' | ||
}, | ||
years_nlp: { | ||
text: 'Years (NLP)', | ||
keypath: 'fields.years.ms_years_nlp_v1' | ||
} | ||
}; | ||
|
||
export const fieldMap = { | ||
afile: afileFieldMap, | ||
page: pageFieldMap | ||
}; | ||
|
||
const pageFieldsToSearchWithin = [ | ||
'anumber', | ||
'form_title_llm', | ||
'countries_nlp', | ||
'years_nlp', | ||
'full_text' | ||
].map((field) => ({ | ||
id: field, | ||
text: pageFieldMap[field]['text'], | ||
keypath: pageFieldMap[field]['keypath'] | ||
})); | ||
|
||
const afileFieldsToSearchWithin = [ | ||
'id', | ||
'form_titles_llm', | ||
'countries_nlp', | ||
'last_name_nara', | ||
'first_name_nara', | ||
'poe_nara', | ||
'cob_nara' | ||
].map((field) => ({ | ||
id: field, | ||
text: afileFieldMap[field]['text'], | ||
keypath: afileFieldMap[field]['keypath'] | ||
})); | ||
|
||
export const fieldsToSearchWithin = { | ||
afile: afileFieldsToSearchWithin, | ||
page: pageFieldsToSearchWithin | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,60 @@ | ||
import { goto } from '$app/navigation'; | ||
import { base } from '$app/paths'; | ||
import { fieldMap } from '$lib/fields'; | ||
import MiniSearch from 'minisearch'; | ||
|
||
const FlexSearch = require('flexsearch'); | ||
|
||
const index = FlexSearch.Index({}); | ||
function pMiniConfig(selectedFields) { | ||
return { | ||
fields: selectedFields, //fields to index for full-text search | ||
extractField: (document, fieldName) => { | ||
return fieldName.split('.').reduce((doc, key) => { | ||
const intKey = parseInt(key, 10); | ||
return doc && doc[isNaN(intKey) ? key : intKey]; | ||
}, document); | ||
}, | ||
storeFields: ['id', 'anumber', 'page_index', 'full_text', 'fields'] // fields to return with search results | ||
}; | ||
} | ||
|
||
export function addDocument(id, content) { | ||
index.add(id, content); | ||
function aMiniConfig(selectedFields) { | ||
return { | ||
fields: selectedFields, // fields to index for full-text search | ||
extractField: (document, fieldName) => { | ||
return fieldName.split('.').reduce((doc, key) => { | ||
const intKey = parseInt(key, 10); | ||
return doc && doc[isNaN(intKey) ? key : intKey]; | ||
}, document); | ||
}, | ||
storeFields: ['id', 'fields'] // fields to return with search results | ||
}; | ||
} | ||
|
||
export function search(query) { | ||
return index.search(query); | ||
export async function search(scope, searchParams) { | ||
const jsonPath = `${base}/api/index/${scope}.json`; | ||
const selectedFields = searchParams | ||
.getAll('selectedFields') | ||
.map((field) => fieldMap[scope][field]['keypath']); | ||
const miniSearch = new MiniSearch( | ||
scope == 'afile' ? aMiniConfig(selectedFields) : pMiniConfig(selectedFields) | ||
); | ||
const query = searchParams.get('query') || MiniSearch.wildcard; | ||
const results = await fetch(jsonPath) | ||
.then((resp) => resp.json()) | ||
.then((data) => { | ||
miniSearch.addAll(data); | ||
console.log('miniSearch', miniSearch); | ||
|
||
return miniSearch.search(query, { prefix: true, combine: 'AND', fuzzy: 0.1 }); | ||
}) | ||
.catch((err) => console.error(err)); | ||
return results; | ||
} | ||
|
||
export function handleSubmit(event) { | ||
event.preventDefault(); | ||
localStorage.setItem('formReferrer', window.location.href); | ||
const data = new FormData(event.target); | ||
const scope = event.target.attributes['scope'].value; | ||
const formValues = {}; | ||
|
||
for (const [key, value] of data) { | ||
if (Object.prototype.hasOwnProperty.call(formValues, key)) { | ||
formValues[key] = [].concat(formValues[key], value); | ||
} else { | ||
formValues[key] = value; | ||
} | ||
} | ||
const queryString = new URLSearchParams(formValues).toString(); | ||
goto(`${base}/results/${scope}?${queryString}`); | ||
const params = new URLSearchParams(data); | ||
goto(`${base}/results/${scope}?${params.toString()}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.