全文检索solr(四)solr基本使用

目录

 

schema.xml

field

dynamicField(动态域)

uniqueKey

copyField(复制域)

fieldType(域类型)

​​​​​​​配置中文分析器

配置业务Field

需求

在数据库中运行solr.sql脚本

​​​​​​​定义Field


​​​​​​​

schema.xml

schema.xml文件在SolrCore的conf目录下,在此配置文件中定义了域以及域的类型等一些配置。在solr中域必须先定义后使用。

field

 

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
  1. Name:域的名称
  2. Type:域的类型
  3. Indexed:是否索引
  4. Stored:是否存储
  5. Required:是否必须
  6. multiValued:是否是多值,存储多个值时设置为true,solr允许一个Field存储多个值,比如存储一个用户的好友id(多个),商品的图片(多个,大图和小图)

dynamicField(动态域)

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

Name:动态域的名称,是一个表达式,*匹配任意字符,只要域的名称和表达式的规则能够匹配就可以使用。

例如:搜索时查询条件[product_i:钻石]就可以匹配这个动态域,可以直接使用,不用单独再定义一个product_i域。

 

uniqueKey

<uniqueKey>id</uniqueKey>

相当于主键,每个文档中必须有一个id域。

copyField(复制域)

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

可以将多个Field复制到一个Field中,以便进行统一的检索。当创建索引时,solr服务器会自动的将源域的内容复制到目标域中。

  1. source:源域
  2. dest:目标域,搜索时,指定目标域为默认搜索域,可以提高查询效率。

 

定义目标域:

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

目标域必须要使用:multiValued="true"

 

​​​​​​​fieldType(域类型)

<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">

  <analyzer type="index">

         <tokenizer class="solr.StandardTokenizerFactory"/>

         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />

         <filter class="solr.LowerCaseFilterFactory"/>

  </analyzer>        

  <analyzer type="query">

         <tokenizer class="solr.StandardTokenizerFactory"/>

         <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />

         <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>

         <filter class="solr.LowerCaseFilterFactory"/>

  </analyzer>

</fieldType>
  1. name:域类型的名称
  2. class:指定域类型的solr类型。
  3. analyzer:指定分词器。在FieldType定义的时候最重要的就是定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤。
  4. type:index和query。Index 是创建索引,query是查询索引。
  5. tokenizer:指定分词器
  6. filter:指定过滤器

​​​​​​​配置中文分析器

使用IKAnalyzer中文分析器

第一步:把IKAnalyzer2012FF_u1.jar添加到solr/WEB-INF/lib目录下。

 

第二步:复制IKAnalyzer的配置文件和自定义词典和停用词词典到solr的solr/WEB-INF/classes目录下。

复制IK分词器配置文件、自定义词典、停用词词典,粘贴到Tomcat的solr的/WEB-INF/classes目录下

 

第三步:在schema.xml中添加一个自定义的fieldType,使用中文分析器。

         <!-- IKAnalyzer-->

    <fieldType name="text_ik" class="solr.TextField">

      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>

    </fieldType>

 

第四步:在schema.xml中添加field,指定field的type属性为text_ik

<!--IKAnalyzer Field-->

<field name="content_ik" type="text_ik" indexed="true" stored="true" />

第五步:重启tomcat

 

  1. 配置业务Field

    需求

要使用solr实现网站中商品搜索,需要 将mysql数据库中数据在solr中创建索引。

 

  1. 需要在solr的schema.xml文件定义要存储的商品Field。
  2. 需要把MySQL的数据导入到solr索引库中
  3. 开发搜索功能​​​​​​​

在数据库中运行solr.sql脚本

​​​​​​​定义Field

先确定定义的商品document的Field域有哪些?

可以根据mysql数据库中商品表的字段来确定:

 

products商品表:

Schema.xml中配置业务域

<!--product-->

<field name="product_name" type="text_ik" indexed="true" stored="true"/>

<field name="product_price"  type="float" indexed="true" stored="true"/>

<field name="product_description" type="text_ik" indexed="true" stored="false" />

<field name="product_picture" type="string" indexed="false" stored="true" />

<field name="product_catalog_name" type="string" indexed="true" stored="true" />



<field name="product_keywords" type="text_ik" indexed="true" stored="false" multiValued="true"/>

<copyField source="product_name" dest="product_keywords"/>

<copyField source="product_description" dest="product_keywords"/>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_35670694

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值