This directory contains comprehensive examples demonstrating how to use Tantivy4Java for various search scenarios. Each example is self-contained and includes detailed comments explaining the concepts.
- BasicSearch - Simple indexing and search operations
- SchemaBuilding - Creating schemas with different field types
- DocumentOperations - Adding, updating, and deleting documents
- QueryTypes - Comprehensive coverage of all query types
- BooleanQueries - Complex boolean logic combinations
- RangeQueries - Numeric and date range searching
- FuzzySearch - Handling typos and spelling variations
- AdvancedSearch - Multi-field search with boosting and scoring
- ProductCatalog - E-commerce search with filtering and faceting
- ContentManagement - Document management system search
- LogAnalytics - Log search and analysis patterns
- BulkIndexing - Efficient bulk document indexing
- SearchOptimization - Performance optimization techniques
- MemoryManagement - Proper resource management patterns
- WebService - REST API integration example
- BatchProcessor - Batch processing workflows
- RealTimeUpdates - Real-time document updates
- Java 11 or higher
- Maven 3.6+
- Tantivy4Java installed in your local repository
# Compile all examples
javac -cp "../target/classes:../target/dependency/*" examples/*.java
# Run a specific example
java -cp "../target/classes:../target/dependency/*:." BasicSearch
# Or use Maven to run examples
mvn exec:java -Dexec.mainClass="BasicSearch" -Dexec.args="sample-data.json"Some examples use sample data files located in the data/ subdirectory:
products.json- Sample product catalog dataarticles.json- Sample article/blog post datalogs.json- Sample log entries for analytics examples
All examples follow the proper resource management pattern:
try (SchemaBuilder builder = new SchemaBuilder();
Schema schema = builder.build();
Index index = new Index(schema);
IndexWriter writer = index.writer()) {
// Operations here
} // Resources automatically closedExamples demonstrate proper error handling:
try {
// Operations
} catch (Exception e) {
System.err.println("Operation failed: " + e.getMessage());
// Appropriate recovery or cleanup
}Examples show how to configure various components:
// Schema configuration
SchemaBuilder builder = new SchemaBuilder()
.addTextField("title", true, true, "default", "position")
.addIntegerField("id", true, true, true);
// Writer configuration
IndexWriter writer = index.writer(
50_000_000, // 50MB heap size
1 // Single thread
);- Start with BasicSearch to understand core concepts
- Explore SchemaBuilding to learn about field types
- Try DocumentOperations for CRUD operations
- Study QueryTypes for comprehensive query coverage
- Learn BooleanQueries for complex search logic
- Practice RangeQueries for numeric/date searches
- Master AdvancedSearch for production-ready search
- Implement ProductCatalog for real-world scenarios
- Optimize with BulkIndexing and SearchOptimization
- Appropriate field type selection
- Tokenizer configuration for different content types
- Fast field usage for aggregatable data
- Building reusable query components
- Proper boost value selection
- Efficient boolean query combinations
- Batch document processing
- Resource pooling and reuse
- Memory-efficient search patterns
- Comprehensive error handling
- Resource cleanup patterns
- Logging and monitoring integration
When contributing new examples:
- Follow the established naming convention
- Include comprehensive comments explaining concepts
- Demonstrate proper resource management
- Add sample data if needed
- Update this README with the new example
If you have questions about any examples:
- Check the main Documentation
- Review the API Reference
- Look at similar examples for patterns
- Create an issue if you find bugs or have suggestions
These examples provide practical, production-ready patterns for building search applications with Tantivy4Java.