[Solr] Apache Solr 简介及使用

前言

Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引;也可以通过Http Get操作提出查找请求,并得到XML格式的返回结果。

Solr下载

  1. 到solr的官网进行下载:http://lucene.apache.org/
  2. 下载历史版本的网址: http://archive.apache.org/dist/lucene/solr/

环境准备

服务器相关包

  1. apache-tomcat-7.0.77.zip
  2. solr-4.10.2.zip

以下是Solr需要的Jar

  1. commons-logging-1.1.1.jar
  2. IKAnalyzer2012FF_u1.jar
  3. log4j-1.2.16.jar
  4. slf4j-api-1.7.6.jar
  5. slf4j-log4j12-1.7.2.jar

IK分词

  1. ext.dic
  2. IKAnalyzer.cfg.xml
  3. log4j.properties
  4. stopword.dic

针对IK分词器的四个文件具体内容在上文中有提到 : Lucene搜索引擎简介

Solr的目录结构

1> 根目录结构
1

2> example目录
2

Solr启动方式一

分别解压 apache-tomcat-7.0.77.zipsolr-4.10.2.zip

在CMD下启动 E:\Solr\solr-4.10.2\example 下的 start.jar

//CMD下
E:\Solr\solr-4.10.2\example> java -jar start.jar

访问 http://localhost:8983/solr/ 即可

Solr启动方式二

1> 将 E:\Solr\solr-4.10.2\example\webapps 下的 solr.war 放到 tomcat的webapps下

扫描二维码关注公众号,回复: 2644517 查看本文章

2> 启动tomcat,等 solr.war 解压完毕后,关闭tomcat,并重命名solr.warsolr.war.bak,防止重启覆盖

3> 将文中提到的5个需要准备好的Jar包及IK分词器复制到 E:\Solr\apache-tomcat-7.0.77\webapps\solr\WEB-INF

11

下面是 classes 与 lib 文件夹里的详细内容

22

33

4> 建立索引库

E:\Solr\solr-4.10.2\example\solr 里的文件全部复制,
新建文件夹solr_home创建索引库,将复制的内容全部粘贴到 E:\Solr\solr_home
44

E:\Solr\solr-4.10.2 下的 contribdist文件夹复制到 E:\Solr\solr-4.10.2\example\solr

55

修改 E:\Solr\solr_home\collection1\conf 下的 solrconfig.xml
将以下

  <lib dir="../../../contrib/extraction/lib" regex=".*\.jar" />
  <lib dir="../../../dist/" regex="solr-cell-\d.*\.jar" />

  <lib dir="../../../contrib/clustering/lib/" regex=".*\.jar" />
  <lib dir="../../../dist/" regex="solr-clustering-\d.*\.jar" />

  <lib dir="../../../contrib/langid/lib/" regex=".*\.jar" />
  <lib dir="../../../dist/" regex="solr-langid-\d.*\.jar" />

  <lib dir="../../../contrib/velocity/lib" regex=".*\.jar" />
  <lib dir="../../../dist/" regex="solr-velocity-\d.*\.jar" />

修改为

  <lib dir="../contrib/extraction/lib" regex=".*\.jar" />
  <lib dir="../dist/" regex="solr-cell-\d.*\.jar" />

  <lib dir="../contrib/clustering/lib/" regex=".*\.jar" />
  <lib dir="../dist/" regex="solr-clustering-\d.*\.jar" />

  <lib dir="../contrib/langid/lib/" regex=".*\.jar" />
  <lib dir="../dist/" regex="solr-langid-\d.*\.jar" />

  <lib dir="../contrib/velocity/lib" regex=".*\.jar" />
  <lib dir="../dist/" regex="solr-velocity-\d.*\.jar" />

5> 修改tomact下的solr的web.xml

打开如下注释,并修改索引库位置

    <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>E:\Solr\solr_home</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

接下来就可以启动tomcat了~ 访问: http://localhost:8080/solr 即可

Solr界面说明

1> 仪表盘信息

1

2> solr启动及运行过程中的log信息

1

3> 索引库的选择

1

documents
1

query

1

schema

1

Solr的配置文件

solrConfig.xml : solr的核心配置文件

solrconfig.xml 配置文件主要定义了 solr 的一些处理规则,包括索引数据的存放 位置,更新,删除,查询的一些规则配置。一般此文件不需要进行修改, 采取默认即可

schema.xml: solr约束文件

先贴上极简版

<?xml version="1.0" encoding="UTF-8" ?>


<schema name="example" version="1.5">
   <field name="_version_" type="long" indexed="true" stored="true"/>
   <field name="_root_" type="string" indexed="true" stored="false"/> 
   <field name="id" type="string" indexed="true" 
                    stored="true" required="true" multiValued="false" /> 
   <field name="name" type="text_ws" indexed="true" stored="true"/>
   <field name="price"  type="float" indexed="true" stored="true"/>
   <field name="title" type="text_ws" indexed="true" stored="true" multiValued="false"/>
   <field name="content" type="text_ws" indexed="true" stored="true" multiValued="false"/>
   <field name="text" type="text_ws" indexed="true" stored="true" multiValued="true"/>
   <field name="date"  type="date" indexed="true" stored="true"/>
   <field name="binary"  type="binary" indexed="true" stored="true"/>

   <dynamicField name="*_ss" type="string"  indexed="true"  
                                stored="true" multiValued="true"/>

   <uniqueKey>id</uniqueKey>

   <copyField source="title" dest="text"/>
   <copyField source="content" dest="text"/>

    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
    <fieldType name="int" class="solr.TrieIntField" 
                        precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="float" class="solr.TrieFloatField" 
                        precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="long" class="solr.TrieLongField" 
                        precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="double" class="solr.TrieDoubleField" 
                        precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="date" class="solr.TrieDateField" 
                        precisionStep="0" positionIncrementGap="0"/>
    <fieldtype name="binary" class="solr.BinaryField"/>


    <fieldType name="text_ws" class="solr.TextField">
        <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>
</schema>

下面是字段的说明

<!--
    第一种标签为 field标签: 主要是用来指定字段名称的, Lucene中是有用户在程序中指定, 
    solr中需要提前在配置文件中指定
-->
<field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/>
<!--
    name: 字段的名称
    type: 字段的类型
    indexed: 是否索引
    stored: 是否保存
    multiValued: 是否多值, 这个字段, 类似存储一个数组  
    这里有两个不允许删除的: 一个是 _version__ 一个是  _root__  这两个是solr内部需要使用的字段

    有一个字段的名称必须为id,其类型都不允许进行修改 原因是id字段已经被主键使用uniqueKey  
    其余的是一些初始化好的字段
-->
<!--
    第二种标签为dynamicField, 被称为是动态域
    此种标签是为程序的扩展所使用的, 因为我们不可能把所有的字段全部定义好, 
    所以就需要动态域来进行动态扩展
-->
<dynamicField name="*_is" type="int" indexed="true" stored="true" multiValued="true"/>


<!--第三种标签为 uniqueKey:  必要标签, 表名文档的唯一属性, 一般默认为id-->
<!--Lucene中是自己进行维护, solr中, 需要自己指定-->
<uniqueKey>id</uniqueKey>


<!--第四种标签为 copyField: 被称为是复制域-->
<!--
    source: 表名要复制那个字段的值
    dest: 复制到那个字段上

    此种标签主要是为了查询所使用的,
    例如, 当查询Text字段的时候, 实质上相当于查询title和name两个字段
-->
<copyField source="cat" dest="text"/>


<!--第五种标签: fieldType   字段类型定义标签-->
<!--此种标签是用来定义字段的类型的,可以指定此字段使用何种分词器进行分词-->
<fieldType name="managed_en" class="solr.TextField" positionIncrementGap="100">
<analyzer>
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.ManagedStopFilterFactory" managed="english"/>
    <filter class="solr.ManagedSynonymFilterFactory" managed="english"/>
</analyzer>
</fieldType>

添加Solr索引库

E:\Solr\solr_home 下的 collection1 复制一份 , 重命名为 collection2

66

删除 E:\Solr\solr_home\collection2 下的 data

修改 E:\Solr\solr_home\collection2 下的 core.properties ,修改索引库名称

#name=collection1
name=mySolrDb

重启一下tomcat即可

至此: solr的部署就完成了

猜你喜欢

转载自blog.csdn.net/Konaji/article/details/81490096
今日推荐