Using Vectorize to build an unreasonably good search engine in 160 lines of code

layout: post


Prerequisites

• A Jekyll site deployed on Netlify.

• Basic knowledge of JavaScript and Jekyll’s structure.

• PartyKit and Cloudflare account access.

Step 1: Install Dependencies

You’ll need the following packages:

PartyKit: For deploying the search backend.

Vectorize: For creating and managing vector embeddings.

  1. Install PartyKit CLI:
npm install -g @partykit/cli
  1. Set up a Vectorize project in Cloudflare following their documentation. This will allow you to create the embeddings for search.

Step 2: Extract Content from Jekyll Site

  1. Create a new JavaScript file (extract-content.js) in your Jekyll project root directory. This script will extract text content from your markdown files (_posts folder) to use for search.

  2. In extract-content.js, write a script to read through your Jekyll markdown files and extract the main content:

const fs = require('fs');

const path = require('path');

  

const postsDir = path.join(__dirname, '_posts');

const posts = fs.readdirSync(postsDir).map(filename => {

  const filePath = path.join(postsDir, filename);

  const content = fs.readFileSync(filePath, 'utf-8');

  const title = content.match(/title:\s*(.*)/)[1];

  const body = content.split('---')[2].trim();

  return { title, body };

});

  

fs.writeFileSync('content.json', JSON.stringify(posts, null, 2));

This script will create a content.json file with titles and content from each post.

Step 3: Create Embeddings with Vectorize

  1. Use the content in content.json to generate vector embeddings. You’ll send each post’s content to Vectorize to create an embedding that represents the semantic meaning of the text.

  2. Write a script (generate-embeddings.js) that reads from content.json and sends each post’s content to Vectorize:

const fetch = require('node-fetch');

const fs = require('fs');

  

const posts = require('./content.json');

const VECTORIZE_API = 'https://vectorize.cloudflare.com/v1/embeddings';

  

async function getEmbeddings(text) {

  const response = await fetch(VECTORIZE_API, {

    method: 'POST',

    headers: { 'Content-Type': 'application/json' },

    body: JSON.stringify({ input: text })

  });

  const data = await response.json();

  return data.embedding;

}

  

async function main() {

  for (const post of posts) {

    post.embedding = await getEmbeddings(post.body);

  }

  fs.writeFileSync('embeddings.json', JSON.stringify(posts, null, 2));

}

  

main();

This creates embeddings.json with content and vector embeddings.

Step 4: Set Up PartyKit Backend

  1. Create a new PartyKit project:
partykit create search_backend

cd search_backend
  1. Define a PartyKit server in index.js that will handle search queries:
import { search } from './search.js';

  

export default function (app) {

  app.get('/search', async (req, res) => {

    const query = req.query.q;

    const results = await search(query);

    res.json(results);

  });

}
  1. In search.js, write a function to compare the query vector with post embeddings and return the best matches:
import { cosineSimilarity } from './cosine-similarity';

  

const posts = require('./embeddings.json');

  

export async function search(query) {

  const queryEmbedding = await getEmbeddings(query);

  return posts

    .map(post => ({

      ...post,

      similarity: cosineSimilarity(queryEmbedding, post.embedding)

    }))

    .sort((a, b) => b.similarity - a.similarity)

    .slice(0, 5);

}

Step 5: Deploy PartyKit Backend

  1. Run the PartyKit CLI to deploy:
partykit deploy
  1. You’ll receive a public URL to access your PartyKit backend, e.g., https://your-backend.partykit.dev.

Step 6: Integrate Search Frontend in Jekyll

  1. Create a search page in your Jekyll site (e.g., search.html) with an input box and button for searching.

  2. Add JavaScript in search.html to handle user input and call the PartyKit backend:

<input type="text" id="searchBox" placeholder="Search...">

<button onclick="performSearch()">Search</button>

<div id="results"></div>

  


<script>

  async function performSearch() {

    const query = document.getElementById('searchBox').value;

    const response = await fetch('https://your-backend.partykit.dev/search?q=' + encodeURIComponent(query));

    const results = await response.json();

    document.getElementById('results').innerHTML = results.map(post => `

      <h3>${post.title}</h3>

      <p>${post.body.slice(0, 100)}...</p>

    `).join('');

  }

</script>

