首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

sunspot使用(一)

2013-11-23 
sunspot使用(1)**sunspot使用**1. gemfile???? add gem sunspot_rails

sunspot使用(1)



**sunspot使用**
=================================

1. gemfile
???? add gem 'sunspot_rails' to the gemfile
2. excute bundle install
3. generate sunspot config file, generate config/sunspot.yml
??? generate solr config files in the root path
???? rails g sunspot_rails:install
??? rake sunspot:solr:start
??? rake sunspot:solr:run

察看solr服务: http://127.0.0.1:8982/solr/

---------------------------------------------------------------------------
5. 在modle中设置索引

class Article < ActiveRecord::Base
? attr_accessible :content, :name, :published_at
? has_many :comments

? searchable do
? ??? text :name, :content
? end

end


服务器建立索引:rake sunspot:reindex
-----------------------------------------------------------------------------
$ :~/github/test_sunspot/solr/data/development/index$ ls -lh
总用量 40K
-rw-rw-r-- 1 zhaol-a zhaol-a?? 69 11月? 4 21:43 _0.fdt
-rw-rw-r-- 1 zhaol-a zhaol-a?? 44 11月? 4 21:43 _0.fdx
-rw-rw-r-- 1 zhaol-a zhaol-a?? 53 11月? 4 21:43 _0.fnm
-rw-rw-r-- 1 zhaol-a zhaol-a? 376 11月? 4 21:43 _0.frq
-rw-rw-r-- 1 zhaol-a zhaol-a?? 14 11月? 4 21:43 _0.nrm
-rw-rw-r-- 1 zhaol-a zhaol-a? 460 11月? 4 21:43 _0.prx
-rw-rw-r-- 1 zhaol-a zhaol-a?? 48 11月? 4 21:43 _0.tii
-rw-rw-r-- 1 zhaol-a zhaol-a 1.7K 11月? 4 21:43 _0.tis
-rw-rw-r-- 1 zhaol-a zhaol-a? 266 11月? 4 21:43 segments_2
-rw-rw-r-- 1 zhaol-a zhaol-a?? 20 11月? 4 21:43 segments.gen


打开文件 /usr/local/solr/solr.xml
这里配置index目录
-------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="true">
? <cores adminPath="/admin/cores" defaultCoreName="core">
??? <core name="core" instanceDir="core/"/>
??? <core name="core_0" instanceDir="core_0/" dataDir="core_0_idx_data"/>
??? <core name="core_code_search" instanceDir="core_code_search/"/>
? </cores>
</solr>

config/sunspot.yml配置如下:
前台使用的是core_0, 配置为path: /solr/core_0
-------------------------------------------------------------------------
production:
? solr:
??? hostname: 192.168.1.80
??? port: 8983
??? log_level: WARNING

development:
? solr:
??? hostname: 192.168.133.51
??? port: 8081
??? path: /solr/core_0
??? log_level: INFO

test:
? solr:
??? hostname: 192.168.22.80
??? port: 8983
??? log_level: INFO

打开文件 /usr/local/solr/core_0/conf/data-config.xml
----------------------------------------------------------------

<dataConfig>
#数据库链接配置:
<dataSource driver="org.postgresql.Driver"? url="jdbc:postgresql://192.168.11.124/gxx:5432"? user="gcj" password="xx" transactionIsolation="TRANSACTION_READ_COMMITTED" holdability="CLOSE_CURSORS_AT_COMMIT"/>

modle配置:
--------------------------------------------------------------------------
class GuidePrice < ActiveRecord::Base

? searchable do
? ??? text :name, :stored => true
??????? text :sepecification, :stored => true
??????? text :search_key
? ??? integer :id, :stored => true
? ??? string :location_area_code, :stored => true
? ??? integer :base_material_type_id, :stored => true
? ??? integer :guide_price_package_material_id , :stored => true#trend
? ??? integer :year, :stored => true
? ??? integer :month, :stored => true
? ??? float :price, :stored => true
? ??? string :unit, :stored => true
? ??? string :code, :stored => true
? ??? string :term_status, :stored => true #finish
? ??? string :guide_price_package_material_status, :stored => true #finish

? end

end
-----------------------------------------------------------------------------------------

#创建索引:
对应schame.xml文件配置data-config.xml, name命名规则配置如下
查询的注意设置:class_name, base_class_name对应的modle

