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...Paperless Principal BookSimplicityCommunication CardsHome VisitsSchoolx is here. get your copy todayNowAPWorkshopPresentations and SpeakingAplusOnboardingSetting Healthy Tech Boundaries for KidsAdlerian psychology in schoolsAI Easy and HardBig impact mixerAn 84% Pay Cut? Would You Take it?How to Combat Chronic AbsenteeismToolsBook 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 WeekImpactMastermindNational Principal's ConferenceObserveOnline Observation FormPhotographersPlanning PeriodASCD PlayRewire19Scales and Goals 2014SchoolXSeminarSponsorshipsSurveyTraumaUCET 2014Loaves and Fishes, not the 5,000How We Use AI In SchoolsSchoolX Book Study - Transform your School with...VERDADSonderUsing Vectorize to build an unreasonably good...Schedules are a big puzzle, and I like puzzlesNot all screen time is created equalThree reasons you should have a podcast to go with...Based on what you know about meMy Thoughts on LearningAI Produces and Your Brain DevelopsI made some big updates to the site today2 Keys to Building Community[Big Impact Mixer] - MiraclesA decade never to be forgottenAccelerated learningA less annoying cookie bannerAdd Fathom Analytics to your Obsidian Publish site3 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 Problems5rs5sigma20BIM January 25 - Breakout Problem SolvingAll parents want their kids to be successfulNobody cares about our life as much as we doNew Disease: NovolibrochitatAdntbf roadmapAdding a Little Complexity to Make Things More...AI As a Fundamental ExpectationWhat I've learned from doing 6000 burpees in 40...A summary of my ship30 writingsA Live Demo Site for a SaaS productAI Can do Anything, But Not Something SpecificAi leader office hours june 2025Advanced teacher's prompts (54)Exploring Descript's Agentic EditingAn overview of prompt engineering for educatorsAnyone interested in fundraising should listen to...Argentinian Food - Fast Sunday Family Feast for...Assessment (58)Assessment and evaluation (50)[Big Impact Mixer] - Vision, Category Design, 10...AssumptionsBe a creator, not a consumerBIM February 2025 - Experiments[Big Impact Mixer] - Are My Social Posts Any Good?Bim april creating a personalized newsletterBIM 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?Candy teacher referralCaroline classroom observationTransforming 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)Do People Call You for Advice?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 got my wife to stop nagging me[Big Impact Mixer] - Experiments FollowupFrameworksI was quoted in a story from Parents MagazineHoliness to the Lord in Everyday LifeFit looks like gritLearning is like a springFollowing up on my post ai counselor botsJethro's Appearances and Media ElsewhereIrish Food - March 2025Guatemalan Food for April 2025FSFF May 2025 - Korean FoodIf I were a ProfessorFSFF June 2025 - Good Ol' American FoodHow i got started in leadershipHow I lost 70 Pounds in a Sustainable WayHow 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 passJust subscribe to these youtube channelsLeadership vs managementLearning is a lifelong processLesson planning (50)NewslettersLong sentencesListen LaterLucky MeatMiraclesMove from blogtalkradio to transistorMaking “Social” Social AgainDoes Magic Mind really give you clean and calm...Notes to staciOSPI Graduation Equity WebinarObsidianOur System is Designed to Enable CheatingPaperless principal submittedPaperless principalParent Teacher communication (50)PatiencePedagogical 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)Project ManagementPromoting 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...Save the webSessions Health - Mental Health Practice...Self EsteemShould i learn to codeStop using generative AI as a search engineSponsored Podcast EpisodeWhen the Stone turns to BreadStrivingStudent engagement and motivation (100)Student Centered learning (50)Students Need Human Relationships to Thrive. Why...Summer pinedaSupporting students transition and college and...SynergyTailgate - February 2025The 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...Trying to Export SubstackTo Radiate Life A Prayer by Elbert HubbardTop 3 podcasts in 2019Knowing how to use Transcripts in Podcasts in 2025Turning learning upside downUse of data to inform practice (50)VAESP Presentation ResourcesVal meditation reminder appWhy everyone should read the courage to be...Why do AI company logos look like buttholes?Why young people worldwide want and need...Spiritual ArchitectureYou Can Just LearnYou choose to not changeYou made up your angerPresentation Notes at ALASBOPresentation Notes at Alaska Municipal LeagueBe Podcast NetworkBest AI ToolsBishop Brady High School AI PresentationBlogBooksCommunication CardsThe Ultimate Inspirational Videos for Teachers and...Eliminate Stress and Improve Relationships in Just...BUHSD Day of LearningDesigners for EducationTeachers Don't Actually Want Feedback. Here's What...You Should be Able to Take Your Data With YouHard thingsHumilityiA 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...media kitLifestyle is your choiceHow we teach our kids does not happen how we thinkThe Little, Narrow Prison of Language: The...Morning RoutinePaper trackingPbis As MedicinePeople Ruin the InternetPersonal board of advisorsPodcast 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