solr配置文件详解

来源:互联网 发布:网络信息平台建设方案 编辑:程序博客网 时间:2024/06/16 02:21

关于solr配置文件小编理解的,大部分都是可以在网上查到的。所以小编主要列举下一些网上大家谈的很少的知识。

1.solrconfig.xml

  • 配置文件(略)
  • 这个文件,我没做很深入的研究。只有一点说明,solr1.3和solr1.4是有区别的
  • solr1.3里面,需要指定solr index date的位置
    Xml代码  收藏代码
    1. <!-- Used to specify an alternate directory to hold all index data  
    2.      other than the default ./data under the Solr home.  
    3.      If replication is in use, this should match the replication configuration. -->  
    4.      <dataDir>${solr.data.dir:./solr/db/data}</dataDir>  
  • solr1.4里面,自动把生成index目录:data(和conf同级)-index (索引文件)
    Xml代码  收藏代码
    1. <!-- The directory where your SpellChecker Index should live.   -->  
    2.      <!-- May be absolute, or relative to the Solr "dataDir" directory. -->  
    3.      <!-- If this option is not specified, a RAM directory will be used -->  
    4.      <str name="spellcheckerIndexDir">spell</str>  
     

2.db-data-config.xml

  • 配置文件(人所在的公司为例) 
    Xml代码  收藏代码
    1. <!--dataConfig is designed  for member search, by using the table member,organization-->  
    2. <dataConfig>  
    3.     <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"  
    4.         url="jdbc:mysql://../test" user=".."  
    5.         password=".." />  
    6.     <document name="private-beta">  
    7.             <!--  field for news search, from member table-->  
    8.             <entity name="member" pk="memid"  
    9.             query="select distinct * from member"  
    10.             deltaQuery="select memid from member   
    11.                         where mem_date_updated > '${dataimporter.last_index_time}'">  
    12.                 <field column="memid" name="memid" />  
    13.                 <field column="name" name="name" />  
    14.                 <field column="mem_first_name" name="mem_first_name" />  
    15.                 <field column="mem_last_name" name="mem_last_name" />  
    16.                 <field column="mem_org_title" name="mem_org_title" />  
    17.                 <field column="mem_org_dept" name="mem_org_dept" />  
    18.                 <field column="mem_job_func" name="mem_job_func" />  
    19.                 <field column="mem_job_level" name="mem_job_level" />  
    20.                 <field column="mem_add_city" name="mem_add_city" />  
    21.                 <field column="mem_add_state_code" name="mem_add_state_code" />  
    22.                 <field column="mem_add_country_code" name="mem_add_country_code" />  
    23.                 <field column="mem_date_created" name="mem_date_created" />  
    24.                 <field column="mem_email" name="mem_email" />  
    25.                 <field column="mem_orgid" name="mem_orgid" />  
    26.                 <field column="mem_add_address" name="mem_add_address" />  
    27.   
    28.                 <!--  field for member search, from organization table-->  
    29.                 <entity name="organization" pk="orgid"   
    30.                 query="select * from organization where orgid='${member.mem_orgid}'" >  
    31.                     <field column="orgid" name="orgid" />       
    32.                     <field column="org_name" name="org_name" />                     
    33.                     <field column="org_website" name="org_website" />   
    34.                     <field column="org_website_protocol" name="org_website_protocol" />     
    35.                     <field column="org_subcat_id" name="org_subcat_id" />   
    36.                     <field column="org_subcat_id2" name="org_subcat_id2" />     
    37.                     <field column="org_subcat_id3" name="org_subcat_id3" />     
    38.                     <field column="org_subcat_id4" name="org_subcat_id4" />     
    39.                     <field column="org_subcat_id5" name="org_subcat_id5" />                             
    40.                 </entity>  
    41.   
    42.         </entity>  
    43.   
    44.     </document>  
    45. </dataConfig>  
     
  • delta-import:通过deltaQuery,查到数据库中未被索引记录的ids(id1,id2...),然后执行deltaImportQuery,如果没用deltaImportQuery,就通过Query去组装deltaImportQuery。对于上面的配置文件,就应该组装成这样的query:select  distinct * from member where memid = id1,把这条记录导入索引库里面,然后再导id2。小编曾以为solr应该组装select .. from .. where .. and memid in (id1, id2)这样的query,可好像它并没这样做。
  • 只有deltaQuery里面select的field和PK一样时(都为memid),才会组装成select  distinct * from member where memid = id1。否则组装成select  distinct * from member and memid = id1,增量导入时会报错。突然明白了
    solr wiki 写道
    pk : The primary key for the entity. It is optional and only needed when using delta-imports. It has no relation to the uniqueKey defined in schema.xml but they both can be the same.
  • left join:涉及到多个表之间有外键连接时,solr源码里面给我们展示的,是<entity></entity>里面,去引用上一层<entity></entity>对应表的外键:orgid='${member.mem_orgid}'。那么,我们何不直接将配置文件中的Query改为:select  distinct * from member left join organization o on orgid = mem_orgid。这样就可以不用分层,把所有<field></field>定义到一块了。这样是可行的,只是效率还是个未知数。不过小编推荐分层的<entity></entity>,逻辑清楚。

