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、查询