Solr使用-创建集合表、存储、查询
上一章我们讲到了Solr的安装和部署,最后的结果是能看到Solr服务器启动状态,如果我们要使用Solr进行开发,还需要在Solr上创建数据集合,然后使用数据集合存储我们业务数据,然后进行查询、排序等等。再下一步就是使用Java环境进行开发了。
(1)创建Solr数据集合
1: 添加数据集合文件
如果你看到了solr主界面,说明你完成了第一步。这一步只能说明你打好了solr的基础,下一步将是见证奇迹的时刻。
首先进入tomcat/webapps/solr/solr/目录,然后创建一个test目录,在test目录中再创建conf目录,
可以从solr源码获取elevate.xml、schema.xml、solrconfig.xml三个文件,拷贝到conf目录下,也可以通过下面的方式配置。
2. 增加数据集合



3. schema.xml格式要求
回顾我们test目录,在conf目录下,有很多文件,其中最关键的就是 【schema.xml】,这个是test这个数据集合能存储数据类型和格式的定义文件,如果不符合,存储就会失败,我们来看看【schema.xml】的内容:
<?xml version="1.0" encoding="UTF-8" ?> <schema name="example" version="1.5"> <field name="_version_" type="long" indexed="true" stored="true"/> <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <field name="name" type="string" indexed="true" stored="true" /> <field name="price" type="double" indexed="true" stored="true" /> <field name="display_picture" type="string" indexed="true" stored="true" /> <field name="release_time" type="long" indexed="true" stored="true" /> <field name="release_state" type="int" indexed="true" stored="true" /> <field name="deals" type="long" indexed="true" stored="true" /> <field name="hits" type="long" indexed="true" stored="true" /> <uniqueKey>id</uniqueKey> <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" /> <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0" /> <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0" /> <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0" /> <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" positionIncrementGap="0" /> <fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0" /> </schema>
讯享网

<schema>标签下的<field>标签表示字段类型,其中_version_是系统自带的,类似于HBase中的时间字段。
<uniqueKey>标签定义那个字段是这个数据集合的主键,存储时是唯一的,类似Oralce、MySQL的主键,区别就是重复存储时,会覆盖,而Oracle、MySQL会报错。
所有出现在JSON、XML中提交的数据,字段名必须在这个里面能找到,刚才我们手动提交的那个demo中,由于【title】字段在这个schema中没有定义,所以存储失败了。
4. 数据存储。

5. 数据查询(检索)
在【Query】菜单中,可以进行数据查询操作,主要是使用Solr的查询规矩语句就能将数据检索出来了。支持全量查询【*:*】和部分查询【name:test1】以及多条件查询【id:1 AND name:test1】,更详细的介绍可以百度。查询检索这个方式与关系型数据库有区别,但是很多思路是一样的,不过大数据量下,Solr支持复杂查询以及中文分词后查询的性能和效率会比传统关系型数据高很多。具体怎么玩,我们一步步来。


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/93333.html