solr笔记

本文是结合solr-4.7.2和apache-tomcat-7.0.65作服务器搭建进行solr的介绍:

准备工作:

  1. 下载solr-4.7.2和apache-tomcat-7.0.65,并解压

  2. 新建一个文件夹(我在d盘建的一个文件夹“D:DeveloperEnvironmentsolrAndTomcatIntegration”,以下我将以这个文件夹进行说明)

步骤说明:

1. 先将tomcat拷贝到solrAndTomcatIntegration文件夹下;

2. 将solr-4.7.2/example/webapps/文件夹下的solr.war的这个包拷贝到tomcat目录的webapps文件夹下;

3. 将solr-4.7.2/example文件夹下的solr文件夹拷贝到solrAndTomcatIntegration文件夹下,并更改solr文件夹名称为 home,以下是我的    solrAndTomcatIntegration文件夹下的文件:

  

4. 更改tomcat的conf文件夹下的配置文件server.xml:

<!-- 在<Host></Host>节点之间添加如下配置 -->
<
Context docBase="solr" path="/solr" reloadable="false" crossContext="true"> <Environment name="solr/home" type="java.lang.String" value="D:DeveloperEnvironmentsolrAndTomcatIntegrationhome" override="true" /> </Context>
<!-- <Environment/>节点中的name 和 type 配置无需修改,需要根据实际配置value,value的值为solr的core文件的home根目录,在这里,我的配置如上 -->

5. 将solr-4.7.2/example/lib/ext文件夹下的jar包拷贝到tomcat目录下的lib文件夹下,然后启动tomcat,使用 http://localhost:8080/solr 访问即可。

启动solr的登录验证:

1. 配置tomcat的conf文件夹下的tomcat-user.xml文件:

<!-- 在<tomcat-users/>节点中添加如下role和user,username随便起 -->
<
role rolename="solr"/> <user username="solrAdmin" password="solrAdmin" roles="solr"/>

2. 配置在tomcat服务器中已经解压缩的solr项目,配置web.xml如下:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Solr Lockdown</web-resource-name>
        <url-pattern>/</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>solr</role-name>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>
<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>Solr</realm-name>
</login-config>

3. 启动tomcat服务器即可。

配置solr的中文分词器 mmseg4j:

  此例使用的mmseg4j的jar包如下:

    

  对于solr集成mmseg4j,只需要引入这两个包即可,因为mmseg4j-solr-2.0.0已经集成了mmseg4j-analysis的这个jar包。

1. 将上述的那两个jar包放到文件夹“solrAndTomcatIntegration/apache-tomcat-7.0.65/webapps/solr/WEB-INF/lib”下,就是在tomcat下solr.war解压出来的那个文件夹的/WEB-INF/lib文件夹之下。

2. 目前我们先使用solr的collection1这个core作说明,这个在我们的“solrAndTomcatIntegration/home”文件夹下已经存在了的,配置如下节点到“solrAndTomcatIntegration/home/collection1/conf”文件夹下的schema.xml文件中:

<!-- 将如下配置加入到<types/>节点中,这段配置可以到mmseg4j官网中找到的 -->
<!--
mmseg4j configuration --> <fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="../dic"/> </analyzer> </fieldtype> <fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="max-word" /> </analyzer> </fieldtype> <fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100"> <analyzer> <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="../dic" /> </analyzer> </fieldtype>

3. 启动tomcat测试即可

配置datasource来源为数据库:

1. 将“solr-4.7.2/dist”文件夹下的如下图的两个jar包引入到“solrAndTomcatIntegration/apache-tomcat-7.0.65/webapps/solr/WEB-INF/lib”文件夹下,这是必须的操作。

 

2. 在“solrAndTomcatIntegration/home”下新建一个core,名字叫"testCore",将“solr-4.7.2/example/example-DIH/solr/db”下的conf文件夹拷贝到“solrAndTomcatIntegration/home/testCore”下面,更改里面的配置文件“db-data-config.xml”,我的配置如下:

<!-- 根据实际你的数据库中的表的结构来设计吧,这里只是一个简单的demo,本人也正在研究这个东西 -->
<
dataConfig> <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/world" user="root" password="password1"/> <document> <entity name="city" query="select id,city_name,country_code,district,population from tbl_city"> <field column="id" name="id" /> <field column="city_name" name="cityName" /> <field column="country_code" name="countryCode" /> <field column="district" name="district" /> <field column="population" name="population" /> </entity> </document> </dataConfig>

3. 我使用的mysql的数据库,所有需要将mysql的jdbc驱动包加入到“solrAndTomcatIntegration/apache-tomcat-7.0.65/webapps/solr/WEB-INF/lib”文件夹之下。

4. 启动tomcat即可。

5. 以下是我的数据库的表的关系:

  

  以下是我的配置文件db-data-config.xml的配置:

<dataConfig>
    <dataSource driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/world" user="root" password="password1"/>
    <document>
        <entity name="country" 
                query="select Code,Name,Continent,GovernmentForm from country"
                deltaQuery="select Code from country where last_modified > '${dataimporter.last_index_time}'">
            <field column="Code" name="id"/>
            <field column="Name" name="name"/>
            <field column="Continent" name="continent"/>
            <field column="GovernmentForm" name="governmentForm"/>
            
            <entity name="countrylanguage"
                query="select Language from countrylanguage where CountryCode='${country.Code}'"
                deltaQuery="select CountryCode from countrylanguage where last_modified > '${dataimporter.last_index_time}'"
                parentDeltaQuery="select Code from country where Code='${countrylanguage.CountryCode}'">
                <field column="Language" name="language"/>
            </entity>
            
            <entity name="city" 
                query="select id,city_name,country_code,district,population from tbl_city where country_code='${country.Code}'"
                deltaQuery="select country_code from tbl_city where last_modified > '${dataimporter.last_index_time}'"
                parentDeltaQuery="select Code from country where Code='${city.country_code}'">
               <field column="id" name="id" />
            <field column="city_name" name="cityName" />
            <field column="country_code" name="countryCode" />
            <field column="district" name="district" />
            <field column="population" name="population" />
           </entity>
    </entity> </document> </dataConfig>
原文地址:https://www.cnblogs.com/wy2185/p/5108650.html