Metadata Filters
The Metadata Filters in Replyke allow developers to filter entities based on their metadata
property, which is flexible and can store unstructured data. This filter is highly versatile, enabling fine-grained control over the feed by matching key-value pairs, verifying key existence, and excluding unwanted metadata.
Overview of Metadata Filters
The metadataFilters
property can be passed into the FeedProvider
as an object with the following structure:
export interface MetadataFilters {
includes?: Record<string, unknown>;
doesNotInclude?: Record<string, unknown>;
exists?: string[];
doesNotExist?: string[];
}
includes
: Specifies key-value pairs that must exist in the entity’smetadata
. Entities must include all specified key-value pairs.doesNotInclude
: Specifies key-value pairs that must not exist in the entity’smetadata
. If any specified key-value pair exists, the entity is excluded.exists
: An array of keys that must exist in the entity’smetadata
, regardless of their value.doesNotExist
: An array of keys that must not exist in the entity’smetadata
.
Dynamically Updating Metadata Filters
Developers can dynamically update the metadata filters using the setMetadataFilters
function provided by the useFeed
hook. This function expects a new metadataFilters
object or null
to clear the filters.
const { setMetadataFilters } = useFeed();
// Set a filter to include entities with specific metadata
setMetadataFilters({ includes: { category: "tech", featured: true } });
// Reset metadata filters
setMetadataFilters(null);
How Metadata Filters Work
Metadata Filters allow for complex queries that include:
- Key-Value Matching:
includes
: Filters entities to include those whosemetadata
contains all specified key-value pairs.doesNotInclude
: Filters entities to exclude those whosemetadata
contains any specified key-value pairs.
- Key Existence:
exists
: Ensures entities include the specified keys in theirmetadata
.doesNotExist
: Ensures entities do not include the specified keys in theirmetadata
.
- Combined Conditions: Developers can combine these conditions to create sophisticated filtering logic.
Example Use Cases
1. Passing Static Filters to the FeedProvider
To set static metadata filters at feed initialization, pass them directly to the FeedProvider
:
<FeedProvider
metadataFilters={{
includes: { category: "tech", featured: true },
doesNotInclude: { deprecated: true },
exists: ["author"],
doesNotExist: ["archived"],
}}
>
<MyFeedComponent />
</FeedProvider>
This setup ensures that the feed includes:
- Entities where
metadata.category
is “tech” andmetadata.featured
istrue
. - Entities where
metadata.deprecated
is nottrue
. - Entities with a
metadata.author
key. - Entities without a
metadata.archived
key.
2. Dynamically Updating Filters Based on User Interaction
Developers can allow users to refine the feed dynamically using the setMetadataFilters
function:
const { setMetadataFilters, infusedEntities, loadMore } = useFeed();
const applyFilters = () => {
setMetadataFilters({
includes: { category: "tech" },
exists: ["author"],
});
};
return (
<div>
<h1>Filtered Feed</h1>
<button onClick={applyFilters}>Apply Tech Filters</button>
<button onClick={() => setMetadataFilters(null)}>Clear Filters</button>
<ul>
{infusedEntities.map((entity) => (
<li key={entity.referenceId}>
<h2>{entity.title}</h2>
<p>Metadata: {JSON.stringify(entity.metadata)}</p>
</li>
))}
</ul>
<button onClick={loadMore}>Load More</button>
</div>
);
In this example, clicking the “Apply Tech Filters” button updates the feed to include entities categorized as “tech” and with an author
key in their metadata.
Important Notes
- Metadata Filters are powerful for working with unstructured data, allowing highly customized feeds.
- Filters are applied immediately, and the feed resets to reflect the updated conditions.
- Passing
null
tosetMetadataFilters
clears all metadata-based filtering.
Conclusion
Metadata Filters provide a robust mechanism to filter entities based on the metadata
property. With support for key-value matching, key existence validation, and exclusion conditions, these filters allow developers to implement advanced and highly specific feed customization. Whether setting static filters at initialization or dynamically adjusting them based on user input, Metadata Filters make it easy to tailor the feed to diverse application needs.