2015年8月16日10:02:46
今天开始学习Solr,学习任何一个开源软件都要先了解其基本概念和用途。
Solr简介:
Solr是Apache Lucene的一个子项目。Lucene为全文搜索功能提供了完备的API,但它只作为一个API库存在,而不能直接用于搜索。因此,Solr基于Lucene构建了一个完整的搜索引擎,它可以为搜索引擎添加文档,对文档内容进行分析,并为用户提供搜索功能,在此基础上提供了一个扩展功能,如hit highline, suggetion等。如果说Lucene是引擎,那么Solr就是安装了Lucene引擎的汽车。
Solr采用Lucene搜索库为核心,提供全文索引和搜索开源企业平台,提供REST的HTTP/XML和JSON的API。
必须理解的基本概念:
1 索引(Indexing)的概念。
2 Solr中的基础数据结构Document的概念
一个索引由一个或多个Documents组成,一个Document有多个Filed。对应于传统数据库的术语就是一个表有多条记录,每条记录有多个字段。
3 模式(Schema)的概念。
模式定义了字段的数据类型,主键、唯一键、哪些字段被索引及如何索引……
4 域(Filed)概念。
具体介绍参考
http://www.solrtutorial.com/basic-solr-concepts.html
由Demo入手学习Solr使用方法。
1 下载
Download Solr 5.2 http://lucene.apache.org/solr/mirrors-solr-latest-redir.html
从其解压包中的solr-5.2.1\example\films文件夹入手学习Demo。
查看README可知,这是一个构建Solr数据库的Demo。其中film_data_generator.py 文件是用于生成films的三种格式–csv、json、xml的Python脚本。README文件具体讲述了如何利用这三种格式的文件进行Solr库的构建。
代码注释如下:
Steps:
1 * Start Solr:启动Solr服务。
bin/solr start
2 * Create a “films” core: 创建一个索引 -c 指的是collection,是Index包含Documents的意思
bin/solr create -c films
3 * Set the schema on a couple of fields that Solr would otherwise guess differently (than we’d like) about:
这是一种修改schema的方式,一般情况下Solr会根据入库的Filed的值推断其数据类型,对于可能有歧义的Filed需要用户指定其数据类型。
curl不懂的话参考:
http://yonik.com/solr/solr-prerequisites/
http://ju.outofmemory.cn/entry/84875
curl命令
curl http://localhost:8983/solr/films/schema -X POST -H ‘Content-type:application/json’ –data-binary ‘{
“add-field” : {
“name”:”name”,
“type”:”text_general”,
“stored”:true
},
“add-field” : {
“name”:”initial_release_date”,
“type”:”tdate”,
“stored”:true
}
}’
入库前必须自定义schema!
4 * Now let’s index the data, using one of these three commands:
三种格式的文件入库方式。
- JSON: bin/post -c films example/films/films.json
- XML: bin/post -c films example/films/films.xml
- CSV: bin/post \
-c films \
example/films/films.csv \
-params "f.genre.split=true&f.directed_by.split=true&f.genre.separator=|&f.directed_by.separator=|"
在Windows平台下试验post命令无效。那么这个post是不是需要在Linux下运行呢,在cygwin(Shell模拟器)下依然没有反应,因此放弃这种方式。
改为下面的方式:在solr-5.2.1\example\exampledocs存在一个post.jar包。
查看java -jar post.jar -help 可知入库方式应为:
Examples:
xml入库
java -Dc=gettingstarted -jar post.jar *.xml
删除Document
java -Ddata=args -Dc=gettingstarted -jar post.jar ‘42 ‘
CVS入库
java -Dtype=text/csv -Dc=gettingstarted -jar post.jar *.csv
其他
java -Ddata=stdin -Dc=gettingstarted -jar post.jar < hd.xml
java -Ddata=web -Dc=gettingstarted -jar post.jar http://example.com/
java -Dtype=application/json -Dc=gettingstarted -jar post.jar *.json
一些自动入库的命令
java -Dauto -Dc=gettingstarted -jar post.jar *
java -Dauto -Dc=gettingstarted -Drecursive -jar post.jar afolder
java -Dauto -Dc=gettingstarted -Dfiletypes=ppt,html -jar post.jar afolder
5 * Let’s get searching! 入库之后的搜索
- Search for ‘Batman’:
http://localhost:8983/solr/films/query?q=name:batman
* If you get an error about the name field not existing, you haven't yet indexed the data
* If you don't get an error, but zero results, chances are that the _name_ field schema type override wasn't set
before indexing the data the first time (it ended up as a "string" type, requiring exact matching by case even).
It's easiest to simply reset the environment and try again, ensuring that each step successfully executes.