jeudi 26 novembre 2015

searchkick aggs undefined method `[]' for nil:NilClass

After install and index the products with searchkick i create a aggregation and rails spit out undefined method `[]' for nil:NilClass

Someone by the way knows or can spare a hint why this behavior with searchkick?

thank's

products model:

after_touch :reindex

     searchkick word_start: [:name],
                 suggest: ["name"],
                 highlight: [:name],
        merge_mappings: true, mappings: {
        product: {
          properties: {
            name: {type: "string", analyzer: "keyword", boost: 100},
             id: {type: "long"},
              price: {type: "long"},
              vitrine_id: {type: "long" },
              brand_id: {type: "long" },
              condition_id: {type: "long"},

          }
        }
      }


    def search_data
        {
            name: name,
            price: price,
            vitrine_id: vitrine_id,
            created_at: created_at,
        }
    end


def self.aggs_search(params)
      query = params[:query].presence || "*"
      conditions = {}
      conditions[:price] = params[:price] if params[:price].present?
      products = Product.search query, where: conditions, 
        aggs: [:price], 
        smart_aggs: true, page: params[:page], suggest: true, highlight: true,
        per_page: 10
      products
    end

products controller:

def index
 @products = Product.aggs_search(params)
end

Products aggs view:

<div class="filtro">
    <h2>Price</h2>
   <% if @products.aggs["price"]["terms"].present? %>
         <ul>
        <% @products.aggs["price"]["terms"].each do |filter| %>
          <li><%= link_to "#{filter["term"]} (#{filter["count"]})", "/products?price=#{filter["term"]}" %></li>
        <% end %>
        </ul>
    <% end %>
  </div>

Aucun commentaire:

Enregistrer un commentaire