淘淘商城22_全文检索_ik中文分词器02_solr的增删改查

一. 创建solr数据库,导入数据

如何导入数据库这里就不再赘述了

百度网盘:

链接:https://pan.baidu.com/s/1qtdadvsR6Cy6281DfGFx7Q 
提取码:6w14 

 

二. 设置业务系统Field

如果不使用Solr提供的Field可以针对具体的业务需要自定义一套Field,如下是商品信息Field

业务逻辑的前提:

搜索的时候通过哪些字段进行搜索,

在页面展示的时候,展示哪些内容.

使用solr做全文检索:  我们搜索的数据和我们页面展示的数据,都来源于solr索引库.

<!--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"/>

 

三. 维护索引

1. 批量导入数据

1.1 把dataimport插件依赖的jar包(solr-4.10.1\dist)添加到solrhome\collection1\lib中

创建一个lib目录

 

还有mysql的驱动包

链接:https://pan.baidu.com/s/11mduCBUMlmoY9hHyoOc_XQ 
提取码:76mn 

 

1.2 配置solrconfig.mxl文件

添加一个requestHandler。

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

 

 1.3 创建一个data-config.xml,保存到collection1\conf\目录下

data-config.xml 

<?xml version="1.0" encoding="UTF-8" ?>  
<dataConfig>   
	<dataSource type="JdbcDataSource"   
			  driver="com.mysql.jdbc.Driver"   
			  url="jdbc:mysql://localhost:3306/solr"   
			  user="root"   
			  password="123456"/>   
	<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>

1.4 重启Tomcat

这样呢,我们就把数据库中的数据导入到索引库

四. 在索引库进行增删改查

1. 增加数据

2. 查询

3. 修改

这里的修改是在原id的基础上进行修改,再进行查询的时候是先删除以前的,再进行查询

4. 删除

要将Dcoument Type改为xml

<delete>
  <id>11100</id>
</delete>

这样查的话,发现还是有数据的,这是怎么回事?

这是因为我们执行删除操作的时候没有提交事物

所以,我们要加上<commit/>

<delete>
  <id>11100</id>
</delete>
<commit/>

这样才删除成功

5. 其他查询

显示的全是有关台灯的信息

5.1 fq:区间  product_price:[30 TO 50]

以下查询的是价格30--50的台灯

5.2 sort:排序 product_price desc

5.3 start,rows

start:从第几条开始

rows:显示多少条数据

5.4 fl:显示的字段  product_name,product_price

5.5 df:默认的搜索域 在schema.xml中有设定

5.6 wt:返回的数据类型,一般返回的是json

5.7  hl:高亮显示

什么是高亮显示?比如说,我们再百度上搜索“手机”,会有红色显示,如图:

这就是高亮显示

猜你喜欢

转载自blog.csdn.net/fjz_lihuapiaoxiang/article/details/85224434
今日推荐