3.schema.xml

  • 配置文件
    Xml代码  收藏代码
    1.    <!-- This is an example oTokenFilterFactoriesf using the KeywordTokenizer along  
    2.         With various  to produce a sortable field  
    3.         that does not include some properties of the source text  
    4.      -->  
    5.    <fieldType name="alphaOnlySort" class="solr.TextField" sortMissingLast="true" omitNorms="true">  
    6.      <analyzer>  
    7.        <!-- KeywordTokenizer does no actual tokenizing, so the entire  
    8.             input string is preserved as a single token  
    9.          -->  
    10.        <tokenizer class="solr.KeywordTokenizerFactory"/>  
    11.        <!-- The LowerCase TokenFilter does what you expect, which can be  
    12.             when you want your sorting to be case insensitive  
    13.          -->  
    14.        <filter class="solr.LowerCaseFilterFactory" />  
    15.        <!-- The TrimFilter removes any leading or trailing whitespace -->  
    16.        <filter class="solr.TrimFilterFactory" />  
    17.        <!-- The PatternReplaceFilter gives you the flexibility to use  
    18.             Java Regular expression to replace any sequence of characters  
    19.             matching a pattern with an arbitrary replacement string,   
    20.             which may include back refrences to portions of the orriginal  
    21.             string matched by the pattern.  
    22.               
    23.             See the Java Regular Expression documentation for more  
    24.             infomation on pattern and replacement string syntax.  
    25.               
    26.             http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/package-summary.html  
    27.          -->  
    28.        <filter class="solr.PatternReplaceFilterFactory"  
    29.                pattern="([^a-z])" replacement="" replace="all"  
    30.        />  
    31.      </analyzer>  
    32.    </fieldType>  
    33.   
    34. <fields>  
    35.   
    36.     <!--  field for member search,all field  is in one-to-one correspondence with the field in db-data-config.xml -->  
    37.     <!--  field from member table-->  
    38.     <field name="memid" type="long" indexed="true" stored="true"  
    39.     multiValued="false" required="true"/>  
    40.     ...  
    41.     <field name="mem_orgid" type="long" indexed="true" stored="true"  
    42.         multiValued="false"/>  
    43.   
    44.   
    45.     <!--  field from organization table-->  
    46.     <field name="orgid" type="long" indexed="true" stored="true"  
    47.         multiValued="false"/>  
    48.     <field name="org_name" type="alphaOnlySort" indexed="true" stored="true"  
    49.         multiValued="false" />  
    50.     ...  
  • alphaOnlySort:对于org_name这样的field,一般我们会将其定义为text类型,如果同时我们需要按org_name排序,那怎么办?显然,text是不能用了。刚好,alphaOnlySort,可以为我们解决这个问题,你应该可以理解上面那段配置。
  • 特别要说明的是<filter class="solr.PatternReplaceFilterFactory"
                    pattern="([^a-z])" replacement="" replace="all"
            />,排除了所有非字母的文本。举个例子,按上面的配置,你查找“123”这样的公司名,它会匹配到任何公司名为数字的公司。所以我把这段给注释掉了。
  • 如果选择了alphaOnlySort,输入ibm,将无法匹配到ibm.com,因为alphaOnlySort类型的ibm.com并不会被分割(<filter class="solr.WordDelimiterFilterFactory...")。

4.stopwords.txt

  • 配置文件(略)
  • stopwords,只针对定义了texttype的field。
  • 里面定义的一些停用词我们需要注意了,比喻“OR”,你能把它停掉吗。不一定,美国就有Oregon洲缩写为OR,如果你把org_state_code的fieldtype设置为text的话,你搜索OR,传到solr server的url:http://localhost:8888/solr/../select?q=*:*?&fq=org_state_code:OR就会变成http://localhost:8888/solr/../select?q=*:*?&fq=org_state_code:,报错!
  • 所以,要么你将org_state_code定义为其他fieldtype,要么你在stopwords里面除掉这些特殊的words。
原创粉丝点击