Solr is a popular open source search platform used to build search applications. One of the key features of Solr is faceted search, which allows users to refine search results by selecting various facets, such as categories, tags, or dates. There are several Solr faceted search clients and React components available that can be used to build search interfaces.
One popular Solr faceted search client is the SolrJS library, which provides a JavaScript API for querying Solr and handling faceted search results. SolrJS can be used in both client-side and server-side applications and supports various Solr query parameters and response formats.
Another popular Solr faceted search client is the React-Solr package, which provides a set of React components for building search interfaces using Solr. React-Solr includes components for search input, search results, and faceted search filters, and supports custom styling and customization.
To use Solr faceted search with React, you can first create a Solr instance and index your data using Solr's indexing tools. Then, you can use a Solr faceted search client or React component to build a search interface that sends queries to Solr and displays the results using React components. You can also customize the search interface to add features such as autocomplete, typeahead, and highlighting.
Here is an example of using React-Solr to build a faceted search interface:
import React from 'react';
import { SolrClient, useSolr } from 'react-solr';
const solr = new SolrClient({
url: 'http://localhost:8983/solr',
collection: 'mycollection',
});
function SearchBar() {
const { value, onChange, onSubmit } = useSolr();
return (
<form onSubmit={onSubmit}>
<input type="text" value={value} onChange={onChange} />
<button type="submit">Search</button>
</form>
);
}
function SearchResults() {
const { data, error, isLoading } = useSolr({
query: '*:*',
params: {
facet: true,
'facet.field': ['category', 'tags'],
'facet.limit': 10,
},
});
if (isLoading) {
return <div>Loading...</div>;
}
if (error) {
return <div>Error: {error.message}</div>;
}
const categories = data.facets.category || [];
const tags = data.facets.tags || [];
return (
<div>
<ul>
{data.docs.map((doc) => (
<li key={doc.id}>{doc.title}</li>
))}
</ul>
<div>
<h2>Categories</h2>
<ul>
{categories.map((category) => (
<li key={category.value}>
<a href={category.url}>{category.value}</a> ({category.count})
</li>
))}
</ul>
<h2>Tags</h2>
<ul>
{tags.map((tag) => (
<li key={tag.value}>
<a href={tag.url}>{tag.value}</a> ({tag.count})
</li>
))}
</ul>
</div>
</div>
);
}
function App() {
return (
<SolrClient solr={solr}>
<SearchBar />
<SearchResults />
</SolrClient>
);
}
export default App;
In this example, we use the SolrClient
component from React-Solr to connect to our Solr instance and specify the collection we want to search.