·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> solr与.net系列课程(二)solr的配置文件及其含义

solr与.net系列课程(二)solr的配置文件及其含义

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

solr与.net系列课程(二)solr的配置文件及其含义

solr与.net系列课程(二)solr的配置文件及其含义

本节内容还是不会涉及到.net与数据库的内容,但是不要着急,这都是学时solr必学要掌握的东西,solr可不是像其他的dll文件一样,只需要引用就能调出方法与数据的,你不配置好是无法使用,前两节主要是起铺垫作用的,看起来会很枯燥无味的.

本章节内容是为下一节连接数据库做准备的,单拿出来看的话,会让人很迷糊,没关系,就当提前预习了,知道有这么个东西就行了,等下一节结合数据库后就好理解了

上一节我们已经完成了solr的基本配置,这里我们来将一下solr的配置文件,之前提到过,solr的主要步骤在于配置,直接讲配置文件,很多没接触过的人看着会很吃力,这个需要结合链接数据库讲才好理解, 但是文章要一步一步的写,对于已经接触过solr的人那就就没什么问题了,这里将为大家提供尽可能详细的配置讲解,新人可以先了解一下,等下节开始讲解链接数据库就容易理解了.

(一)首先列出solr的几个重要配置文件名称

1.solr.xml(多个)

2.server.xml(tomcat的文件)

3. schema.xml

4. solrconfig.xml

5. data-config.xml(链接数据库的配置文件,需要自己创建)

那么接下来我们就开始讲解这些配置文件

(1)solr.xml

这个文件主要有两个地方,第一个地方是在上一节我们配置solr中手动创建的,忘了的朋友可以回去看一下,这个文件主要是建立tomcat 与solr之间的关系的,它的作用是让tomcat找到你所配置的solr.代码如下:

<?xml version="1.0" encoding="utf-8"?><Context docBase="C:\PRogram Files\Apache Software Foundation\Tomcat 7.0\webapps\solr.war" debug="0" crossContext="true">  <Environment name="solr/home" type="java.lang.String" value="C:\Program Files\Apache Software Foundation\Tomcat 7.0\solr" override="true"/></Context>

第二个solr.xml的位置在C:\Program Files\Apache Software Foundation\Tomcat 7.0\solr,这个是solr文件中自带的(就是上一节我们复制到tomcat下的文件夹),我们来看看他的代码.

<solr>  <solrcloud>    <str name="host">${host:}</str>    <int name="hostPort">${jetty.port:8983}</int>    <str name="hostContext">${hostContext:solr}</str>    <int name="zkClientTimeout">${zkClientTimeout:30000}</int>    <bool name="genericCoreNodeNames">${genericCoreNodeNames:true}</bool>  </solrcloud>  <shardHandlerFactory name="shardHandlerFactory"    class="HttpShardHandlerFactory">    <int name="socketTimeout">${socketTimeout:0}</int>    <int name="connTimeout">${connTimeout:0}</int>  </shardHandlerFactory></solr>

这个文件是用来配置solr单核与多核模式的(多核模式的意思是在同一服务器下配置多个solr),上面的是单核模式,不需要做任何更改(多核需要修改),他默认匹配该文件夹下的collection1文件夹,其他的配置文件都在这个文件夹下,本节可我们主要讲解单核模式,单核模式理解了,多核模式就容易理解了.(多核在后面的内容中会讲到,毕竟是教程,需要从简单的开始讲起)

(2)server.xml

这个文件是tomcat下的配置文件,位置在C:\Program Files\Apache Software Foundation\Tomcat 7.0\conf,检查一下该文件下这段代码:

  <Connector port="8080" protocol="HTTP/1.1"               connectionTimeout="20000"               redirectPort="8443"                URIEncoding="UTF-8" />

如果没有URIEncoding="UTF-8",solr在查询的时候可能会出现乱码,有可能导致查不出东西来.

(3)schema.xml

这个文件是solr 中比较重要的文件了 ,solr中的索引配置就写在这个文件中了,文件的内容因为太多了,就没法在文章里展示了,学习的朋友可以打开自己这个文件再结合文章去学习,s上一节中solr的分词器就是复制在这里的,我们先来看一看它自带的一些代码.

1.types节点

  <types>    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>    <fieldtype name="binary" class="solr.BinaryField"/>    <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"/>   ......................<!--下面的省略了--></type>  

这就是solr中支持的数据类型,它能匹配大部分数据库所包含的类型,我们在配置索引的时候会用到,这里面定义FieldType子节点,包括name,class,positionIncrementGap等一些参数。

name:就是这个FieldType的名称。

class:指向org.apache.solr.analysis包里面对应的class名称,用来定义这个类型的行为(该名字对应的解析器)。

