The Orama Astro plugin provides seamless integration for adding powerful search capabilities to your Astro websites. It generates search indexes at build time and enables client-side search functionality.
Installation
Install the plugin using your preferred package manager:
npm install @orama/plugin-astro
Configuration
Add the Orama integration to your astro.config.mjs file:
import { defineConfig } from 'astro/config'
import orama from '@orama/plugin-astro'
export default defineConfig ({
integrations: [
orama ({
// Define one or more database configurations
mydb: {
// Required: Only pages matching this regex will be indexed
pathMatcher: /blog \/ [ 0-9 ] {4} \/ [ 0-9 ] {2} \/ [ 0-9 ] {2} \/ . + $ / ,
// Optional: Language for search (default: 'english')
language: 'spanish' ,
// Optional: CSS selectors for content to index (default: ['body'])
contentSelectors: [ 'h1' , 'main' ]
}
})
]
})
Configuration Options
A regular expression to match page paths that should be indexed. Only pages matching this pattern will be included in the search database.
The language for text analysis and stemming. Supports multiple languages for better search accuracy.
contentSelectors
string[]
default: "['body']"
Array of CSS selectors to specify which HTML elements should be indexed. Use this to focus on specific content areas.
Multiple Databases
You can generate multiple search databases with different configurations:
import { defineConfig } from 'astro/config'
import orama from '@orama/plugin-astro'
export default defineConfig ({
integrations: [
orama ({
blog: {
pathMatcher: /blog \/ . + $ / ,
language: 'english' ,
contentSelectors: [ 'article' ]
},
docs: {
pathMatcher: /docs \/ . + $ / ,
language: 'english' ,
contentSelectors: [ 'main' , 'h1' , 'h2' , 'h3' ]
},
allPages: {
pathMatcher: / . * / ,
language: 'english'
}
})
]
})
Build Process
When you run astro build, the plugin automatically:
Scans all pages matching the configured path patterns
Extracts and indexes content based on the content selectors
Generates database files in the dist/assets directory
Names each database as oramaDB_<dbname>.json
For example, a database named mydb will be saved as dist/assets/oramaDB_mydb.json.
Client-Side Usage
Use the generated databases in your pages by importing the client utilities:
< head >
< script >
import { getOramaDB , search } from '@orama/plugin-astro/client'
// Load the database (async operation)
const db = await getOramaDB ( 'mydb' )
// Perform a search
const results = await search ( db , { term: 'search query' })
console . log ( 'Search Results:' , results )
</ script >
</ head >
Interactive Search Component
Here’s a complete example of a search component:
---
// Component script
---
< div class = "search-container" >
< input type = "search" id = "search-input" placeholder = "Search..." />
< div id = "search-results" ></ div >
</ div >
< script >
import { getOramaDB , search } from '@orama/plugin-astro/client'
const searchInput = document . getElementById ( 'search-input' )
const searchResults = document . getElementById ( 'search-results' )
// Load database on page load
const db = await getOramaDB ( 'mydb' )
searchInput ?. addEventListener ( 'input' , async ( e ) => {
const query = e . target . value
if ( query . length < 2 ) {
searchResults . innerHTML = ''
return
}
const results = await search ( db , { term: query , limit: 10 })
searchResults . innerHTML = results . hits . map ( hit => `
<div class="result-item">
<a href=" ${ hit . document . path } ">
<h3> ${ hit . document . title } </h3>
<p> ${ hit . document . content . substring ( 0 , 150 ) } ...</p>
</a>
</div>
` ). join ( '' )
})
</ script >
< style >
.search-container {
max-width : 600 px ;
margin : 0 auto ;
}
#search-input {
width : 100 % ;
padding : 12 px ;
font-size : 16 px ;
border : 1 px solid #ccc ;
border-radius : 4 px ;
}
#search-results {
margin-top : 16 px ;
}
.result-item {
padding : 12 px ;
border-bottom : 1 px solid #eee ;
}
.result-item:hover {
background-color : #f5f5f5 ;
}
</ style >
API Reference
getOramaDB()
Loads a database generated at build time.
getOramaDB ( dbName : string ): Promise < AnyOrama >
The name of the database to load, as defined in your configuration.
Returns: A Promise that resolves to an Orama database instance.
search()
Performs a search query on the database.
search ( db : AnyOrama , params : SearchParams ): Promise < SearchResult >
The database instance returned by getOramaDB().
Search parameters including term, limit, offset, and more.
Returns: A Promise that resolves to search results with hits and metadata.
Limitations
The Astro plugin currently only supports read-only databases. You cannot insert, update, or delete documents at runtime. All indexing happens during the build process.
Requirements
Node.js >= 20.0.0
Astro ^2.0.4
Next Steps
Search Guide Learn about advanced search configuration options
Core Concepts Explore the core Orama search engine