q2009a06z22 发表于 2017-12-20 10:19:02

全文检索引擎Solr系列—–全文检索基本原理

Sorl定义:
  sorl是独立的企业级搜索服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的xml文件,生成索引。
  同时也可以通过Http Get操作提出查询请求,并以xml返回查询结果
  特点:
  sorl采用java5开发的基于Lucene的全文服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一款非常优秀的全文搜索引擎。

工作方式:
  文档通过Http利用XML 加到一个搜索集合中。查询该集合也是通过http收到一个XML/JSON响应来实现。它的主要特性包括:高效、灵活的缓存功能,
  垂直搜索功能,高亮显示搜索结果,通过索引复制来提高可用性,提供一套强大Data Schema来定义字段,类型和设置文本分析,提供基于Web的管理界面等。

全文检索引擎Solr系列—–全文检索基本原理
  http://www.importnew.com/12707.html
  全文检索可以归纳为两个过程:1、索引创建(indexing)2、搜索索引(search)

  Solr/Lucene采用的是一种反向索引,所谓反向索引:就是从关键字到文档的映射过程,保存这种映射这种信息的索引称为反向索引
  索引创建:
  1)把原始文档交给分词组件(Tokenizer)
  把文档分解为一个个单词
  去除标点符号
  去除停词
  2)词汇单元(Token)传给语言处理组件(Linguistic Processor)
  变为小写
  将单词缩减为词根形式
  将单词转变为词根形式
  3)得到的词(Term)传给索引组件(Indexer)
  搜索过程
  1)对查询内容进行语法分析、词法分析、语言处理
  2)搜索索引,得到符合语法树的文档集合
  3)根据查询语句与文档的相关性,对结果进行排序
  Sorl的安装
  因为Solr是java开发。所以需要安装jdk,安装环境Linux,需要安装Tomcat。

全文检索引擎Solr系列——Solr核心概念、配置文件
  http://www.importnew.com/12770.html

Document
  Document是Solr索引(动词,indexing)和搜索的最基本单元,它类似于关系数据库表中的一条记录,可以包含一个或多个字段(Field),每个字段包含一个name和文本值。
  字段在被索引的同时可以存储在索引中,搜索时就能返回该字段的值,通常文档都应该包含一个能唯一表示该文档的id字段。例如:
  

<doc>  <field name="id">company123</field>
  <field name="companycity">Atlanta</field>
  <field name="companystate">Georgia</field>
  <field name="companyname">Code Monkeys R Us, LLC</field>
  <field name="companydescription">we write lots of code</field>
  <field name="lastmodified">2013-06-01T15:26:37Z</field>
  
</doc>
  


Schema
  Solr中的Schema类似于关系数据库中的表结构,它以schema.xml的文本形式存在在conf目录下,在添加文当到索引中时需要指定Schema,Schema文件主要包含三部分:字段(Field)、字段类型(FieldType)、唯一键(uniqueKey)


[*]字段类型(FieldType):用来定义添加到索引中的xml文件字段(Field)中的类型,如:int,String,date,
[*]字段(Field):添加到索引文件中时的字段名称
[*]唯一键(uniqueKey):uniqueKey是用来标识文档唯一性的一个字段(Feild),在更新和删除时用到
  例如:
  

<schema name="example" version="1.5">  <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  <field name="title" type="text_general" indexed="true" stored="true" multiValued="true"/>
  <uniqueKey>id</uniqueKey>
  <fieldType name="string" sortMissingLast="true" />
  <fieldType name="text_general" positionIncrementGap="100">
  <analyzer type="index">
  <tokenizer/>
  <filter ignoreCase="true" words="stopwords.txt" />
  <!-- in this example, we will only use synonyms at query time
  <filter synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
  -->
  <filter/>
  </analyzer>
  <analyzer type="query">
  <tokenizer/>
  <filter ignoreCase="true" words="stopwords.txt" />
  <filter synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
  <filter/>
  </analyzer>
  </fieldType>
  
</schema>
  


Field
  在Solr中,字段(Field)是构成Document的基本单元。对应于数据库表中的某一列。字段是包括了名称,类型以及对字段对应的值如何处理的一种元数据。比如:
  

<field name="name" type="text_general" indexed="true" stored="true"/>  

  


[*]Indexed:Indexed=true时,表示字段会加被Sorl处理加入到索引中,只有被索引的字段才能被搜索到。
[*]Stored:Stored=true,字段值会以保存一份原始内容在在索引中,可以被搜索组件组件返回,考虑到性能问题,对于长文本就不适合存储在索引中。
Field Type
  Solr中每个字段都有一个对应的字段类型,比如:float、long、double、date、text,Solr提供了丰富字段类型,同时,我们还可以自定义适合自己的数据类型,例如:


<!-- Ik 分词器 -->
<fieldType name="text_cn_stopword" class="solr.TextField">
   <analyzer type="index">
         <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerSolrFactory" useSmart="false"/>
   </analyzer>
   <analyzer type="query">
         <tokenizer class="org.wltea.analyzer.lucene.IKAnalyzerSolrFactory" useSmart="true"/>
   </analyzer>
</fieldType>
<!-- Ik 分词器 -->

Solrconfig:
  如果把Schema定义为Solr的Model的话,那么Solrconfig就是Solr的Configuration,它定义Solr如果处理索引、高亮、搜索等很多请求,同时还指定了缓存策略,用的比较多的元素包括:


[*]指定索引数据路径

<!--
Used to specify an>
other than the default ./data under the Solr home.
If replication is in use, this should match the replication configuration.
-->
<dataDir>${solr.data.dir:./solr/data}</dataDir>


[*]缓存参数

<filterCache
class="solr.FastLRUCache"
size="512"
initialSize="512"
autowarmCount="0"/>
<!-- queryResultCache caches results of searches - ordered lists of
   document>
   of documents requested.-->
<queryResultCache
class="solr.LRUCache"
size="512"
initialSize="512"
autowarmCount="0"/>
<!-- documentCache caches Lucene Document objects (the stored fields for each document).
   Since Lucene internal document>transient, this cache will not be autowarmed.-->
<documentCache
class="solr.LRUCache"
size="512"
initialSize="512"
autowarmCount="0"/>


[*]请求处理器  请求处理器用于接收HTTP请求,处理搜索后,返回响应结果的处理器。比如:query请求:

<!-- A request handler that returns indented JSON by default -->
<requestHandler name="/query" class="solr.SearchHandler">
   <lst name="defaults">
       <str name="echoParams">explicit</str>
       <str name="wt">json</str>
       <str name="indent">true</str>
       <str name="df">text</str>
   </lst>
</requestHandler>  

每个请求处理器包括一系列可配置的搜索参数,例如:wt,indent,df等等。  

  


[*]搜索组件
1.    solrJ添加索引库
  1、把solrJ的jar包添加到工程。
  2、创建一个SolrServer对象。创建一个和sorl服务的连接。HttpSolrServer。
  3、创建一个文档对象。SolrInputDocument。
  4、向文档对象中添加域。必须有一个id域。而且文档中使用的域必须在schema.xml中定义。

页: [1]
查看完整版本: 全文检索引擎Solr系列—–全文检索基本原理