可选的属性:

sortMissingLastsortMissingFirst两个属性是用在可以内在使用String排序的类型上(包括:string,boolean,sint,slong,sfloat,sdouble,pdate)。

sortMissingLast="true",没有该field的数据排在有该field的数据之后,而不管请求时的排序规则。值默认是设置成false

sortMissingFirst="true",跟上面倒过来呗。值默认是设置成false

2.fields节点

(1)field子节点

<fields>      <field name="_version_" type="long" indexed="true" stored="true"/>      <!-- points to the root document of a block of nested documents. Required for nested      document support, may be removed otherwise   -->   <field name="_root_" type="string" indexed="true" stored="false"/>   <!-- Only remove the "id" field if you have a very good reason to. While not strictly     required, it is highly recommended. A <uniqueKey> is present in almost all Solr      installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".   -->      <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />            <field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>   <field name="name" type="text_general" indexed="true" stored="true"/>   <field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/>.........................<!----省略-></fields>    

定义具体的字段(类似数据库的字段),含有以下属性:

name:字段名

type:之前定义过的各种FieldType

indexed:是否被索引

stored:是否被存储(如果不需要存储相应字段值,尽量设为false)

multiValued:是否有多个值(对可能存在多值的字段尽量设置为true,避免建索引时抛出错误)

这个就是配置索引的地方了,什么意思的,就数据存储在数据库下时是存在字段下的,比如id,name,在solr也需要有字段去存储它啊,上面的name就是存储的名字,可以随便起,在data-conf.xml(下节会讲)文件下会有数据库与solr的映射表.

(2)Copy Fields子字节

  <copyField source="cat" dest="text"/>   <copyField source="name" dest="text"/>   <copyField source="manu" dest="text"/>

这段代码也在<types></types>中,拷贝字段,就是把两个字段结合到一个字段中,例:

<schema name="eshequn.post.db_post.0" version="1.1"      xmlns:xi="http://www.w3.org/2001/XInclude">       <fields>          <!-- for title -->          <field name="t" type="text" indexed="true" stored="false" />          <!-- for abstract -->          <field name="a" type="text" indexed="true" stored="false" />          <!-- for title and abstract -->          <field name="ta" type="text" indexed="true" stored="false" multiValued="true"/>      </fields>      <copyField source="t" dest="ta" />      <copyField source="a" dest="ta" />  </schema>

段t是文章的标题,字段a是文章的摘要,字段ta是文章标题和摘要的联合。添加索引文档时,只需要传入t和a字段的内容,solr会自动索引ta字段。这 算不上多高级的功能,不过如果让你来实现这个功能,你会怎么做呢?我接手的搜索系统原来就有类似的功能,它的做法是,将t和a字段的文本合并,塞到ta字 段,无可厚非的做法。不过,有人注意到lucene的Document类提供的public final Field[] getFields(String name)类似函数不?也就是说,lucene中的一个name可以对应多个Field。solr在添加索引时,会检查field name是不是copyField集合中的source,是的话就以其value构造dest field。如果dest由多个source构成,就需要将其指定为multiValued。 对于查询来说,如果查询字段要来自多个字段,一种选择是使用CopyField,化多个字段为一个字段,缺点是不能区分各个字段的重要度差别。比如文章的标题和摘要,标题就要比摘要重要性更强,如果有这方面的要求,可以选择查询多个字段的做法。

(3)DynamicField

  <dynamicField name="*_i"  type="int"    indexed="true"  stored="true"/>   <dynamicField name="*_is" type="int"    indexed="true"  stored="true"  multiValued="true"/>   <dynamicField name="*_s"  type="string"  indexed="true"  stored="true" />   <dynamicField name="*_ss" type="string"  indexed="true"  stored="true" multiValued="true"/> 

动态字段(Dynamic fields)允许 solr 索引没有在 schema 中明确定义的字段。这个在忘记定义一些字段时很有用。动态字段可以让系统更灵活,通用性更强。动态字段和常规字段类似,除了它名字中包含一个通配符外,在索引文档时,一个字段如果在常规字段中没有匹配时,将到动态字段中匹配。假设schema中定义了一个叫*_i的动态动态字段,如果要索引一个叫 cost_i 的字段,但是 schema 中不存在 cost_i 的字段,这样 cost_i 将被索引到 *_i 字段中。动态字段也是定义在 schema.xml 文件中,和其他字段一样,它也有个名词,字段类型,和属性。建议在 schema.xml 定义一些基本的动态字段,以备扩展之用。

(4)其他一些标签

<uniqueKey>id</uniqueKey>

文档的唯一标识(就是主键,solr是