Skip to content

Instantly share code, notes, and snippets.

@Akifcan
Created October 20, 2022 19:44
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 Akifcan/f2d098129c20bad57b6fb9574bf3396f to your computer and use it in GitHub Desktop.
Save Akifcan/f2d098129c20bad57b6fb9574bf3396f to your computer and use it in GitHub Desktop.
Elastic Search Demo
const data = [
'the bright blue butterfly hangs on the breeze',
'its best to forget the great sky and to retire from every wind',
'under blue sky in bright sunlight, one not need search around',
'Love is blind. ',
`I don't want to go to school.`,
`Let's get it over with.`,
`The red one. `,
`I have a big dog. `,
`Where's the nearest bus stop? `,
`Sorry. I would like to, but I don't know how to drive. `,
`She avoided him whenever possible. `,
`There is an urgent need for more people to donate their time and money. `,
`I have ten pens. `,
`He came back last August.`,
`For some reason, I'm wide awake and can't fall asleep. `,
`I left home at seven.`,
`I didn't get the joke.`,
`Where are you?`,
`Don't let him know her address.`,
`I want to stay in America for a few years.`,
`Fill it up, please.`,
`All the boys in class worked hard.`,
`How do you usually decide what to eat?`,
`OK.`,
`I am willing to help you.`,
`What do you do on Sunday?`,
`He looks like his father.`,
`It was only yesterday that I realized what she really meant.`,
`My father loves my mother.`,
]
const docs = []
data.forEach((x, i) => {
x.split(' ').forEach(z => {
const currentDoc = docs.find(doc => doc.term === z)
if(currentDoc){
currentDoc.id.push(i)
}else{
docs.push({
term: z.toLowerCase(),
id: [i]
})
}
})
})
function search(keyword){
const results = []
keyword.split(' ').forEach(term => {
const curr = docs.find(doc => doc.term === term)
if(curr){
results.push(curr.id.map(v => {
return data[v]
}))
}
})
return {
count: results.flat(1).length,
records: results.flat(1)
}
}
console.log(search('blue sky you'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment