在 上一篇 中, 我们安装并启动了solr服务器, 在这一篇中, 我们将从0开始创建我们自己的搜索服务
在本篇中, 将会介绍如何创建自己的搜索引擎, 并往其中插入索引数据
假设情景
我们从动漫花园爬取了所有的动画分类数据, 然后需要一个搜索引擎, 来帮助我们索引数据
创建core
1
| > ./bin/solr create_core -c my_core
|
配置
在创建完core之后, 可以在 server/solr 下看到生成了一个名为 my_core 的目录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| . ├── conf │ ├── currency.xml │ ├── elevate.xml │ ├── lang │ ├── managed-schema │ ├── params.json │ ├── protwords.txt │ ├── solrconfig.xml │ ├── stopwords.txt │ └── synonyms.txt ├── core.properties └── data ├── index ├── snapshot_metadata └── tlog
|
其中最关键的2个文件为 conf/managed-schema 和 conf/solrconfig.xml
managed-schema
在 managed-schema 中, 定义我们的数据类型, 记得 保存后重启core
1 2 3 4 5 6 7 8 9 10 11
| <field name="id" type="int" indexed="true" stored="true" required="true" multiValued="false" />
<field name="fansub_name" type="string" indexed="true" stored="true" required="false" multiValued="false" />
<field name="fansub_id" type="int" indexed="true" stored="true" required="false" multiValued="false" />
<field name="title" type="string" indexed="true" stored="true" required="false" multiValued="false" />
<copyField source="fansub_name" dest="_text_"/> <copyField source="title" dest="_text_"/>
|
几种属性解释
- type 对应字段类型
- indexed 是否需要索引数据, 如果为false则无法通过该字段检索
- stored 是否在solr中保存该值, 如果为false则搜索结果中不会返回该字段
- multiValued 是否为多值
其中 copyField 字段的意思是将 fansub_name 和 title 放到 _text_ 字段中去, 使得查询 _text_ 字段时可以同时检索 fansub_name 和 title
创建索引
1 2 3 4 5 6 7 8 9 10 11 12
| curl -X POST -H "Content-Type: application/json" -d '{ "add": { "doc": { "id": 11243, "title": "[澄空学园&华盟字幕社] 四月是你的谎言 第01-22话 MKV 720p 简体外挂 合集 特典付", "fansub_name": "澄空学园", "fansub_id": 58 }, "commitWithin": 1000, "overwrite": true } }' "http://localhost:8983/solr/my_core/update/?wt=json"
|
使用solrAdmin查看, 应该可以看到刚才创建的记录

下一篇 将介绍如何使用DataImport来构建索引