Step 7: Deploy the Updated Jekyll Site on Netlify

  1. Commit and push your Jekyll changes to your Git repository. Netlify will automatically rebuild your site.

  2. Visit your search page on Netlify and test out the functionality.

Summary

You now have a Jekyll site with a vector-based search feature hosted on Netlify using PartyKit and Cloudflare’s Vectorize! This approach enables semantic search for more accurate and relevant results.

Notes mentioning this note

There are no notes linking to this note.


Here are all the notes in this site, along with their links, conveniently visualized as a graph.

1 tip i would give an educator to start using ai...Project ManagementSimplicityCommunication CardsHome VisitsSchoolx is here. get your copy todayPresentations and SpeakingSetting Healthy Tech Boundaries for KidsSynergyToolsBook fair controversy at Prairie Hill sparks...Organic Skills are the New Soft SkillsOn Lasting ForeverSites I want to model this site afterBest Year EverInstructional Coaches MastermindCUES 2014District MastermindsFamily GoalsFETC22FOCUSIdeal WeekImpactAPWorkshopMastermindNational Principal's ConferenceObserveOnline Observation FormPhotographersPlanning PeriodASCD PlayRewire19Scales and Goals 2014SchoolXSeminarSponsorshipsSurveyTraumaUCET 2014SonderPatienceNot all screen time is created equal2 Keys to Building CommunityOSPI Graduation Equity Webinar3 Rules for Academic Writing with AI3 things i'm interested in right now5 best rti episodes on transformative principal5 ways our edu system would be different if...50 Questions to Ask to Solve Problems5rs5sigma20Argentinian Food - Fast Sunday Family Feast for...Adlerian psychology in schoolsAI Easy and HardBig impact mixerHow We Use AI In SchoolsSchedules are a big puzzle, and I like puzzlesThree reasons you should have a podcast to go with...Based on what you know about meAI Produces and Your Brain Develops[Big Impact Mixer] - MiraclesA decade never to be forgottenAccelerated learningA less annoying cookie bannerAdd Fathom Analytics to your Obsidian Publish siteBIM January 25 - Breakout Problem SolvingAll parents want their kids to be successfulNobody cares about our life as much as we doA Live Demo Site for a SaaS productAdntbf roadmapAdding a Little Complexity to Make Things More...Be a creator, not a consumerBIM February 2025 - Experiments[Big Impact Mixer] - Are My Social Posts Any Good?AI As a Fundamental ExpectationWhat I've learned from doing 6000 burpees in 40...A summary of my ship30 writingsAI Can do Anything, But Not Something SpecificAdvanced teacher's prompts (54)Bim april creating a personalized newsletterExploring Descript's Agentic EditingAn overview of prompt engineering for educatorsAnyone interested in fundraising should listen to...Assessment (58)Assessment and evaluation (50)[Big Impact Mixer] - Vision, Category Design, 10...AssumptionsBIM May - What are your Gifts?Brooks duncan on handwritten notesBuilding classroom community (50)Building parent and community partnerships (50)Building an App I will use DailyEducatorsCan we trust the ai?Transforming Old Content into Engaging Case Study...Chatgpt prompts for educatorsClassroom creativity(55)Classroom differentiation (50)Classroom discipline (50)Classroom environment (50)Classroom gamification (50)Classroom management (52)Classroom technology integration (50)ClippingsThere’s no such thing as learning loss or learning...Collaboration (54)Compass Among usEdit an episode with me (full podcast production...Don't call it a Substack. - Anil DashDecember - Brazilian FeijoadaDelusional dreams of excess freedomWhy the Ebenezer is so Important in Come Thou...Convertkit essential workflowsCulturally responsive teaching (50)Dan koe's writer's bootcampData dashboardDay 1 ship30Day 17 the 1 app i can’t live without for podcast...“Your Decision, My Decision, Our Decision”: A...Differentiated instruction (100)Effective feedback (54)Election Day, but no TrustEthan Mollick on Knowing When to Use AIEverything AIExplicit teaching (54)January - MolcajeteFast Sunday Family Feast - A Jones Family...February - Borscht and PelmeniHow I lost 70 Pounds in a Sustainable WayHow i got my wife to stop nagging me[Big Impact Mixer] - Experiments FollowupFrameworksHoliness to the Lord in Everyday LifeFit looks like gritFollowing up on my post ai counselor botsJethro's Appearances and Media ElsewhereIrish Food - March 2025Guatemalan Food for April 2025How i got started in leadershipHow i would start my online presence todayHow writing helped me present betterHow to Be a Transformative Principal By Jethro...How This Site is BuiltHow to use mint mobile's minternational passIf I were a ProfessorJust subscribe to these youtube channelsI was quoted in a story from Parents MagazineLeadership vs managementLearning is like a springLearning is a lifelong processLesson planning (50)NewslettersLong sentencesMiraclesMove from blogtalkradio to transistorMaking “Social” Social AgainListen LaterLucky MeatDoes Magic Mind really give you clean and calm...New Disease: NovolibrochitatNotes to staciObsidianPaperless principal submittedPaperless principalParent Teacher communication (50)Pedagogical approaches (50)Pedagogy and instructional strategies (100)Podcast gearPolson middle schoolPresentations on aiPraiseProblem in edu is that we don’t have outcome Based...Professional growth and development (50)Promoting ethical decision Making and responsible...Recipes on the Web are the WORST!Read ai summary from the vaesp lunch and learnRssAsk QuestionsQuoting Cory DoctorowCurriculumHomeStudents Who ThriveEnrollmentHow it worksSstHow we told families school doesn't matter and...SchoolX Book Study - Transform your School with...SchoolX Book Study - Transform your School with...Save the webSessions Health - Mental Health Practice...Self EsteemAn 84% Pay Cut? Would You Take it?Should i learn to codeStop using generative AI as a search engineTailgate - February 2025Sponsored Podcast EpisodeStrivingStudent engagement and motivation (100)Student Centered learning (50)Students Need Human Relationships to Thrive. Why...Summer pinedaSupporting students transition and college and...The leaders who have influenced me mostThe lord keeps things hiddenThe man who saved the world with human intuition,...The pass method how i've taught ai to be an...The power of no expectationsThe power of personality tests in the hiring...The best book i've read on business financeThe best piece of leadership advice i ever...This Is Mind Blowing.TherapyThree simple tips for recording yourself more...To Radiate Life A Prayer by Elbert HubbardTop 3 podcasts in 2019Trying to Export SubstackKnowing how to use Transcripts in Podcasts in 2025Turning learning upside downI made some big updates to the site todayUse of data to inform practice (50)BooksCommunication CardsThe Ultimate Inspirational Videos for Teachers and...Eliminate Stress and Improve Relationships in Just...BUHSD Day of LearningVERDADUsing Vectorize to build an unreasonably good...VAESPWhy everyone should read the courage to be...Why do AI company logos look like buttholes?Why young people worldwide want and need...You Can Just LearnYou choose to not changeYou made up your angerAplusBest AI ToolsPresentation Notes at ALASBOPresentation Notes at Alaska Municipal LeagueSpiritual ArchitectureBe Podcast NetworkBishop Brady High School AI PresentationBlogDesigners for EducationTeachers Don't Actually Want Feedback. Here's What...You Should be Able to Take Your Data With YouHard thingsiA Presenter Now Allows Sharing on the WebExperimentsProject iSTAR: An Integrated Strategic,...High Rep WorkoutsFacebook adsHow to get unstuck when you're stuckHow Many Teachers Do I Need To Hire For Social...Humilitymedia kitMy Thoughts on LearningLifestyle is your choicePaperless Principal BookNowPeople Ruin the InternetOnboardingHow to Combat Chronic AbsenteeismLoaves and Fishes, not the 5,000How we teach our kids does not happen how we thinkPersonal board of advisorsThe Little, Narrow Prison of Language: The...Morning RoutinePaper trackingPbis As MedicinePodcast AppearancesAll Learning Is SpiritualProjectsPsychological SafetyReopening Schools WorkshopResultsSdlSecret newsletterSubscribeSure Go AheadTake Your Daughter To Work DayThanksThe KindleTrauma Podcast MapUcet2013Your Values are Only Worth What You’re Willing to...UCET 2023Supporting Students with Webs of Support - Podcast...Where To Startand An Action ItemWhy I Do What I DoWhy Most Reopening Plans Are Doomed To FailWorld Down Syndrome Awareness DayYoutube For Schools“Put Your Shoulder to the Wheel”
Follow
Follow
Follow