Skip to content

Instantly share code, notes, and snippets.

@craigshoemaker
Last active November 23, 2016 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigshoemaker/aeab77f87ced020f8d930611edabc5e2 to your computer and use it in GitHub Desktop.
Save craigshoemaker/aeab77f87ced020f8d930611edabc5e2 to your computer and use it in GitHub Desktop.
Proof of concept working with search-index
const si = require('search-index');
const stream = require('stream');
const topics = [
`Title for File 1 \n A long time ago, in a galaxy far far way...`,
`Title for File 2 \n Once upon a time there was a beautiful princess...`,
`Title for File 3 \n It was the best of times, it was the worst of times...`,
`Title for File 4 \n There once was a Prince and a frog...`,
`Title for File 5 \n In a galaxy with princes, princesses, frogs and times, we at chocolate.`
];
let options = {
indexPath: '_db/topics',
logLevel: 'error'
};
const seedDatabase = (err, index, callback) => {
if(err) console.log(err);
const Readable = stream.Readable;
const readStream = new Readable({ objectMode: true });
topics.forEach((topicBody, i) => {
readStream.push({
id: `topic-${i+1}`,
body: topicBody
});
});
readStream.push(null);
readStream.pipe(index.defaultPipeline())
.pipe(index.add())
.on('data', data => console.log(data))
.on('end', () => {
console.log('All files are indexed');
callback();
});
};
const search = (index, query) => {
console.log(`searching for: ${JSON.stringify(query)}`);
index.search(query)
.on('data', data => console.log(data))
.on('end', () => console.log('search is complete'));
};
si(options, (err, index) => {
if(err) console.log(err);
seedDatabase(err, index, () => {
if(err) console.log(err);
const queryContainer = {
query: [
{ AND: { 'body': ['time'] } },
{ AND: { 'body': ['beautiful'] } },
]
};
search(index, queryContainer);
});
});
/* ******* Begin Results **********
All files are indexed
searching for: {"query":[{"AND":{"body":["time"]}},{"AND":{"body":["beautiful"]}}]}
{ id: 'topic-2',
scoringCriteria:
[ { tf: [Object],
df: [Object],
tfidf: [Object],
boost: 0,
score: 0.7359535705891886 } ],
score: 0.7359535705891886,
document:
{ id: 'topic-2',
body: 'Title for File 2 \n Once upon a time there was a beautiful princess...' } }
{ id: 'topic-1',
scoringCriteria:
[ { tf: [Object],
df: [Object],
tfidf: [Object],
boost: 0,
score: 0.7359535705891886 } ],
score: 0.7359535705891886,
document:
{ id: 'topic-1',
body: 'Title for File 1 \n A long time ago, in a galaxy far far way...' } }
search is complete
******* End Results ********** */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment