Solr基本使用及核心配置

1 、solr基本使用

1.1   schema.xml

schema.xml文件在SolrCore的conf目录下,它是Solr数据表配置文件,在此配置文件中定义了域以及域的类型还有其他一些配置,solr中域必须先定义后使用

1.1.1  field

l  Name:域的名称

l  Type:域的类型

l  Indexed:是否索引

l  Stored:是否存储

l  Required:是否必须

l  multiValued:是否是多值,存储多个值时设置为true,solr允许一个Field存储多个值,比如存储一个用户的好友id(多个),商品的图片(多个,大图和小图)

1.1.2  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" />
        <!-- in this example, we will only use synonyms at query time
        <filter class="solr.SynonymFilterFactory" synonyms="index\_synonyms.txt" ignoreCase="true" expand="false"/>
        -->
        <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>

l  name:域类型的名称

l  class:指定域类型的solr类型。

l  analyzer:指定分词器。在FieldType定义的时候最重要的就是定义这个类型的数据在建立索引和进行查询的时候要使用的分析器analyzer,包括分词和过滤。

l  type:index和query。Index 是创建索引,query是查询索引。

l  tokenizer:指定分词器

l  filter:指定过滤器

1.1.3  uniqueKey

<uniqueKey>id</uniqueKey>

1.1.4  copyField(复制域)相当于主键,每个文档中必须有一个id域。

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

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

l  dest:目标域,搜索时,指定目标域为默认搜索域,可以提供查询效率。

定义目标域:

必须要使用:multiValued="true"

1.1.5  dynamicField(动态域)

<dynamicField  name="\*\_i"  type="int"  indexed="true"  stored="true"/>

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

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

1.2   配置中文分析器

使用IKAnalyzer中文分析器。

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

第二步:复制IKAnalyzer的配置文件和自定义词典和停用词词典到solr的classpath下。

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

<!-- IKAnalyzer-->
    <fieldType name="text\_ik" class="solr.TextField">
      <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    </fieldType>

第四步:定义field,指定field的type属性为text_ik

<!--IKAnalyzer Field-->
   <field name="title\_ik" type="text\_ik" indexed="true" stored="true" />
   <field name="content\_ik" type="text\_ik" indexed="true" stored="false" multiValued="true"/>

第五步:重启tomcat

1.3   配置业务field

1.3.1  需求

要使用solr实现电商网站中商品搜索。

电商中商品信息在mysql数据库中存储了,将mysql数据库中数据在solr中创建索引。

需要在solr的schema.xml文件定义商品Field。

1.3.2  定义步骤

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

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

products商品表:

商品document的field包括:pid、name、catalog、catalog_name、price、description、picture

先定义Fieldtype:

solr本身提供的fieldtype类型够用了不用定义新的了。

再定义Field:

pid:商品id主键

使用solr本身提供的:

<!--
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
name:商品名称
<field name="product\_name" type="text\_ik" indexed="true" stored="true"/>
catalog:商品分类
<field name="product\_catalog" type="string" indexed="true" stored="true"/>
catalog\_name:商品分类名称
<field name="product\_catalog\_name" type="text\_ik" indexed="true" stored="true"/>
price:商品价格
<field name="product\_price"  type="float" indexed="true" stored="true"/>
description:商品描述
<field name="product\_description" type="text\_ik" indexed="true" stored="false"/>
picture:商品图片
<field name="product\_picture" type="string" indexed="false" stored="true"/>
-->  
<!--product-->
<field name="product\_name" type="text\_ik" indexed="true" stored="true"/>
<field name="product\_catalog" type="string" indexed="true" stored="true"/>
<field name="product\_catalog\_name" type="string" 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\_keywords" type="text\_ik" indexed="true" stored="false" multiValued="true"/>

原文链接 https://www.hanyuanhun.cn | https://node.hanyuanhun.cn

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值