Hey everyone, I am trying to create search suggest...
# community-help
s
Hey everyone, I am trying to create search suggestions but i am having problems. I already have a collection named as
Copy code
hotels_new
with over around 173000 data. and the search is perfectly working fine but since I am trying to create search suggestions. So, I run the following and the rules and the hotels_new queries are perfectly created without any error. I checked the queries it has initially 0 number of documents. The issue currently is when I do search on the hotels_new the hotels_new_queries isn't being updated. I have a Lightsail server hosted with the following configurations Typesense Setup
Copy code
hotels_new_queries = {
    "name": "hotels_new_queries",
    "fields": [
        {"name": "q", "type": "string"},
        {"name": "count", "type": "int64"}
    ]
}

client.collections.create(hotels_new_queries)

rule_name = 'hotels_new_queries_aggregation'
rule_configuration = {
    "type": "popular_queries",
    "params": {
        "source": {
            "collections": ["hotels_new"]
        },
        "destination": {
            "collection": "hotels_new_queries"
        },
        "expand_query": False,
        "limit": 1000
    }
}

print(client.analytics.rules.upsert(rule_name, rule_configuration))
LightSail typesense.ini configuration
Copy code
; Typesense Configuration
[server]
api-address = 0.0.0.0
api-port = 8108
data-dir = /var/lib/typesense
api-key = api_key_working_fine
log-dir = /var/log/typesense
enable-search-analytics = true
analytics-dir = /var/lib/typesense/analytics
analytics-flush-interval = 60
Also I checked the logs, everything seems fine and there is nothing unusual. Along with I also checked the /var/lib/typesense, all the files and permissions are same. I also checked after flush interval but nothing, the number of documents is 0. What can I try next?
k
What version of Typesense are you using?
s
27.1 Version
k
I recommend trying this configuration out on a sample collection on your localhost. This way we can rule out environment issues.
s
I have done running on docker container with local configuration but nothing is helping can you guys some tutorial on that.
k
s
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()
the response was like this
python test_locally.py
Books collection warning: [Errno 409] A collection with name
books
already exists. Error adding book 1: [Errno 409] A document with id 1 already exists. Error adding book 2: [Errno 409] A document with id 2 already exists. Error adding book 3: [Errno 409] A document with id 3 already exists. Queries collection warning: [Errno 409] A collection with name
books_queries
already exists. Analytics rule created: {'name': 'books_queries_aggregation', 'params': {'destination': {'collection': 'books_queries'}, 'limit': 1000, 'source': {'collections': ['books']}}, 'type': 'popular_queries'} Retrieved rule: {'name': 'books_queries_aggregation', 'params': {'destination': {'collection': 'books_queries'}, 'expand_query': False, 'limit': 1000, 'source': {'collections': ['books']}}, 'type': 'popular_queries'} Performing test searches... Searched for 'Suzanne': 1 results Searched for 'javascript': 1 results Searched for 'Suzanne': 1 results Searched for 'programming': 1 results Searched for 'coding': 0 results Waiting for analytics flush (120 seconds)... Checking analytics collection... Analytics Results: {'facet_counts': [], 'found': 0, 'hits': [], 'out_of': 0, 'page': 1, 'request_params': {'collection_name': 'books_queries', 'first_q': '*', 'per_page': 10, 'q': '*'}, 'search_cutoff': False, 'search_time_ms': 0}
k
@Sudip Parajuli The
true
must be in CAPS Instead of
Copy code
TYPESENSE_ENABLE_SEARCH_ANALYTICS=true
It should be
Copy code
TYPESENSE_ENABLE_SEARCH_ANALYTICS=TRUE
We have fixed this in recent builds, but making the above change should work in 27.1
1
s
Thank You @Kishore Nallan for the help. I think now it's working.
🙌 1
k
Sorry that it took so long to solve this one.