六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 1390|回复: 0

全文检索引擎Solr系列——Solr核心概念、配置文件

[复制链接]
 楼主| 发表于 2014-11-28 16:33:53 | 显示全部楼层 |阅读模式
DocumentDocument是Solr索引(动词,indexing)和搜索的最基本单元,它类似于关系数据库表中的一条记录,可以包含一个或多个字段(Field),每个字段包含一个name和文本值。字段在被索引的同时可以存储在索引中,搜索时就能返回该字段的值,通常文档都应该包含一个能唯一表示该文档的id字段。例如:
  1. <doc>
  2.     <field name="id">company123</field>
  3.     <field name="companycity">Atlanta</field>
  4.     <field name="companystate">Georgia</field>
  5.     <field name="companyname">Code Monkeys R Us, LLC</field>
  6.     <field name="companydescription">we write lots of code</field>
  7.     <field name="lastmodified">2013-06-01T15:26:37Z</field>
  8. </doc>
复制代码
SchemaSolr中的Schema类似于关系数据库中的表结构,它以schema.xml的文本形式存在在conf目录下,在添加文当到索引中时需要指定Schema,Schema文件主要包含三部分:字段(Field)、字段类型(FieldType)、唯一键(uniqueKey)
  • 字段类型(FieldType):用来定义添加到索引中的xml文件字段(Field)中的类型,如:int,String,date,
  • 字段(Field):添加到索引文件中时的字段名称
  • 唯一键(uniqueKey):uniqueKey是用来标识文档唯一性的一个字段(Feild),在更新和删除时用到
例如:
  1. <schema name="example" version="1.5">
  2.     <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  3.     <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>

  4.     <uniqueKey>id</uniqueKey>
  5.     <fieldType name="string" class="solr.StrField" sortMissingLast="true" />
  6.     <fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
  7.           <analyzer type="index">
  8.             <tokenizer class="solr.StandardTokenizerFactory"/>
  9.             <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  10.             <!-- in this example, we will only use synonyms at query time
  11.             <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
  12.             -->
  13.             <filter class="solr.LowerCaseFilterFactory"/>
  14.           </analyzer>
  15.           <analyzer type="query">
  16.             <tokenizer class="solr.StandardTokenizerFactory"/>
  17.             <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
  18.             <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
  19.             <filter class="solr.LowerCaseFilterFactory"/>
  20.           </analyzer>
  21.     </fieldType>
  22. </schema>
复制代码
Field在Solr中,字段(Field)是构成Document的基本单元。对应于数据库表中的某一列。字段是包括了名称,类型以及对字段对应的值如何处理的一种元数据。比如:
  1. <field name="name" type="text_general" indexed="true" stored="true"/>
复制代码
  • Indexed:Indexed=true时,表示字段会加被Sorl处理加入到索引中,只有被索引的字段才能被搜索到。
  • Stored:Stored=true,字段值会以保存一份原始内容在在索引中,可以被搜索组件组件返回,考虑到性能问题,对于长文本就不适合存储在索引中。
Field TypeSolr中每个字段都有一个对应的字段类型,比如:float、long、double、date、text,Solr提供了丰富字段类型,同时,我们还可以自定义适合自己的数据类型,例如:
  1. <!-- Ik 分词器 -->
  2. <fieldType name="text_cn_stopword" class="solr.TextField">
  3.      <analyzer type="index">
  4.          <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerSolrFactory" useSmart="false"/>
  5.      </analyzer>
  6.      <analyzer type="query">
  7.          <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerSolrFactory" useSmart="true"/>
  8.      </analyzer>
  9. </fieldType>
  10. <!-- Ik 分词器 -->
复制代码
Solrconfig:如果把Schema定义为Solr的Model的话,那么Solrconfig就是Solr的Configuration,它定义Solr如果处理索引、高亮、搜索等很多请求,同时还指定了缓存策略,用的比较多的元素包括:
  • 指定索引数据路径
    1. <!--
    2. Used to specify an alternate directory to hold all index data
    3. other than the default ./data under the Solr home.
    4. If replication is in use, this should match the replication configuration.
    5. -->
    6. <dataDir>${solr.data.dir:./solr/data}</dataDir>
    复制代码
    缓存参数
    1. <filterCache
    2.   class="solr.FastLRUCache"
    3.   size="512"
    4.   initialSize="512"
    5.   autowarmCount="0"/>

    6. <!-- queryResultCache caches results of searches - ordered lists of
    7.      document ids (DocList) based on a query, a sort, and the range
    8.      of documents requested.  -->
    9. <queryResultCache
    10.   class="solr.LRUCache"
    11.   size="512"
    12.   initialSize="512"
    13.   autowarmCount="0"/>

    14. <!-- documentCache caches Lucene Document objects (the stored fields for each document).
    15.    Since Lucene internal document ids are transient, this cache will not be autowarmed.  -->
    16. <documentCache
    17.   class="solr.LRUCache"
    18.   size="512"
    19.   initialSize="512"
    20.   autowarmCount="0"/>
    复制代码
    请求处理器
    请求处理器用于接收HTTP请求,处理搜索后,返回响应结果的处理器。比如:query请求:
    1. <!-- A request handler that returns indented JSON by default -->
    2. <requestHandler name="/query" class="solr.SearchHandler">
    3.      <lst name="defaults">
    4.        <str name="echoParams">explicit</str>
    5.        <str name="wt">json</str>
    6.        <str name="indent">true</str>
    7.        <str name="df">text</str>
    8.      </lst>
    9. </requestHandler>
    复制代码
    每个请求处理器包括一系列可配置的搜索参数,例如:wt,indent,df等等。  摘自:http://www.importnew.com/12770.html


该会员没有填写今日想说内容.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表