** 'GuidePrice' class_name, 'ActiveRecord::Base' base_class_name**
** class GuidePrice < ActiveRecord::Base**

*_s == store #store
*_i == 字段类型为integer
*_is == 字段类型为integer, store="true"
*_ss == 字段类型为string, store="true"
*_texts == 字段类型为text, store="true"
...
注意: *_s 的store="false"
------------------------------------------------------------------
<document>
<entity name="guide_prices" pk="id" query="select 'GuidePrice'||' '||id as class_id, 'GuidePrice' class_name, 'ActiveRecord::Base' base_class_name, name, sepecification, location_area_code, base_material_type_id, guide_price_package_material_id, year, month, price, unit, code, term_status, guide_price_package_material_status from guide_prices_view">
<field column="class_id" name="id" />
<field column="class_name" name="class_name" />
<field column="guide_price_package_material_status" name="guide_price_package_material_status_ss" />
</entity>
</document>


===========================================
注意定义searchable
**正确写法**
在索引中可以正常保存name 和content信息
```ruby
class Article < ActiveRecord::Base

? searchable do
? ??? text :content, :stored => true
? ??? text :name,? :stored => true
? end

end
```


**错误写法**
content不能被保存在索引中
```ruby
class Article < ActiveRecord::Base

? searchable do
? ??? text :name, :content, stored => true
? end

end
```
===========================================

当访问:http://0.0.0.0:3000/articles?search=百度 时,转发给solr服务器进行搜索

SOLR Request (10.9ms)? [

path=#<RSolr::Client:0xb5fb38c0>
parameters={
data: fq=type%3AArticle&q=%E7%99%BE%E5%BA%A6&fl=%2A+score&qf=name_text+content_text&defType=dismax&start=0&rows=30, method: post,
params: {:wt=>:ruby},
query: wt=ruby,
headers: {"Content-Type"=>"application/x-www-form-urlencoded; charset=UTF-8"},
path: select,
uri: http://localhost:8982/solr/select?wt=ruby,
open_timeout: ,
read_timeout: ,
retry_503: ,
retry_after_limit: }

]
http://localhost:8982/solr/select?wt=ruby&fq=type%3AArticle&q=%E7%99%BE%E5%BA%A6&fl=*+score&qf=name_text+content_text&defType=dismax&start=0&rows=30

**uri + data**
{'responseHeader'=>{
??? 'status'=>0,
??? 'QTime'=>10,
??? 'params'=>{
??? ??? 'fl'=>'* score',
??? ??? 'start'=>'0',
??? ??? 'q'=>'百度','
??? ??? qf'=>'name_text content_text',
??? ??? 'wt'=>'ruby',
??? ??? 'fq'=>'type:Article',
??? ??? 'defType'=>'dismax',
??? ??? 'rows'=>'30'}
??? },
'response'=>{
??? 'numFound'=>4,
???? 'start'=>0,
??? 'maxScore'=>2.1419475,
??? 'docs'=>[
??? ??? {'id'=>'Article 4','score'=>2.1419475},
??? ??? {'id'=>'Article 3','score'=>0.43654194},
??? ??? {'id'=>'Article 1','score'=>0.42706013},
??? ??? {'id'=>'Article 2','score'=>0.33079934}
??? ]
}}


solr结构图:
------------------------------------------------------------------------------------
.
├── conf
│?? ├── admin-extra.html
│?? ├── elevate.xml
│?? ├── mapping-ISOLatin1Accent.txt
│?? ├── protwords.txt
│?? ├── schema.xml
│?? ├── scripts.conf
│?? ├── solrconfig.xml
│?? ├── spellings.txt
│?? ├── stopwords.txt
│?? └── synonyms.txt
├── data
│?? └── development
│?????? ├── index
│?????? │?? ├── _4.fdt
│?????? │?? ├── _4.fdx
│?????? │?? ├── _4.fnm
│?????? │?? ├── _4.frq
│?????? │?? ├── _4.nrm
│?????? │?? ├── _4.prx
│?????? │?? ├── _4.tii
│?????? │?? ├── _4.tis
│?????? │?? ├── segments_6
│?????? │?? └── segments.gen
│?????? └── spellchecker
│?????????? ├── segments_1
│?????????? └── segments.gen
└── pids
??? └── development
??????? └── sunspot-solr-development.pid

???

热点排行