繁体   English   中英

Elasticsearch高级搜索

[英]Elasticsearch advanced search

我找到了Elasticsearch宝石并想使用它。 例如,我有:

app/controller/search_controller.rb

def search
  if params[:q].nil?
    @articles = []
  else
    @articles = Article.search params[:q]
  end
end

查看app/views/search/search.html.erb

<h1>Articles Search</h1>

<%= form_for search_path, method: :get do |f| %>
  <p>
    <%= f.label "Search for" %>
    <%= text_field_tag :q, params[:q] %>
    <%= submit_tag "Go", name: nil %>
  </p>
<% end %>

<ul>
  <% @articles.each do |article| %>
    <li>
      <h3>
        <%= link_to article.title, controller: "articles", action: "show", id: article._id%>
      </h3>
    </li>
  <% end %>
</ul>

并在config/routes.rb搜索路由:

get 'search', to: 'search#search'

我想创建高级搜索。 例如,当我在搜索行中编写#cucumber时,我想获得以下行: localhost:3000/tagged?tag=cucumber 我需要按标签显示产品。 怎么做?

我将更愿意在您的操作中这样做。

tags = params[:q].present? ? params[:q].scan(/#[^ #]+/).collect{|tag| tag.gsub(/#/, '')} : []

现在您可以在搜索功能中使用此标签数组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM