0

I am using Searchkick in my Rails app. I am cleaning up my tests and I get this for a basic index page:

Searchkick::InvalidQueryError: [400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [name] in order to load field data by uninverting the inverted index.

My model (I have added multi-tenant complexity and tried all these custom mappings I found for possible solutions):

    class Vendor < ApplicationRecord
      multi_tenant :company
      searchkick merge_mappings: true, inheritance: true, index_name: -> { [MultiTenant.current_tenant.tenant_name, model_name.plural, Rails.env].join('_') },
        mappings: {
          properties: {
            name: {
              type: "text",
              fielddata: true,
              fields: {
                keyword: {
                  type: "keyword"
                }
              }
            }
          }
        }

  def search_data
    attributes.merge({
      tracker: tracker_id,
      status: aasm_state,
      comments: comments.map(&:note).join(" "),
      city_es: city.presence ? city : "None",
      province_es: province.presence ? province : "None"
      })
  end

My controller (the es_ methods are some custom helpers I added as I was tiered of setting these repetitive param values):

@vendors = Vendor.search(es_query(),
  limit: es_limit(),
  aggs: { status: {}, city_es: {}, province_es: {} },
  where: where,
  offset: es_offset(),
  order: { es_order_col("name") => { order: es_order_dir(), unmapped_type: "long" }},
  match: :word,
  operator: "or",
  includes: [])

The browser development env works fine. No idea what I am missing here to fix this.

Dan Tappin
  • 2,692
  • 3
  • 37
  • 77

1 Answers1

1

It figures as soon as I add a bounty I figure it out. I needed to add Vendor.reindex to my test helper. Normally minitest complains about this with a separate error as a great reminder.

Dan Tappin
  • 2,692
  • 3
  • 37
  • 77