Solr5.1案例文檔導入

1、創建內核

[root@hadoop exampledocs]# cd /hadoop/solr/
[root@hadoop solr]# ./bin/solr create -c jcg -d basic_configs

Setup new core instance directory:
/hadoop/solr/server/solr/jcg

Creating new core 'jcg' using command:
http://localhost:8983/solr/admin/cores?action=CREATE&name=jcg&instanceDir=jcg

{
  "responseHeader":{
    "status":0,
    "QTime":191},
  "core":"jcg"}

core創建成功之後,會在server/solr目錄下生成名爲jcg的文件夾,這就是我們創建的jcg core的配置,jcg目錄下有兩個文件夾和一個文件,如下所示。
[root@hadoop solr]# ls server/solr/
configsets core_one jcg README.txt solr.xml zoo.cfg
[root@hadoop solr]# ls server/solr/jcg/
conf core.properties data
然後在solr控制檯就可以看到jcg了。
在這裏插入圖片描述
根據solr提供的示例,通過設置一個schema,然後導入books.csv(example/exampledocs文件夾下)文件,產生索引,然後重啓solr,利用api查詢。

[root@hadoop exampledocs]# pwd
/hadoop/solr/example/exampledocs
[root@hadoop exampledocs]# ls
books.csv            hd.xml          manufacturers.xml  monitor2.xml  post.jar     solr-word.pdf  utf8-example.xml
books.json           ipod_other.xml  mem.xml            monitor.xml   sample.html  solr.xml       vidcard.xml
gb18030-example.xml  ipod_video.xml  money.xml          mp500.xml     sd500.xml    test_utf8.sh
books.csv文件的內容:後續我們會根據這些內容進行查詢
[root@hadoop exampledocs]# cat books.csv 
id,cat,name,price,inStock,author,series_t,sequence_i,genre_s
0553573403,book,A Game of Thrones,7.99,true,George R.R. Martin,"A Song of Ice and Fire",1,fantasy
0553579908,book,A Clash of Kings,7.99,true,George R.R. Martin,"A Song of Ice and Fire",2,fantasy
055357342X,book,A Storm of Swords,7.99,true,George R.R. Martin,"A Song of Ice and Fire",3,fantasy
0553293354,book,Foundation,7.99,true,Isaac Asimov,Foundation Novels,1,scifi
0812521390,book,The Black Company,6.99,false,Glen Cook,The Chronicles of The Black Company,1,fantasy
0812550706,book,Ender's Game,6.99,true,Orson Scott Card,Ender,1,scifi
0441385532,book,Jhereg,7.95,false,Steven Brust,Vlad Taltos,1,fantasy
0380014300,book,Nine Princes In Amber,6.99,true,Roger Zelazny,the Chronicles of Amber,1,fantasy
0805080481,book,The Book of Three,5.99,true,Lloyd Alexander,The Chronicles of Prydain,1,fantasy
080508049X,book,The Black Cauldron,5.99,true,Lloyd Alexander,The Chronicles of Prydain,2,fantasy
[root@hadoop exampledocs]# 

1、修改server/solr/jcg/conf/manage-schema,添加如下配置

[root@hadoop exampledocs]# cd /hadoop/solr/server/solr/jcg/conf/
[root@hadoop conf]# vim schema.xml 
  <!--fields for book.csv load -->

  <field name="cat" type="text_general" indexed="true" stored="true"/>  
  
  <field name="name" type="text_general" indexed="true" stored="true"/>  
  
  <field name="price" type="tdouble" indexed="true" stored="true"/>  
  
  <field name="inStock" type="boolean" indexed="true" stored="true"/>  
  
  <field name="author" type="text_general" indexed="true" stored="true"/> 

這些配置對應books.csv文件中的一些字段field:
name:名稱,隨便定義,在查詢的時候根據定義的名稱來查詢。
type:類型
indexed:是否做索引
stored:是否存儲
這裏我們只配置了部分字段cat,name,price,inStock,author,像series_t,sequence_i,genre_s,這些字段沒有配置,這些配置在manage-schema中有默認的配置,他們就是根據字段後綴來做索引,這些字段就稱爲動態屬性字段,每一個後綴對應可以有多個這樣的動態屬性,但是字段類型需要保持和定義時一致。部分動態屬性定義如下:

<dynamicField name="*_i"  type="int"    indexed="true"  stored="true"/>
<dynamicField name="*_is" type="int"    indexed="true"  stored="true"  multiValued="true"/>
<dynamicField name="*_s"  type="string"  indexed="true"  stored="true" />

2、停止solr實例,並重新開啓。

[root@hadoop conf]# cd /hadoop/solr/
[root@hadoop solr]# ./bin/solr stop
Sending stop command to Solr running on port 8983 ... waiting 5 seconds to allow Jetty process 50254 to stop gracefully.
[root@hadoop solr]# ./bin/solr start -p 8983
Waiting to see Solr listening on port 8983 [/]  
Started Solr server on port 8983 (pid=51134). Happy searching!
     

進入example/exampledocs文件夾,導入books.csv

導入books.csv文件,使用如下命令

[root@hadoop exampledocs]# java  -Dtype=text/csv -Durl=http://192.168.1.66:8983/solr/jcg/update -jar post.jar books.csv
SimplePostTool version 5.0.0
Posting files to [base] url http://192.168.1.66:8983/solr/jcg/update using content-type text/csv...
POSTing file books.csv to [base]
1 files indexed.
COMMITting Solr index changes to http://192.168.1.66:8983/solr/jcg/update...
Time spent: 0:00:00.211

這樣創建索引的過程就完成了。

4、查詢
先看一個通配符查詢,按照名稱中帶有of的查詢,查詢出四個結果。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章