solr使用总结

来源:互联网 发布:西安行知中学张淑珍 编辑:程序博客网 时间:2024/06/01 07:26

总结一下吧,原来是在学校里学的,结果学完不能说忘了一半吧,但是真正到用的时候还是有些手足无措,把这几天遇到的问题、收获记一下

我用的是solr 4.10.3  版本,新版本没有war包了,正在研究如何启动solr


刚接到任务,把数据库表里的内容改为使用solr查询,不再使用sql语句,因为当时记得有个一键导入索引库的步骤,就修改scheam.xml增加导入的字段,弄了半天。

这里修改scheam.xml时,一定要先定义,再添加具体的

<!-- IKAnalyzer-->    <fieldType name="text_ik" class="solr.TextField">      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>    </fieldType><!--IKAnalyzer Field-->   <field name="title_ik" type="text_ik" indexed="true" stored="true" />   <field name="content_ik" type="text_ik" indexed="true" stored="false" multiValued="true"/><!--product-->   <field name="product_name" type="text_ik" indexed="true" stored="true"/>   <field name="product_price"  type="float" indexed="true" stored="true"/>   <field name="product_description" type="text_ik" indexed="true" stored="false" />   <field name="product_picture" type="string" indexed="false" stored="true" />   <field name="product_catalog_name" type="string" indexed="true" stored="true" />   <field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>   <copyField source="product_name" dest="product_keywords"/>   <copyField source="product_description" dest="product_keywords"/>


当然,我用到了ik分词器,需要先配置ik分词器,我是在windows下配置的,把ik分词器的jar包放在 tomcat/webapp/solr/WEBINF/lib下,把分词的配置文件放在WEBINF/classes下。


添加

1、查询所有商品数据。List<SearchItem> itemList = itemMapper.getItemList();//2、创建一个SolrServer对象。//3、为每个商品创建一个SolrInputDocument对象。for (SearchItem searchItem : itemList) {SolrInputDocument document = new SolrInputDocument();// 4、为文档添加域document.addField("id", searchItem.getId());document.addField("item_title", searchItem.getTitle());document.addField("item_sell_point", searchItem.getSell_point());document.addField("item_price", searchItem.getPrice());document.addField("item_image", searchItem.getImage());document.addField("item_category_name", searchItem.getCategory_name());document.addField("item_desc", searchItem.getItem_desc());// 5、向索引库中添加文档。solrServer.add(document);}solrServer.commit();//6、返回TaotaoResult。
先从数据库查询,再加入到document,最后一定要提交   字段一定要跟scheam.xml里的一致

添加到此为止,后来才想起来 添加在solr界面 有个从数据库导入的快捷操作。。如下


配置数据库与solrconfig.xml

Dataimport插件

可以批量把数据库中的数据导入到索引库中。

1.添加jar包

需要的jar包,再solr,目录的dist下,找到solr-dataimporthandler-4.7.0.jar,solr-dataimporthandler-extras-4.7.0.jar,移动到collection1下,lib包,如果没有lib包,自己创建一个即可。

除了这两个包,还需要数据库驱动包,mysql-connector-java-5.1.7-bin.jar,找不到可点击进行下载。同样放到lib包下。

2.修改solrconfig.xml,添加一个requestHandler。

找到collection1/conf目录的solrconfig.xml文件。

<requestHandler name="/dataimport"  class="org.apache.solr.handler.dataimport.DataImportHandler">     <lst name="defaults">        <str name="config">data-config.xml</str>     </lst></requestHandler>

3.创建一个data-config.xml。目录和solrconfig.xml在同一个目录下collection1\conf

<?xml version="1.0" encoding="UTF-8" ?>  <dataConfig>   <dataSource type="JdbcDataSource"             driver="com.mysql.jdbc.Driver"             url="jdbc:mysql://192.168.2.10:3306/lucene"             user="root"             password="root"/>   <document>       <entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">         <field column="pid" name="id"/>          <field column="name" name="product_name"/>          <field column="catalog_name" name="product_catalog_name"/>          <field column="price" name="product_price"/>          <field column="description" name="product_description"/>          <field column="picture" name="product_picture"/>     </entity>   </document>   </dataConfig>

数据库url,配置成你自己的数据库地址和数据库名称即可。


4.配置好后,重启tomcat。访问页面。




Dataimport就配置成功了。下面可以导入数据库数据了。

Command : full-import--全导入  data-import---导入没导入的。

Entity:选择配置文件中定义的product实体进行导入。

点击Execute 即可。执行过程中,可能你会觉得很长时间怎么还没完成。点击 Refresh Status 刷新即可。

导入完毕后进行Query 查询。


有时候点击execute没反应,可能是数据库路径没设置好




添加索引到上面就结束了,下面是搜索索引  


当时学的是query只接收一个参数查询 这一次 需要接收多个参数   就有点懵逼了  ,开始百度solr组合条件查询,终于找到了一个可以用的

http://blog.csdn.net/millery22/article/details/49658981   感谢这位博主的博客,让我顺利的查询出来了



就先这样吧,感觉想写的很多,但是还写的不条理。。自己用作以后复习吧。。


原创粉丝点击