Hey. I'm trying to create a new analytic rule but ...
# community-help
r
Hey. I'm trying to create a new analytic rule but I'm getting
{ "message": "Not Found"}
. I'm sure that the destination and source collections that I'm sending exist on the cloud. I'm sending
popular_queries
for the name and type. can you help me to understand why I'm getting
404
, please? when do you return 404 for the
POST url/analytics/rules
endpoint? Thanks.
Copy code
On Typesense Cloud
We automatically set --enable-search-analytics=true and --analytics-flush-interval=300 (every 5 minutes) in Typesense Cloud (more context in the section above).
is it possible that analytics to be disabled on cloud?
j
Could you share the full curl request that's throwing the 404?
(minus the API key)
r
sure
Copy code
curl -k "<https://313asdfasfa31241.typesense.net/analytics/rules/>" \
      -X POST \
      -H "Content-Type: application/json" \
      -H "X-TYPESENSE-API-KEY: " \
      -d '{
        "name": "popular_queries",
        "type": "popular_queries",
        "params": {
            "source": {
                "collections": ["products_v6"]
            },
            "destination": {
                "collection": "products_analytics_v1"
            },
            "limit": 1000
        }
      }'
j
Looks like that cluster is running
0.24.0
which doesn't have the analytics feature. You want to upgrade to
v0.25.2.rc6
(Cluster Configuration > Modify). Could you do that and try again?
Side note: v0.25.0 and v0.25.1 have a bug that prevents analytics from being collected... so you would have to upgrade to the above to get the search terms to be aggregated
r
aaaa get it
not sure if the typsense ruby library works with v0.25.2.rc6
j
That's the minimum version
r
indeed, sorry
I'll update it now to the last version
👍 1
Error (Typesense::Error::HTTPStatus0Error)
. maybe it's something wrong with the ruby client. I'll create the rule manually
j
It's a DNS caching issue
If you flush your DNS cache on your OS and try again, it should work
r
perfect, it worked
thanks a lot 🦾
🙌 1
should I add also a document to the destination collection?
or will it be created automatically after a few searches?
j
You'd only need to create the destination collection with the schema mentioned in the docs. The documents will be auto-created by Typesense
r
Great 🍻
🙌 1
a
@Raz are you using the ruby client with rails? did you use this library or did you roll your own? last time I checked with Jason he said that repo wasn't ready for prime time yet
r
https://github.com/typesense/typesense-ruby but I'm just managing the collections from the backend rails app. we are using react for the frontend.
a
gotcha, same!
here is my hand-rolled searchable concern
Copy code
module Searchable
  extend ActiveSupport::Concern

  included do
    after_save :reindex_after_save
    after_destroy :destroy_indexed_document # No idea why this works, you'd think the fact that the record was deleted would prevent an after_destroy hook from knowing what the records id was but it seems to work!
  end

  # Immediately after we save a searchable, we want that record to be reindexed inline.
  # If something goes wrong, log it and throw it in the reindexing queue
  # The queue takes a little bit of time to go through, but after you save a record you want
  # it to be updated immediately, so we do it inline.
  def reindex_after_save
    reindex_inline
  rescue Typesense::Error => e
    puts e
  end

  def collection_index
    TypesenseClient.fetch.collections[self.class.index_name]
  end

  def indexed_document
    collection_index.documents[id]
  end

  def indexed_document_json
    indexed_document.retrieve
  end

  def destroy_indexed_document
    indexed_document.delete
  end

  # actually reindexes the document
  def reindex_inline
    json = self.class.blueprint.render_as_json(self, view: :instantsearch)
    collection_index.documents.upsert(json)
  end

  module ClassMethods
    def index_name
      name
    end

    def create_index
      schema = formated_searchable_schema
      TypesenseClient.fetch.collections.create(schema)
    rescue Typesense::Error::ObjectAlreadyExists => e
      puts e
    end

    def reindex_all_inline
      json = blueprint.render_as_json(all, view: :instantsearch)
      TypesenseClient.fetch.collections[index_name].documents.import(json, { action: 'upsert' })
    end

    def inspect_index
      JSON(['[', TypesenseClient.fetch.collections[index_name].documents.export.gsub("\n", ','), ']'].join(''))
    end

    def destroy_index
      TypesenseClient.fetch.collections[index_name].delete
    end

    def blueprint
      blueprint_name = "#{name.demodulize.classify}Blueprint"
      blueprint_name.constantize
    end

    private

    def formated_searchable_schema
      rv = searchable_schema_hash
      rv['name'] = index_name if rv['name'].blank?
      rv['fields'] = searchable_schema_hash['fields'].map do |searchable_schema_field|
        searchable_schema_field['name'] = searchable_schema_field['name'].camelcase(:lower)

        searchable_schema_field
      end
      rv['default_sorting_field'] = 'created_at'.camelcase(:lower) if rv['default_sorting_field'].blank?

      rv
    end

    def searchable_schema_class
      searchable_schema_name = "#{name.demodulize.classify}SearchableSchema"
      searchable_schema_name.constantize
    end

    def searchable_schema_hash
      searchable_schema_class.as_hash
    end
  end
end
@Jason Bosco curious if we put this on GH somewhere? Not a proper gem, just 100 lines I have been using to manage typesense clusters reliably for the past three years
j
Nice! Yeah, if you want to put this up in a GitHub repo, may be I can link to it from the current rails gem repo