solr入门学习

»其他

  Solr是apache下的一个开源项目,完全用java编写的高性能,全功能的文本搜索引擎,适用于需要全文搜索的应用。

目录


整合

开发环境:

solr-4.10.3、apach-tomcat-8.5.30、jdk1.8.0_131

整合步骤:

  1. 导包
  2. 创建solrhome
  3. 修改配置文件中solrhome位置

开始整合

<env-entry>
   <env-entry-name>solr/home</env-entry-name>
   <!--存放solrhome的绝对路径-->
   <env-entry-value>F:\solr\solrhome</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

测试整合

运行Tomcat,在浏览器输入localhost:8080/solr image


配置分词器

配置步骤:

  1. 导包
  2. 修改配置文件

导包


修改配置文件

打开solrhome\collection1\conf\schema.xml


重启Tomcat,测试

image


批量导入数据

步骤:

  1. 导包
  2. 配置solrconfig.xml
  3. 创建一个data-config.xml文件
  4. 在schema.xml中新建域
  5. 启动Tomcat查看

导入dataimport插件包


书写solrconfig.xml配置文件

添加一个requestHandler

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

创建data-config.xml

在solrconfig.xml同级目录下创建一个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="数据库账号"   
		  password="密码"/>   

<!--索引库中的域与数据库中的字段的映射-->
<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>

在schema.xml中新建域

<!--product-->
<!--都使用的是之前定义的IK分词器-->
<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"/>

<!--将name和description都copy进keywords域-->
<!--即用keywords域可以同时查询name和description两个域-->
<copyField source="product_name" dest="product_keywords"/>
<copyField source="product_description" dest="product_keywords"/>

启动Tomcat查看

  1. ==删除索引==:选中Documents,然后用XML
<!--删除所有-->
<delete>
  <query>*:*</query>
</delete>
<commit/>
  1. ==导入==:从数据库中导入数据 在Dataimport中选择Entity然后Execute。
  2. ==查询==:可以条件查询并且高亮显示

image