This commit is contained in:
2025-08-22 18:22:09 +02:00
parent fc7672c78a
commit eb07386cf4
4 changed files with 1965 additions and 0 deletions

238
examples/tantivy_search_demo.sh Executable file
View File

@@ -0,0 +1,238 @@
#!/bin/bash
# HeroDB Tantivy Search Demo
# This script demonstrates full-text search capabilities using Redis commands
# HeroDB server should be running on port 6381
set -e # Exit on any error
# Configuration
REDIS_HOST="localhost"
REDIS_PORT="6381"
REDIS_CLI="redis-cli -h $REDIS_HOST -p $REDIS_PORT"
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
# Function to print colored output
print_header() {
echo -e "${BLUE}=== $1 ===${NC}"
}
print_success() {
echo -e "${GREEN}$1${NC}"
}
print_info() {
echo -e "${YELLOW} $1${NC}"
}
print_error() {
echo -e "${RED}$1${NC}"
}
# Function to check if HeroDB is running
check_herodb() {
print_info "Checking if HeroDB is running on port $REDIS_PORT..."
if ! $REDIS_CLI ping > /dev/null 2>&1; then
print_error "HeroDB is not running on port $REDIS_PORT"
print_info "Please start HeroDB with: cargo run -- --port $REDIS_PORT"
exit 1
fi
print_success "HeroDB is running and responding"
}
# Function to execute Redis command with error handling
execute_cmd() {
local cmd="$1"
local description="$2"
echo -e "${YELLOW}Command:${NC} $cmd"
if result=$($REDIS_CLI $cmd 2>&1); then
echo -e "${GREEN}Result:${NC} $result"
return 0
else
print_error "Failed: $description"
echo "Error: $result"
return 1
fi
}
# Function to pause for readability
pause() {
echo
read -p "Press Enter to continue..."
echo
}
# Main demo function
main() {
clear
print_header "HeroDB Tantivy Search Demonstration"
echo "This demo shows full-text search capabilities using Redis commands"
echo "HeroDB runs on port $REDIS_PORT (instead of Redis default 6379)"
echo
# Check if HeroDB is running
check_herodb
echo
print_header "Step 1: Create Search Index"
print_info "Creating a product catalog search index with various field types"
# Create search index with schema
execute_cmd "FT.CREATE product_catalog ON HASH PREFIX 1 product: SCHEMA title TEXT WEIGHT 2.0 SORTABLE description TEXT category TAG SEPARATOR , price NUMERIC SORTABLE rating NUMERIC SORTABLE location GEO" \
"Creating search index"
print_success "Search index 'product_catalog' created successfully"
pause
print_header "Step 2: Add Sample Products"
print_info "Adding sample products to demonstrate different search scenarios"
# Add sample products
products=(
"product:1 title 'Wireless Bluetooth Headphones' description 'Premium noise-canceling headphones with 30-hour battery life' category 'electronics,audio' price 299.99 rating 4.5 location '-122.4194,37.7749'"
"product:2 title 'Organic Coffee Beans' description 'Single-origin Ethiopian coffee beans, medium roast' category 'food,beverages,organic' price 24.99 rating 4.8 location '-74.0060,40.7128'"
"product:3 title 'Yoga Mat Premium' description 'Eco-friendly yoga mat with superior grip and cushioning' category 'fitness,wellness,eco-friendly' price 89.99 rating 4.3 location '-118.2437,34.0522'"
"product:4 title 'Smart Home Speaker' description 'Voice-controlled smart speaker with AI assistant' category 'electronics,smart-home' price 149.99 rating 4.2 location '-87.6298,41.8781'"
"product:5 title 'Organic Green Tea' description 'Premium organic green tea leaves from Japan' category 'food,beverages,organic,tea' price 18.99 rating 4.7 location '139.6503,35.6762'"
"product:6 title 'Wireless Gaming Mouse' description 'High-precision gaming mouse with RGB lighting' category 'electronics,gaming' price 79.99 rating 4.4 location '-122.3321,47.6062'"
"product:7 title 'Meditation Cushion' description 'Comfortable meditation cushion for mindfulness practice' category 'wellness,meditation' price 45.99 rating 4.6 location '-122.4194,37.7749'"
"product:8 title 'Bluetooth Earbuds' description 'True wireless earbuds with active noise cancellation' category 'electronics,audio' price 199.99 rating 4.1 location '-74.0060,40.7128'"
)
for product in "${products[@]}"; do
execute_cmd "HSET $product" "Adding product"
done
print_success "Added ${#products[@]} products to the index"
pause
print_header "Step 3: Basic Text Search"
print_info "Searching for 'wireless' products"
execute_cmd "FT.SEARCH product_catalog wireless" "Basic text search"
pause
print_header "Step 4: Search with Filters"
print_info "Searching for 'organic' products in 'food' category"
execute_cmd "FT.SEARCH product_catalog 'organic @category:{food}'" "Filtered search"
pause
print_header "Step 5: Numeric Range Search"
print_info "Finding products priced between \$50 and \$150"
execute_cmd "FT.SEARCH product_catalog '@price:[50 150]'" "Numeric range search"
pause
print_header "Step 6: Sorting Results"
print_info "Searching electronics sorted by price (ascending)"
execute_cmd "FT.SEARCH product_catalog '@category:{electronics}' SORTBY price ASC" "Sorted search"
pause
print_header "Step 7: Limiting Results"
print_info "Getting top 3 highest rated products"
execute_cmd "FT.SEARCH product_catalog '*' SORTBY rating DESC LIMIT 0 3" "Limited results with sorting"
pause
print_header "Step 8: Complex Query"
print_info "Finding audio products with noise cancellation, sorted by rating"
execute_cmd "FT.SEARCH product_catalog '@category:{audio} noise cancellation' SORTBY rating DESC" "Complex query"
pause
print_header "Step 9: Geographic Search"
print_info "Finding products near San Francisco (within 50km)"
execute_cmd "FT.SEARCH product_catalog '@location:[37.7749 -122.4194 50 km]'" "Geographic search"
pause
print_header "Step 10: Aggregation Example"
print_info "Getting index information and statistics"
execute_cmd "FT.INFO product_catalog" "Index information"
pause
print_header "Step 11: Search Comparison"
print_info "Comparing Tantivy search vs simple key matching"
echo -e "${YELLOW}Tantivy Full-Text Search:${NC}"
execute_cmd "FT.SEARCH product_catalog 'battery life'" "Full-text search for 'battery life'"
echo
echo -e "${YELLOW}Simple Key Pattern Matching:${NC}"
execute_cmd "KEYS *battery*" "Simple pattern matching for 'battery'"
print_info "Notice how full-text search finds relevant results even when exact words don't match keys"
pause
print_header "Step 12: Fuzzy Search"
print_info "Demonstrating fuzzy matching (typo tolerance)"
execute_cmd "FT.SEARCH product_catalog 'wireles'" "Fuzzy search with typo"
pause
print_header "Step 13: Phrase Search"
print_info "Searching for exact phrases"
execute_cmd "FT.SEARCH product_catalog '\"noise canceling\"'" "Exact phrase search"
pause
print_header "Step 14: Boolean Queries"
print_info "Using boolean operators (AND, OR, NOT)"
execute_cmd "FT.SEARCH product_catalog 'wireless AND audio'" "Boolean AND search"
echo
execute_cmd "FT.SEARCH product_catalog 'coffee OR tea'" "Boolean OR search"
pause
print_header "Step 15: Cleanup"
print_info "Removing test data"
# Delete the search index
execute_cmd "FT.DROPINDEX product_catalog" "Dropping search index"
# Clean up hash keys
for i in {1..8}; do
execute_cmd "DEL product:$i" "Deleting product:$i"
done
print_success "Cleanup completed"
echo
print_header "Demo Summary"
echo "This demonstration showed:"
echo "• Creating search indexes with different field types"
echo "• Adding documents to the search index"
echo "• Basic and advanced text search queries"
echo "• Filtering by categories and numeric ranges"
echo "• Sorting and limiting results"
echo "• Geographic searches"
echo "• Fuzzy matching and phrase searches"
echo "• Boolean query operators"
echo "• Comparison with simple pattern matching"
echo
print_success "HeroDB Tantivy search demo completed successfully!"
echo
print_info "Key advantages of Tantivy full-text search:"
echo " - Relevance scoring and ranking"
echo " - Fuzzy matching and typo tolerance"
echo " - Complex boolean queries"
echo " - Field-specific searches and filters"
echo " - Geographic and numeric range queries"
echo " - Much faster than pattern matching on large datasets"
echo
print_info "To run HeroDB server: cargo run -- --port 6381"
print_info "To connect with redis-cli: redis-cli -h localhost -p 6381"
}
# Run the demo
main "$@"