`
huze104
  • 浏览: 96646 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Solr 3.5 入门配置应用

阅读更多

 

机器上已安装 : Tomcat 6.0    jdk1.7    mysql 5.0

1  访问 http://www.apache.org/dyn/closer.cgi/lucene/solr , 在这个网址里选择一个路径 , 下载 solr 3.5 的版本

2  solr3.5 在本机解压缩以后 , 把 apache-solr-3.5.0\example\webapps  目录下的  solr.war  文件拷贝到  Tomcat 6.0 的webapps 目录下

3   在 Tomcat 6.0\webapps\solr  目录里 新建一个 文件夹  conf

4   把 solr3.5 本机解压缩文件夹  apache-solr-3.5.0\example  下的 multicore 文件夹 考本到  Tomcat 6.0\webapps\solr\conf  目录下

5   在 Tomcat 6.0\conf\Catalina\localhost  目录下新建一个  solr.xml 文件 , 里面的内容如下

<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="${catalina.home}/webapps/solr.war" debug="0" crossContext="true" >

   <!-- 这里配置的是 Solr 运行的 Home 目录 -->

   <Environment name="solr/home" type="java.lang.String" value="${catalina.home}/webapps/solr/conf/multicore" override="true" /> 
</Context>

6   访问你的 solr 项目   localhost:8080/solr  会显示出两个  core  ,  solr 正常运行

7   配置分词 , 使用的是  mmseg4j 和 搜狗词库 ,

    下载地址 : http://code.google.com/p/mmseg4j/   ,  http://code.google.com/p/mmseg4j/downloads/detail?name=data.zip&can=2&q

8   把本地下载的 mmseg4j  解压缩 , 把里面的mmseg4j-all-1.8.5.jar 文件 拷贝到 Tomcat 6.0\webapps\solr\WEB-INF\lib 目录下

9   在  Tomcat 6.0\webapps\solr  目录下新建一个  dic  文件夹 , 把 新下载的  词库 拷贝到 dic 目录下

10  在 \Tomcat 6.0\webapps\solr\conf\multicore\core0\conf\schema.xml  文件的  types 节点里 添加如下节点 :

<fieldtype name="textComplex" class="solr.TextField" positionIncrementGap="100">
        <analyzer>
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="complex" dicPath="E:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/solr/dic">
            </tokenizer>
        </analyzer>
    </fieldtype>
    <fieldtype name="textMaxWord" class="solr.TextField" positionIncrementGap="100">
        <analyzer>
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="maxword" dicPath="E:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/solr/dic">
            </tokenizer>
        </analyzer>
    </fieldtype>
    <fieldtype name="textSimple" class="solr.TextField" positionIncrementGap="100">
        <analyzer>
            <tokenizer class="com.chenlb.mmseg4j.solr.MMSegTokenizerFactory" mode="simple" dicPath="E:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/solr/dic">
            </tokenizer>
        </analyzer>
    </fieldtype>

 

11  在 \Tomcat 6.0\webapps\solr\conf\multicore\core0\conf\schema.xml  文件的  fields 节点里 添加如下节点 :

  <field name="simple" type="textSimple" indexed="true" stored="true" multiValued="true" />
  <field name="complex" type="textComplex" indexed="true" stored="true" multiValued="true" />
  <field name="text" type="textMaxWord" indexed="true" stored="true" multiValued="true" />

 

12  因为 solr3.5 里有两个 core , 所以针对 core1 重复 10,11 两步

13  对分词进行测试 , 访问  http://localhost:8180/solr/core0/admin/analysis.jsp?highlight=on

13.1   Field[Name]   输入   :  complex

13.2   Field Value(index)   输入  :   中国银行第一分行  ,   Field Value(index)  下面的 verbose outpu 点选

13.3   点击     Analyze  按钮 ,  查看分词结果    :  中国银行 |  第一 |  分行

 

14  此时 Solr3.5 已经可以进行 分词 , 接下来配置 solr 3.5 连接  mysql 数据库 , 生成索引 , 进行分词

14.1   下载 java 的 mysql 驱动 , 本机解压 mysql-connector-java-5.1.18-bin.jar,  然后拷贝到 Tomcat 6.0\webapps\solr\WEB-INF\lib  目录下

14.2   在 \Tomcat 6.0\webapps\solr  目录下 新建  db  文件夹

14.3   在 \Tomcat 6.0\webapps\solr\db 文件夹下面新建一个  db-data-config.xml 文件 , 内容如下 :

<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" user="root" password="123" />
    <document name="messages">
        <entity name="message" transformer="ClobTransformer" query="select * from test1">
            <field column="ID" name="id" />
            <field column="Val" name="text" />
        </entity>
    </document>
</dataConfig>

 url="jdbc:mysql://localhost:3306/test" user="root" password="123"  这里配置了 mysql 的连接路径 , 用户名 , 密码

 <field column="ID" name="id" /><field column="Val" name="text" />  这里配置的是 数据库里要索引的字段 , 注意name 是 11 步配置的

14.4   在 Tomcat 6.0\webapps\solr\conf\multicore\core0\conf 目录下的 solrconfig.xml 文件里 , 添加如下代码 :

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">E:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/solr/db/db-data-config.xml</str> 
    </lst>
  </requestHandler>

“E:/Program Files/Apache Software Foundation/Tomcat 6.0/webapps/solr/db/db-data-config.xml” 是 14.3 配置文件的绝对路径

 

14.5  在Tomcat 6.0\webapps\solr\conf\multicore\core1\conf\solrconfig.xml   路径里重复 14.4

14.6  把本地下载解压的 solr3.5 文件里 , dist 目录下的  apache-solr-dataimporthandler-3.5.0.jar 和 apache-solr-dataimporthandler-extras-3.5.0.jar  Tomcat 6.0\webapps\solr\WEB-INF\lib  目录下

14.7   solr3.5 连接 mysql 已经配置完成 , 测试读取 mysql 生成 索引 , 访问 :   http://localhost:8180/solr/core0/dataimport?command=full-import 

14.8   测试分词查询 , 访问  http://localhost:8180/solr/core0/admin/  查询数据库里索引列里有的词

 

注意 , 这仅仅是配置 solr3.5 连接 mysql 生成索引 , 可以执行正常 词语  的查询 , 但是不能执行  对搜索短语的分词 查询

multicore  目录下面多个 core 文件夹 , 每一个都是一个接口 , 有独立的配置文件 , 处理某一类数据 。

multicore/core0/conf/  目录下的  schema.xml  文件 相当于数据表配置文件 , 它定义了加入索引的数据的数据类型 。文件里有一个 <uniqueKey>id</uniqueKey> 的配置 , 这里将 id 字段作为索引文档的唯一标示符 , 非常重要 。

 

FieldType 类型 , name 是这个 FieldType 的名称 , class 指向了 org.apache.solr.analysis 包里面对应的 class 名称 , 用来定义这个类型的定义 。在 FieldType 定义的时候最重要的就是定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤 。

Fields 字段 :  结点内定义具体的字段(类似数据库中的字段) , 就是 field , 包含 name , type(为之前定义过的各种FieldType) , indexed(是否被索引) , stored(是否被存储) , multiValued(是否有多个值)

copeField(赋值字段): 建立一个拷贝字段 , 将所有的全文字段复制到一个字段中 , 以便进行统一的检索 。

 

分享到:
评论
1 楼 huze104 2012-02-21  
DataImportHandler 最大的优点是基本不用写代码,把数据库(其实也可以用 http/file 资源)中的记录放到索引中。现大概看下步骤:

1、编辑 solrconfig.xml 注册一个请求 uri 为 "/dataimport" 的请求处理器(org.apache.solr.handler.dataimport.DataImportHandler),代码如:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> 
<lst name="defaults"> 
  <str name="config">data-config.xml</str> 
</lst> 
</requestHandler> 
2、创建 data-config.xml 文件放到 solr.home/conf 目录下,内容如下:

<dataConfig> 
  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/> 
  <document> 
    <entity name="id" 
            query="select id,name from mytable"> 
    </entity> 
  </document> 
</dataConfig> 
3、在 schema.xml 里创建 id 与 name 的字段声明。

4、把 mysql 的 jdbc.jar 放到 solr.home/lib 目录下。

5、启动 solr 后,执行命令:

http://localhost:8080/solr/dataimport?command=full-import 这 url 告诉 solr 做全量索引,做索引中会删除所有数据。当然也可以用 clean=false 参数来告诉它不删除,但也会删除相同id的(在 scheam.xml 的uniqueKey 声明的)。http://localhost:8080/solr/dataimport?command=full-import&clean=false

上面的是 mysql 字段与 scheam.xml 声明的一样,当然不一样时可以用 sql 的 as 来使它们一样。

还可以用 data-config.xml 里指定,如:

<dataConfig> 
  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/> 
  <document> 
    <entity name="id" 
            query="select id,name from mytable"> 
       <field column="id" name="solr_id"/> 
       <field column="name" name="solr_name"/> 
    </entity> 
  </document> 
</dataConfig> 
entity/field 中的 name 是指 scheam.xml 里指定的,column 是 sql 里出来的字段。

大多数,做索引时的数据不是在一个表里的,比较一对多的。DIH 还提供多表取数据。

把 data-config.xml 改为:

<dataConfig> 
  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver" 
              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/> 
  <document> 
    <entity name="outer" 
            query="select id,name,desc from mytable"> 
       <field column="id" name="solr_id"/> 
       <field column="name" name="solr_name"/> 
       <entity name="inner" 
               query="select details from another_table where id ='${outer.id}'"> 
              <field column="details" name="solr_details"/> 
       </entity> 
    </entity> 
  </document> 
</dataConfig> 
在 scheam.xml 里指定 solr_details 字段为多值的 multiValued="true"。在上面 inner entity 里的 sql 语句可以加参数 ${outer.id} 就是 mytable 的 id 字段。

修改好后,重启,再做一次索引就有效果了,一对多一个缺点是会有 N+1 个 sql 查询。对小数据量的索引合适用 solr data import。

参考:http://wiki.apache.org/solr/DIHQuickStart

相关推荐

Global site tag (gtag.js) - Google Analytics