天天看点

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

二、如果想下载lucene,下载地址是: https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/java/

三、其中solr-4.10.3的包结构如下:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

bin

solr的脚本

contrib

solr为了增强自身的功能,所添加的扩展依赖包

dist

solr  build过程中产生的war包和jar包,还有它的一些依赖包。

docs

文档

example

例子目录

solr

配置了solr运行时的信息,它是一个标准的solrhome目录

multicore

可以配置多个solrcore实例

webapps

solr.war的目录

licenes

版本信息

四、solrcore的安装配置

1.solrhome是一个目录,它是solr运行的主目录,它包括多个solrcore目录,solrcore目录中就solr实例的运行配置文件和数据文件。

其中solrhome的结构如下,以solr-4.10.3为例进行说明:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

solrhome中可以包括多个solrcore,每个solrcore相互独立,而且可以单独对外提供搜索和索引服务。这个目录可以随意放置。比如我将这个文件放置在了:d:\installed\solr-resources\solrhome

其中solrfcore的目录结构如下:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

目录说明:

conf

该solrcore的运行配置信息

data

在索引创建成功之后,这个目录将会出现。它主要存储了索引文件。

例如下面是我创建solrcore成功之后的目录结构:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

其中conf的目录结构:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

2.创建solrcore:

创建solrcore需要先准备solrhome:

拷贝d:\installed\solr-4.10.3\example\solr到d:\installed\solr-resources,并将solr名称改成solrhome

最终的内容结构如下:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

五、solr部署到tomcat

1:

solr需要运行一个servlet容器,它默认通过了一个servlet容器,jetty(java写的),项目中大多使用的是tomcat.

我的tomcat位置:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

2: solr的war部署到tomcat中。

其中solr的war位置是:d:\installed\solr-4.10.3\example\webapps

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

将solr.war拷贝到d:\installed\apache-tomcat-7.0.47\webapps,重启一下,当solr.war被解压完成之后删除solr这个war包。

3:添加扩展依赖包(日志包)

拷贝以下目录的扩展包

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

到d:\installed\apache-tomcat-7.0.47\webapps\solr\web-inf\lib:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

4添加log4j的配置文件,拷贝以下的文件:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

拷贝到以下目录:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

5.在web.xml中指定solrhome的位置

找到web.xml

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

即:

<env-entry>

       <env-entry-name>solr/home</env-entry-name>

       <env-entry-type>java.lang.string</env-entry-type>

    </env-entry>

注意上面的env-entry-name不要动。下面的env-entry-value指定的是solrhome的路径

6.启动tomcat进行访问

即:点击d:\installed\apache-tomcat-7.0.47\bin\startup.bat

7.solrcore的配置

在solrcore的conf目录下有一个文件叫solrconfig.xml

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

下面这些配置主要是配置solrcore的扩展jar包,solr.install.dir表示solrcore的安装目录

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

修改如下:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

其中的../../对应的目录:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

把solr下载包中的contrib和dist文件拷贝到以下目录(其中contrib和dist文件就在解压后的solr-4.10.3文件夹下):

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

实际的代码如下:

<lib dir="${solr.install.dir:../..}/contrib/extraction/lib" regex=".*\.jar" />

  <lib dir="${solr.install.dir:../..}/dist/" regex="solr-cell-\d.*\.jar" />

  <lib dir="${solr.install.dir:../..}/contrib/clustering/lib/" regex=".*\.jar" />

  <lib dir="${solr.install.dir:../..}/dist/" regex="solr-clustering-\d.*\.jar" />

  <lib dir="${solr.install.dir:../..}/contrib/langid/lib/" regex=".*\.jar" />

  <lib dir="${solr.install.dir:../..}/dist/" regex="solr-langid-\d.*\.jar" />

  <lib dir="${solr.install.dir:../..}/contrib/velocity/lib" regex=".*\.jar" />

  <lib dir="${solr.install.dir:../..}/dist/" regex="solr-velocity-\d.*\.jar" />

  <lib dir="${solr.install.dir:../..}/contrib/dataimporthandler/lib" regex=".*\.jar"/>

  <lib dir="${solr.install.dir:../..}/contrib/db/lib" regex=".*\.jar"/>将d:\installed\solr-4.10.3\dist中的solr-dataimporthandler-4.10.3.jar和solr-dataimporthandler-extras-4.10.3.jar放入到d:\installed\solr-resources\contrib\dataimporthandler\lib中,将mysql-connector-java-5.1.7-bin.jar放入到d:\installed\solr-resources\contrib\db\lib中,如果没有相应目录的话创建相应的目录

