tantivy #5

Open
despiegk wants to merge 18 commits from tantivy into main
Showing only changes of commit 8decbf3375 - Show all commits

View File

@@ -55,11 +55,11 @@ check_herodb() {
# Function to execute Redis command with error handling
execute_cmd() {
local cmd="$1"
local description="$2"
local description="${@: -1}"
set -- "${@:1:$(($#-1))}"
echo -e "${YELLOW}Command:${NC} $cmd"
if result=$($REDIS_CLI $cmd 2>&1); then
echo -e "${YELLOW}Command:${NC} $(printf '%q ' "$@")"
if result=$($REDIS_CLI "$@" 2>&1); then
echo -e "${GREEN}Result:${NC} $result"
return 0
else
@@ -92,7 +92,7 @@ main() {
print_info "Creating a product catalog search index with various field types"
# Create search index with schema
execute_cmd "FT.CREATE product_catalog SCHEMA title TEXT description TEXT category TAG price NUMERIC rating NUMERIC location GEO" \
execute_cmd FT.CREATE product_catalog SCHEMA title TEXT description TEXT category TAG price NUMERIC rating NUMERIC location GEO \
"Creating search index"
print_success "Search index 'product_catalog' created successfully"
@@ -102,14 +102,14 @@ main() {
print_info "Adding sample products to demonstrate different search scenarios"
# Add sample products using FT.ADD
execute_cmd "FT.ADD product_catalog product:1 1.0 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'" "Adding product 1"
execute_cmd "FT.ADD product_catalog product:2 1.0 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'" "Adding product 2"
execute_cmd "FT.ADD product_catalog product:3 1.0 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'" "Adding product 3"
execute_cmd "FT.ADD product_catalog product:4 1.0 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'" "Adding product 4"
execute_cmd "FT.ADD product_catalog product:5 1.0 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'" "Adding product 5"
execute_cmd "FT.ADD product_catalog product:6 1.0 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'" "Adding product 6"
execute_cmd "FT.ADD product_catalog product:7 1.0 title 'Comfortable meditation cushion for mindfulness practice' description 'Meditation cushion with premium materials' category 'wellness,meditation' price 45.99 rating 4.6 location '-122.4194,37.7749'" "Adding product 7"
execute_cmd "FT.ADD product_catalog product:8 1.0 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'" "Adding product 8"
execute_cmd FT.ADD product_catalog product:1 1.0 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' "Adding product 1"
execute_cmd FT.ADD product_catalog product:2 1.0 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' "Adding product 2"
execute_cmd FT.ADD product_catalog product:3 1.0 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' "Adding product 3"
execute_cmd FT.ADD product_catalog product:4 1.0 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' "Adding product 4"
execute_cmd FT.ADD product_catalog product:5 1.0 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' "Adding product 5"
execute_cmd FT.ADD product_catalog product:6 1.0 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' "Adding product 6"
execute_cmd FT.ADD product_catalog product:7 1.0 title 'Comfortable meditation cushion for mindfulness practice' description 'Meditation cushion with premium materials' category 'wellness,meditation' price 45.99 rating 4.6 location '-122.4194,37.7749' "Adding product 7"
execute_cmd FT.ADD product_catalog product:8 1.0 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' "Adding product 8"
print_success "Added 8 products to the index"
pause
@@ -117,60 +117,60 @@ main() {
print_header "Step 3: Basic Text Search"
print_info "Searching for 'wireless' products"
execute_cmd "FT.SEARCH product_catalog wireless" "Basic text search"
execute_cmd FT.SEARCH product_catalog wireless "Basic text search"
pause
print_header "Step 4: Search with Filters"
print_info "Searching for 'organic' products"
execute_cmd "FT.SEARCH product_catalog organic" "Filtered search"
execute_cmd FT.SEARCH product_catalog organic "Filtered search"
pause
print_header "Step 5: Numeric Range Search"
print_info "Searching for 'premium' products"
execute_cmd "FT.SEARCH product_catalog premium" "Text search"
execute_cmd FT.SEARCH product_catalog premium "Text search"
pause
print_header "Step 6: Sorting Results"
print_info "Searching for electronics"
execute_cmd "FT.SEARCH product_catalog electronics" "Category search"
execute_cmd FT.SEARCH product_catalog electronics "Category search"
pause
print_header "Step 7: Limiting Results"
print_info "Searching for wireless products with limit"
execute_cmd "FT.SEARCH product_catalog wireless LIMIT 0 3" "Limited results"
execute_cmd FT.SEARCH product_catalog wireless LIMIT 0 3 "Limited results"
pause
print_header "Step 8: Complex Query"
print_info "Finding audio products with noise cancellation"
execute_cmd "FT.SEARCH product_catalog 'noise cancellation'" "Complex query"
execute_cmd FT.SEARCH product_catalog 'noise cancellation' "Complex query"
pause
print_header "Step 9: Geographic Search"
print_info "Searching for meditation products"
execute_cmd "FT.SEARCH product_catalog meditation" "Text search"
execute_cmd FT.SEARCH product_catalog meditation "Text search"
pause
print_header "Step 10: Aggregation Example"
print_info "Getting index information and statistics"
execute_cmd "FT.INFO product_catalog" "Index information"
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'"
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'"
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
@@ -178,32 +178,32 @@ main() {
print_header "Step 12: Fuzzy Search"
print_info "Searching for headphones"
execute_cmd "FT.SEARCH product_catalog headphones" "Text search"
execute_cmd FT.SEARCH product_catalog headphones "Text search"
pause
print_header "Step 13: Phrase Search"
print_info "Searching for coffee products"
execute_cmd "FT.SEARCH product_catalog coffee" "Text search"
execute_cmd FT.SEARCH product_catalog coffee "Text search"
pause
print_header "Step 14: Boolean Queries"
print_info "Searching for gaming products"
execute_cmd "FT.SEARCH product_catalog gaming" "Text search"
execute_cmd FT.SEARCH product_catalog gaming "Text search"
echo
execute_cmd "FT.SEARCH product_catalog tea" "Text search"
execute_cmd FT.SEARCH product_catalog tea "Text search"
pause
print_header "Step 15: Cleanup"
print_info "Removing test data"
# Delete the search index
execute_cmd "FT.DROP product_catalog" "Dropping search index"
execute_cmd FT.DROP product_catalog "Dropping search index"
# Clean up documents from search index
for i in {1..8}; do
execute_cmd "FT.DEL product_catalog product:$i" "Deleting product:$i from index"
execute_cmd FT.DEL product_catalog product:$i "Deleting product:$i from index"
done
print_success "Cleanup completed"
@@ -236,4 +236,4 @@ main() {
}
# Run the demo
main "$@"
main "$@"