Rails 5:如何在搜索结果中创建动态聚合和过滤器

时间:2017-10-28 00:55:49

标签: javascript ruby-on-rails search aggregation facet

我有一个搜索表单,用于搜索并显示要列出的搜索结果。

列表架构是:

    t.string   "title"
    t.text     "description"
    t.string   "city"
    t.string   "state"
    t.string   "zipcode"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "category_id"
    t.integer  "subcategory_id"
    t.float    "latitude"
    t.float    "longitude"
    t.integer  "user_id"
    t.string   "image"
    t.integer  "year"
    t.integer  "miles"
    t.string   "transmission"
    t.string   "color"
    t.string   "cylinder"
    t.string   "fuel"
    t.string   "drive"
    t.string   "address"
    t.boolean  "wholesale",         default: false
    t.integer  "price",             default: 0
    t.string   "newused"
    t.string   "vin"
    t.string   "stocknumber"
    t.string   "model"
    t.string   "trim"
    t.string   "enginedescription"
    t.string   "interiorcolor"
    t.string   "options"
    t.string   "imagefront"
    t.string   "imageback"
    t.string   "imageleft"
    t.string   "imageright"
    t.string   "frontinterior"
    t.string   "rearinterior"
    t.string   "bodytype"
    t.string   "make"

我的搜索表单使用户能够搜索newused,make,model,price,near和model。

<div id="Make" class="tabcontent1">
            <section class="formclass">

              <!-- f.select :transmission, ['Automanual','Automatic','Automatic 4 Speed','Automatic 5 Speed','Automatic 6 Speed','CVT','Manual'] -->
                <h3 style = "color:#F00000; text-align: center;"><strong><%= @carcount %> CARS LISTED!</strong></h3>

                <hr>

                <h3 style = "color:#7C064D;"><span class="glyphicon glyphicon-search"></span> <strong>SEARCH CARS FOR SALE BY MAKE</strong></h3>
                <%= form_tag search_listings_path, method: :get, class: 'navbar-form navbar-center' do |f| %>

                <div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12"> 
                    <%= select_tag :NewUsed, "<option>New</option><option>Used</option>".html_safe, style: "width: 100%; margin: 1% 0;" %>
                </div>

                <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                    <%= select_tag :category, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
                </div>

                <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                    <%= select_tag :subcategory, include_blank: true, style: "width: 100%; margin: 1% 0;" %>
                </div>
                <!-- <div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">  -->
                    <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                        <%= text_field_tag :minprice, nil, placeholder: 'Min Price', style: "width: 100%; margin: 1% 0;" %>
                    </div>
                    <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                        <%= text_field_tag :maxprice, nil, placeholder: 'Max Price', style: "width: 100%; margin: 1% 0;" %>
                    </div>
                <!-- </div> -->
                <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                    <%= text_field_tag :location, nil, placeholder: 'Near', style: "width: 100%; margin: 1% 0;" %>
                </div>
                <div class= "col-xs-6 col-sm-6 col-lg-6 col-md-6"> 
                    <%= text_field_tag :radius, nil, placeholder: 'Radius', style: "width: 100%; margin: 1% 0;" %>
                </div>


                <div class= "col-xs-12 col-sm-12 col-lg-12 col-md-12">              
                    <%= submit_tag 'Search', class: 'btn btn-danger', style: "width: 100%;" %>
                </div>
              <% end %>    
            </section>
      </div>

搜索是Listing模型中的基本搜索方法:

def self.search(params)
        if params
            listings = Listing.all
            listings = listings.joins(:category).where("categories.name like '#{params[:category].downcase}'") if params[:category].present?
            listings = listings.joins(:subcategory).where("subcategories.name like '#{params[:subcategory].downcase}'") if params[:subcategory].present?
            listings = listings.where("listings.NewUsed = '#{params[:NewUsed][0].upcase}'") if params[:NewUsed].present?
            listings = listings.where("price >= ?", "#{params[:minprice]}") if params[:minprice].present?           
            listings = listings.where("price <= ?", "#{params[:maxprice]}") if params[:maxprice].present?           


            if params[:radius].present?
                listings = listings.near(params[:location],params[:radius]) if params[:location].present?
            else
                listings = listings.near(params[:location],200) if params[:location].present?
            end

            listings

        else
            all
        end

    end
  • 我想为用户提供sql / pgsql聚合过滤器。聚合 根据更广泛的搜索结果生成过滤器。
  • 如果显示有20辆车,我想生成一个过滤器链接 左边显示了每种传输类型和数量的链接 (如动态过滤器),单击时,应过滤搜索 结果进一步。它还提供了删除链接
  • 在模型中使用范围会产生静态过滤器,我希望根据显示的结果自动生成静态过滤器。 像弹性搜索中的聚合。
  • 另外,我希望它允许同时选择多个过滤器。
  • 我想知道在模型,控制器和视图中添加什么才能做到这一点。只是一个简单的例子也可以。

用于所有航空公司,酒店,汽车销售网站。请帮帮我,或者至少向我展示一些方向。

0 个答案:

没有答案