修改用于数据导入的配置文件中的内容:

修改d:\installed\solr-resources\solrhome\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,catalog_name,price,description,picture from products">

<field column="pid" name="id"/> 

<field column="name" name="product_name"/> 

<field column="catalog" name="product_catalog"/>

<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>

修改:d:\installed\solr-resources\solrhome\collection1\conf\schema.xml,创建索引字段信息等(在最后添加如下内容):

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

<!--定义field,指定field的type属性为text_ik-->

<field name="title_ik" type="text_ik" indexed="true" stored="true" />

    <field name="content_ik" type="text_ik" indexed="true" stored="false" multivalued="true"/>

<!-- 商品名称 -->

<field name="product_name" type="text_ik" indexed="true" stored="true" />

<!-- 商品分类id -->

<field name="product_catalog" type="string" indexed="true" stored="true" />

<!-- 商品分类名称 -->

<field name="product_catalog_name" type="string" 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_pic" type="string" indexed="false" stored="true" />

<field name="product_keywords" type="text_ik" indexed="true" stored="false" multivalued="true"/>

<!-- 

使用复制域、将product_name和product_description

都复制到product_keywords,当搜索product_keywords的时候

-->

<copyfield source="product_name" dest="product_keywords"/>

<copyfield source="product_description" dest="product_keywords"/>

重启一下tomcat,最后访问地址:

最后进入如下界面,点击execute,然后进行数据导入:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

http://127.0.0.1:8080/solr/#/

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

看到该页面表示安装成功了。

六、多solrcore配置

一个solr工程中可以配置多个solrcore实例。

配置多个solrcore实例好处:

1.      一个solr工程配置多个solrcore,而且每个solrcore之间是互相独立,可以单独对外提供搜索和索引服务。这类似于数据库。一个数据库可以有多个实例。如果有新需求,可以直接扩展实例。

2.      多个solrcore在管理索引文件时,可以进行分类管理。

3.      使用solrcloud(solr集群)必须要建立多个solrcore.

复制solrhome下的collection1到本目录下,并且改名为collection2

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

修改collection2文件中core.properties

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入
1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

七、配置中文分词器

使用ikanalyzer中文分词器:

1、把ikanalyzer2012ff_u1.jar(就这一个)添加到solr/web-inf/lib目录下

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

2、复制ikanalyzer的配置文件和定义词典和停用词词典到solr的classpath下。

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

效果如下:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

3置域的类型、配置fieldtype,指定使用中文分词器。(在d:\installed\solr-resources\solrhome\collection1\conf\schema.xml中配置)

错误(也就是说不要analyzer这个标签)

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

正确的:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

 即(注意下面的text_ik和配置下面的field中的type要相同。

<fieldtype name="text_ik" class="solr.textfield">

   <analyzer class="org.wltea.analyzer.lucene.ikanalyzer"/>

</fieldtype>

4配置field,使用中文分词器的fieldtype

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

<field name="title_ik" type="text_ik" indexed="true" stored="true" />

<field name="content_ik" type="text_ik" indexed="true" stored="false" multivalued="true"/>

   只有指定了type=”text_ik“了,才表示要进行分词。

5重启tomcat测试

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

八、界面功能介绍

1.analysis

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

2.document

通过该界面操作,可以对索引库进行添加索引、删除索引、修改索引

在solr中,添加或修改文档时,必须要传入一个唯一主键id

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

3、query

*:*:可以查询索引的数据

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

九、schema.xml的基本使用

schema.xml文件在solrcore中conf目录下。这个文件主要是配置域名及域的类型等信息。

solr中的域要先定义后使用

文件位置:

1、solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入

4.1.1 field(重点)

配置solr的域:

<a target="_blank">&lt;fieldname="id" type="string" indexed="true"stored="true" required="true" multivalued="false"/&gt;</a>

name:域名

type:域的类型(也需要配置)

indexed:是否索引

stored:是否存储

required:是否必须,一般只有id才设置

multivalued:是否可以多值。如果设置多值的话,里面的值就采用数组方式存储。比如商品图片(大图、中图、小图等等)

4.1.2 dynamicfield

配置动态域

&lt;dynamicfield name="*_i"  type="int"    indexed="true"  stored="true"/&gt;

name:指定域的名称,该域的名称是通过一个表达式来指定的

type:域的类型(在使用动态域的时候,要分清该域对应的类型)