为针对于SimpleDB的Rails应用程序配置solr支持
Rails Solr Sunspot Sunspot_rails Nosql
配置环境
- Server: Ubuntu 10.04
- solr version: 1.4.1
-
sunspot_rails version: 1.2.0
-
Tomcat6_home: /var/lib/tomcat6
-
$SOLR_HOME = /home/ubuntu/java_app/apache-solr/example/solr
注:存放solr.war,conf/文件夹中存放solr的配置文件
注:存放索引文件所在的位置
步骤一:安装Tomcat6
tomcat的安装可以通过apt-get完成
步骤二:配置Solr
1. 解压apache-solr-xxx.zip,存放在家目录下在java_app文件夹中,此处命名为apache-solr,将example/webapps下的solr.war拷贝到example/solr目录下,
2. 添加sorl.xml
进入/var/lib/tomcat6/conf/Catalina/localhost目录,新建solr.xml,编写一下内容:
- <?xml version="1.0" encoding="utf-8"?>
- <Context docBase="$SOLR_HOME/solr.war"
- debug="0" crossContext="true">
- <Environment name="solr/home" type="java.lang.String"
- value="$SOLR_HOME" override="true"/>
- </Context>
3. 配置索引文件存放的位置,需要修改$SOLR_HOME/conf/solrconfig.xml
- 修改<dataDir>${solr.data.dir:./solr/data}</dataDir>为<dataDir>${solr.data.dir:$INDEX_HOME }</dataDir>
- sudo chown –R tomcat6:tomcat6 solr
4. 验证配置
- 重启Apache, 访问localhost:8080/solr,如果能正常访问solr的页面,证明之前的配置都正常。
i. 到$SOLR_HOME/exampledocs目录下,执行以下命令为solr.xml文件建立索引
ii. 在http://localhost:8080/solr/admin后台检索界面输入“solr”进行检索,有返回结果证明已经正确建立索引。
5. 配置中文分词功能,需要修改$SOLR_HOME/solr/conf/schema.xml
- <fieldType name="text" class="solr.TextField" >
- <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
- <analyzer type="index">
- <tokenizer
- class="org.wltea.analyzer.solr.IKTokenizerFactory"
- isMaxWordLength="false"/>
- <filter class="solr.StopFilterFactory"
- ignoreCase="true" words="stopwords.txt"/>
- <filter class="solr.WordDelimiterFilterFactory"
- generateWordParts="1"
- generateNumberParts="1"
- catenateWords="1"
- catenateNumbers="1"
- catenateAll="0"
- splitOnCaseChange="1"/>
- <filter class="solr.LowerCaseFilterFactory"/>
- <filter class="solr.EnglishPorterFilterFactory"
- protected="protwords.txt"/>
- <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
- </analyzer>
- <analyzer type="query">
- <tokenizer
- class="org.wltea.analyzer.solr.IKTokenizerFactory" isMaxWordLength="false"/>
- <filter class="solr.StopFilterFactory"
- ignoreCase="true" words="stopwords.txt"/>
- <filter class="solr.WordDelimiterFilterFactory"
- generateWordParts="1"
- generateNumberParts="1"
- catenateWords="1"
- catenateNumbers="1"
- catenateAll="0"
- splitOnCaseChange="1"/>
- <filter class="solr.LowerCaseFilterFactory"/>
- <filter class="solr.EnglishPorterFilterFactory"
- protected="protwords.txt"/>
- <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
- </analyzer>
- </fieldType>
6. 获得中文分词包IKAnalyzer3.2.8.jar,并拷贝到$TOMCAT_HOME/webapps/solr/WEB-INF/lib中,并修改其拥有者和拥有组为tomcat6
7. 验证中文分词功能,在solr.xml中增加中文,然后搜索该词,过程同第5步。
步骤三:add sunspot support to Rails
1. 修改gemfile,增加一行内容
然后执行
2. 生成sunspot配置文件,在Rails项目中执行以下命令:
- rails generate sunspot_rails:install
3. 修改sunspot.yml文件
- production:
- solr:
- hostname: localhost
- port: 8080
- path: '/solr/'
- log_level: WARNING
-
- development:
- solr:
- hostname: localhost
- port: 8080
- path: '/solr/'
- log_level: INFO
-
- test:
- solr:
- hostname: localhost
- port: 8080
- path: '/solr/'
- log_level: WARNING
4. 为Model增加solr的支持,这里以Post类为例进行说明
- 为simpleRecord编写适配器,新增一个SunspotHelper模块,编写两个适配器类
- require 'rubygems'
- require 'sunspot'
-
- module SunspotHelper
- class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
- def id
- if(@instance.class.to_s == "String")
- @instance
- else
- @instance.id
- end
- end
- end
-
- class DataAccessor < Sunspot::Adapters::DataAccessor
- def load(id)
-
- @clazz.find(id)
- end
-
- end
- end
- Sunspot::Adapters::InstanceAdapter.register(SunspotHelper::InstanceAdapter, Post, String)
- Sunspot::Adapters::DataAccessor.register(SunspotHelper::DataAccessor, Post, String)
- Sunspot.setup(Post) do
- text :title, :body
- time :date, :trie => true
- end
-
- def save
- super()
- Sunspot.index(self)
- Sunspot.commit
- end
- 将~/.rvm/gems/ruby-1.9.2-p180/gems/sunspot-1.2.0/solr/solr/conf下的schema.xml文件拷贝到$SOLR_HOME/conf下替换掉原来的schema.xml,并按上一步中的第5点配置中文分词功能。
5.
在
rails
中进行搜索
,
可在
console
中进行以下调试
- post = Post.first
- => ...
- post.save
- => {"responseHeader"=>{"status"=>0, "QTime"=>480}}
- search = Sunspot.search(Post) { keywords '想想' }
- => <Sunspot::Search:{:fq=>["type:Post"] …
- r = search.results
- => [
配置成功。