Solr6.6.2之基本使用

一、managed-schema

这里写图片描述
managed-schema文件中,主要配置了solrcore的一些数据信息,包括FieldFieldType的定义等信息,在solr中,FieldFieldType都需要先定义后使用。

1、Field

<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
  • Name:指定域的名称
  • Type:指定域的类型
  • Indexed:是否索引
  • Stored:是否存储
  • Required:是否必须
  • multiValued:是否多值,比如商品信息中,一个商品有多张图片,一个Field想存储多个值的话,必须将multiValued设置为true。

2、dynamicField

<dynamicField name="*_i" type="int" indexed="true" stored="true"/>
  • Name:指定动态域的命名规则

3、uniqueKey

<uniqueKey>id</uniqueKey>
其中的id是在Field标签中已经定义好的域名,而且该域要设置为required为true。
一个managed-schema文件中必须有且仅有一个唯一键

4、copyField

<copyField source="cat" dest="text"/>
  • Source:要复制的源域的域名
  • Dest:目标域的域名

dest指定的目标域,必须设置multiValued为true。

5、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>
  • Name:指定域类型的名称
  • Class:指定该域类型对应的solr的类型
  • Analyzer:指定分析器
  • Type:index、query,分别指定搜索和索引时的分析器
  • Tokenizer:指定分词器
  • Filter:指定过滤器

二、中文分词器(IKAnalyzer-6.5.0.jar)

1、将ikanalyzer的jar包拷贝到以下目录

这里写图片描述

2、将ikanalyzer的扩展词库的配置文件拷贝到以下目录

这里写图片描述

3、在managed-schema中配置FieldType和Field

<!-- 配置中文分词器 -->
<fieldType name="text_ik" class="solr.TextField">
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<field name="text_ik" type="text_ik" indexed="true" stored="true" multiValued="false" />

4、重启tomcat

这里写图片描述


三、配置业务Field(managed-schema)

这里写图片描述
需要往索引库添加的字段有:
pid、name、catalog、catalog_name、price、description、picture

FieldType:

经分析,由于中文分词器已经配置完FieldType,所以目前FieldType已经满足需要,无需配置。

Field

<!-- 配置案例的Field -->
<!-- 商品名称 -->
<field name="product_name" type="text_ik" indexed="true" stored="true"/>
<!-- 商品分类ID -->
<field name="product_catalog" type="string" indexed="true" stored="true"/> 
<!-- 商品分类名称 -->
<field name="product_catalog_name" type="string" indexed="true" stored="false"/>
<!-- 商品价格 -->
<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="true" multiValued="true"/> 
<!-- 将商品名称添加到目标域 -->
<copyFile source="product_name" dest="product_keywords" />
<!-- 将商品名称添加到目标域 -->
<copyFile source="product_description" dest="product_keywords" />

四、DataImport

该插件可以将数据库中指定的sql语句的结果导入到solr索引库中。

1、添加jar包

a)将/solr-6.6.2/distsolr-dataimporthandler-6.6.2.jar拷贝到/solr_home/contrib/dataimporthandler/lib
这里写图片描述
b)将mysql的驱动包,复制到以下目录(注意:原来没有的文件夹就自己新建)
这里写图片描述
c)修改solrconfig.xml,增加以下内容

<lib dir="${solr.install.dir:..}/contrib/dataimporthandler/lib" regex=".*\.jar" />
<lib dir="${solr.install.dir:..}/contrib/db/lib" regex=".*\.jar" />

2、配置requestHandler

solrconfig.xml中增加以下内容

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
        <str name="config">data-config.xml</str>
    </lst>
</requestHandler>

3、创建data-config.xml

solrconfig.xml同级目录下,创建data-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource name="source" 
        type="JdbcDataSource" 
        driver="com.mysql.jdbc.Driver" 
        url="jdbc:mysql://localhost:3306/solr" 
        user="root" password="root" 
        batchSize="-1" />
    <document>
        <entity name="product" query="select pid,name,catalog,catalog_name,price,description,picture from products">
            <field column="pid" name="id"/>
            <field column="name" name="product_name"/>
            <field column="catalog" name="product_catalog"/>
            <field column="catalog_name" name="product_catalog_name"/>
            <field column="price" name="product_price"/>
            <field column="description" name="product_description"/>
            <field column="picture" name="product_picture"/>
        </entity>
    </document>
</dataConfig>

4、重启tomcat

这里写图片描述
这里写图片描述

上一篇:《Solr6.6.2之环境搭建》
下一篇:《Solr6.6.2之SolrJ》

猜你喜欢

转载自blog.csdn.net/code_shadow/article/details/80727741