我正在使用Sunspot来索引和搜索Rails项目中的几个模型,我需要根据模型与模型的HABTM关联来限制结果Department
.这是因为用户可能没有权限查看所有部门的记录,因此不应返回这些部门的结果.
以下是两个模型的重要部分:
class Message < ActiveRecord::Base
has_many :comments, dependent: :destroy
has_and_belongs_to_many :departments
searchable do
text :title, :body
text :comments do
comments.map(&:body)
end
date :created_at
integer :department_ids, using: :department_ids, references: Department, multiple: true
end
end
class Document < ActiveRecord::Base
has_and_belongs_to_many :departments
searchable do
text :name
date :created_at
integer :department_ids, using: :department_ids, references: Department, multiple: true
end
end
Run Code Online (Sandbox Code Playgroud)
这是搜索控制器代码:
class SearchController < ApplicationController
def index
# These arrays are created here for the sake of this example
document_permitted_departments = [1, …
Run Code Online (Sandbox Code Playgroud) 我正在使用acts_as_solr将Rails应用程序转换为太阳黑子.
该应用程序使用在acts_as_solr中公开的solr中的字段搜索功能.你可以给它一个像这样的查询字符串:
title:"The thing to search"
Run Code Online (Sandbox Code Playgroud)
它会在标题字段中搜索该字符串.
在转换为太阳黑子时,我正在解析查询字符串的字段特定部分,我需要动态生成搜索块.像这样的东西:
Sunspot.search(table_clazz) do keywords(first_string, :fields => :title) keywords(second_string, :fields => :description) ... paginate(:page => page, :per_page => per_page) end
如果查询需要,还需要执行持续时间(秒,整数)范围和否定,这很复杂.
在当前系统上,用户可以在标题中搜索某些内容,不包括在其他字段中包含其他内容的记录,并按持续时间确定范围.
简而言之,如何动态生成这些块?
我正在调试我的Solr架构,我希望看到标记特定字段的结果.
举一个简单的例子,如果我有:
<fieldType name="text" class="solr.TextField" omitNorms="false">
<analyzer>
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StandardFilterFactory"/>
<filter class="solr.LowerCaseFilterFactory"/>
<filter class="solr.PorterStemFilterFactory"/>
<filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front"/>
</analyzer>
</fieldType>
Run Code Online (Sandbox Code Playgroud)
我用一个值索引了一个字段"Hello, worlds!"
,我希望看到以下内容:
hello world he el ll lo hel ell llo hell ello hello wo or rl ld wor orl rld worl orld
确保所有内容都按照我的设想进行标记化.
这有可能吗?
我正在尝试在一个小的Rails应用程序中安装Sunspot,完全遵循gem设置说明,但每当RSolr::Error::Http: RSolr::Error::Http - 404 Not Found
我尝试索引数据时遇到错误.我可以用一个新的应用程序重现这个; 以下是我遵循的确切步骤:
创建一个新的Rails 4.2.5应用程序:
$ rails new test_sunspot
$ cd test_sunspot/
$ spring stop # spring can cause `generate` commands to hang
$ rails g model Thing title:string
$ rake db:migrate
$ rails c
> Thing.create!(title: "Cats")
> Thing.create!(title: "Pizza")
> exit
Run Code Online (Sandbox Code Playgroud)
向模型添加太阳黑子索引:
class Thing < ActiveRecord::Base
searchable do
text :title
end
end
Run Code Online (Sandbox Code Playgroud)
将Sunspot添加到Gemfile:
...
gem 'sunspot_rails', '2.2.2'
gem 'sunspot_solr', '2.2.2'
...
Run Code Online (Sandbox Code Playgroud)
安装,启动和重新编制太阳黑子:
$ bundle install
$ rails g sunspot_rails:install # …
Run Code Online (Sandbox Code Playgroud) 我正在使用Sunspot生成很多我的应用程序索引和概述.
在这个应用程序中,我有2个模型,它们具有父/子一对多的关系.使用太阳黑子我索引父母拥有的孩子数量,因此可用于排序,范围等.
但是,当我更改子模型时,父模型不会自动重新编制索引(因为它没有更改).通过子节点上的call_back强制parent.save也不会强制索引.
所以在我开始乱砍之前:
当子模型被更改/添加时,在Sunspot中强制对父类执行索引操作的最佳方法是什么?
我使用的太阳黑子用于搜索和使用地理编码的地址,然后计算距离,Geokit-Rails3中.
class Product < ActiveRecord::Base
belongs_to :store
searchable do
text :name
end
end
class Store < ActiveRecord::Base
acts_as_mappable
geocoded_by :address, :latitude => :lat, :longitude => :lng
attr_accessible :lat, :lng, :address
has_many :products
end
Run Code Online (Sandbox Code Playgroud)
输入要搜索的产品时我想要的是能够在另一个字段内键入地址以搜索半径为30英里的给定区域中的产品.
这是我的控制器,它允许我搜索产品:
class SearchController < ApplicationController
def index
@search = Product.search do |q|
q.fulltext params[:search]
end
@products = @search.results
end
end
Run Code Online (Sandbox Code Playgroud)
所以我相信在完成之后表单会看起来像这样:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search]" %>
<%= submit_tag "Search", :name => …
Run Code Online (Sandbox Code Playgroud) 我正在尝试执行搜索,随机排序结果,并且只返回一些结果,而不是所有匹配.类似限制的东西(2)我尝试过使用Solr param'rows',但似乎没有做任何事情:
@featured_articles = Article.search do
with(:is_featured, true)
order_by :random
adjust_solr_params do |params|
params[:rows] = 2
end
end
Run Code Online (Sandbox Code Playgroud)
@ featured_articles.total应为2,但返回的次数超过2
如何获得随机固定数量的结果?
如何在SunSpot Solr中正确搜索多个模型?
档案模型
has_one :match
searchable do
string :country
string :state
string :city
end
Run Code Online (Sandbox Code Playgroud)
匹配模型
belongs_to :profile
searchable do
string :looking_for_education
integer :age_from
integer :age_to
end
Run Code Online (Sandbox Code Playgroud)
ProfilesController#指数
def index
@search = Sunspot.search Profile, Match do
with(:country, params[:country])
with(:state, params[:state])
with(:looking_for_education, params[:looking_for_education]) <= from the 2nd model
end
@profiles = @search.results
end
Run Code Online (Sandbox Code Playgroud)
这失败了:
Using a with statement like
with(:age).between(params[:age_from]..params[:age_to])
undefined method `gsub' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
删除
with(:age).between(params [:age_from] .. params [:age_to])行然后尝试
然后它试图加载
view app/views/educators/educator.html.haml
Run Code Online (Sandbox Code Playgroud)
它不存在(我只使用
/app/views/profiles/_profile.html.haml
Run Code Online (Sandbox Code Playgroud)
显示个人资料
什么是使用太阳黑子和solr的红宝石轨道上的优秀开源项目,以更先进的方式来看看?也许我可以在那里找到答案.如果这个问题导致了这个问题,那么这个方向的任何答案也将被接受.
当我运行测试时,大多数测试都失败,并显示以下错误。
Failure/Error: let(:user){ FactoryGirl.create(:user) }
RSolr::Error::Http:
RSolr::Error::Http - 500 Internal Server Error
Error: {msg=SolrCore 'test' is not available due to init failure: Error opening new searcher,trace=org.apache.solr.common.SolrException: SolrCore 'test' is not available due to init failure: Error opening new searcher
at org.apache.solr.core.CoreContainer.getCore(CoreContainer.java:745)
at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:299)
at org.apache.solr.servlet.SolrDispatchFilter.doFilter(SolrDispatchFilter.java:207)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1419)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:455)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1075)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:384)
URI: http://localhost:8981/solr/test/update?wt=ruby
Request Headers: {"Content-Type"=>"text/xml"}
Request Data: "<?xml version=\"1.0\" encoding=\"UTF-8\"?><add><doc><field name=\"id\">User 13497</field><field name=\"type\">User</field><field name=\"type\">ActiveRecord::Base</field><field name=\"class_name\">User</field><field name=\"phone_number_text\">+11111111200</field><field name=\"name_text\">111.111.1200</field></doc></add>"
Run Code Online (Sandbox Code Playgroud)
开发工作正常。我认为这可能是一个损坏的测试索引,所以:
$ rm -rf …
Run Code Online (Sandbox Code Playgroud) 我一直试图找出一个工作流程,即使开发实例正在运行,我的 Minitest 套件也会启动第二个 Solr 实例进行功能测试。但是,我在启动服务器时遇到了问题(即当我在测试之外启动它们时)。
要启动我正在使用的服务器:
RAILS_ENV=development bin/rake sunspot:solr:start
RAILS_ENV=test bin/rake sunspot:solr:start
Run Code Online (Sandbox Code Playgroud)
但是,无论哪个服务器第二次启动都会被锁定。任何在测试中或仅在开发中访问服务器的尝试都会产生此错误:
RSolr::Error::Http - 500 Internal Server Error
Error: {msg=SolrCore 'test& 'is not available due to init failure: Index locked for write for core 'test'. Solr now longer supports forceful unlocking via 'unlockOnStartup'. Please verify locks manually!,trace=org.apache.solr.common.SolrException: SolrCore 'test' is not available due to init failure: Index locked for write for core 'test'. Solr now longer supports forceful unlocking via 'unlockOnStartup'. Please verify locks manually!
at org.apache.solr.core.CoreContainer.getCore(CoreContainer.java:974) …
Run Code Online (Sandbox Code Playgroud)