I tried this locally once again
docker run -d --name typesense -p 8108:8108 -v $(pwd)/typesense-data:/data -v $(pwd)/typesense-analytics:/analytics -e TYPESENSE_API_KEY=abcd -e TYPESENSE_DATA_DIR=/data -e TYPESENSE_ENABLE_SEARCH_ANALYTICS=true -e TYPESENSE_ANALYTICS_DIR=/analytics -e TYPESENSE_ANALYTICS_FLUSH_INTERVAL=60 typesense/typesense:27.1
this was the docker command i run and this is the file i am testing can you please review am i missing something
from typesense import Client
import time
import json
# Setup client
client = Client({
'nodes': [{
'host': 'localhost',
'port': '8108',
'protocol': 'http'
}],
'api_key': 'abcd',
'connection_timeout_seconds': 2
})
# Create books collection
try:
books_schema = {
"name": "books",
"fields": [
{"name": "title", "type": "string"},
{"name": "authors", "type": "string[]", "facet": True},
{"name": "publication_year", "type": "int32", "facet": True},
{"name": "ratings_count", "type": "int32"},
{"name": "average_rating", "type": "float"},
{"name": "image_url", "type": "string"}
],
"default_sorting_field": "ratings_count"
}
client.collections.create(books_schema)
except Exception as e:
print(f"Books collection warning: {e}")
# Add sample books
sample_books = [
{
'id': '1',
'authors': ['Suzanne Collins'],
'average_rating': 4.34,
'publication_year': 2008,
'title': 'The Hunger Games',
'image_url': '
https://images.gr-assets.com/books/1447303603m/2767052.jpg▾
',
'ratings_count': 4780653
},
{
'id': '2',
'authors': ['Robert Martin'],
'average_rating': 4.7,
'publication_year': 2008,
'title': 'Clean Code: A Handbook of Programming',
'image_url': '
https://example.com/cleancode.jpg▾
',
'ratings_count': 100000
},
{
'id': '3',
'authors': ['Douglas Crockford'],
'average_rating': 4.5,
'publication_year': 2008,
'title': 'JavaScript: The Good Parts',
'image_url': '
https://example.com/javascript.jpg▾
',
'ratings_count': 80000
}
]
for book in sample_books:
try:
client.collections['books'].documents.create(book)
except Exception as e:
print(f"Error adding book {book['id']}: {e}")
# Drop existing analytics rule if any
try:
client.analytics.rules['books_queries_aggregation'].delete()
except Exception as e:
print(f"No existing rule to delete: {e}")
# Create analytics collection
try:
queries_schema = {
"name": "books_queries",
"fields": [
{"name": "q", "type": "string"},
{"name": "count", "type": "int32"}
]
}
client.collections.create(queries_schema)
except Exception as e:
print(f"Queries collection warning: {e}")
# Setup analytics rule
try:
create_response = client.analytics.rules.create({
"name": "books_queries_aggregation",
"type": "popular_queries",
"params": {
"source": {
"collections": ["books"]
},
"destination": {
"collection": "books_queries"
},
"limit": 1000
}
})
print(f"Analytics rule created: {create_response}")
# Verify rule creation
rule = client.analytics.rules['books_queries_aggregation'].retrieve()
print(f"Retrieved rule: {rule}")
except Exception as e:
print(f"Analytics rule error: {e}")
# # Perform test searches
def perform_test_searches():
searches = [
"Suzanne",
"javascript",
"Suzanne", # Duplicate to test count increment
"programming",
"coding"
]
for query in searches:
try:
result = client.collections["books"].documents.search({
"q": query,
"query_by": "title,authors"
})
print(f"Searched for '{query}': {len(result['hits'])} results")
except Exception as e:
print(f"Search error for '{query}': {e}")
# Check analytics collection
def check_analytics():
try:
analytics = client.collections["books_queries"].documents.search({
"q": "*",
"query_by": "q",
})
print("\nAnalytics Results:", analytics)
except Exception as e:
print(f"Analytics error: {e}")
if
name == "__main__":
print("Performing test searches...")
perform_test_searches()
print("\nWaiting for analytics flush (120 seconds)...")
time.sleep(120) # Wait for flush interval
print("\nChecking analytics collection...")
check_analytics()