solr6.6 了解managed-schema

1、managed-schema是什么

managed-schema定义了索引库的数据类型,同时指明某个类型的字段是不是要进行索引,是不是要进行保存到索引库里等等。大概就是做这种事情。

在创建一个core的时候,整个文件会产生,并且里面默认帮忙定义了很多类型,基本够使用了。但还有一些不满足的地方,比如需要一个类型,中文分词。这样的话,还需要自定义。

2、fieldType

 <fieldType name="int" class="solr.TrieIntField" docValues="true" precisionStep="0" positionIncrementGap="0"/>

 <fieldType name="string" class="solr.StrField" sortMissingLast="true" docValues="true" />

以上定义了一个int类型和string类型,指定了它们的实现分别是TrieIntField和StrField。

fieldType 中的name可以随便写,在整个managed-schema唯一即可。

2.1 自定义fieldType

自定义fieldType很简单,模仿它默认的就可以了。在managede-schema添加如下代码:

 <fieldType name="mycore_string" class="solr.StrField" sortMissingLast="true" docValues="true" />

mycore_string这个filedType使用class为solr.StrField实现,跟string实现一样,也就有了string一样的功能。

2.2 实验:使用自定义的fileType来进行数据导入。

下面使用自定义的fileType来为mycore导入数据。把之前导入数据的filed改一下,使用自定义的:


<!--自定义filedType-->
    <fieldType name="mycore_string" class="solr.StrField" sortMissingLast="true" docValues="true" />
    <fieldType name="mycore_int" class="solr.TrieIntField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="mycore_date" class="solr.TrieDateField" docValues="true" precisionStep="0" positionIncrementGap="0"/>




 <!--column的id是数据库的id,name的id是managed_schema里面的id,id是必须,并且唯一的-->
    <field name="vip" type="mycore_string" indexed="true"  stored="true" />
    <field name="point" type="mycore_int" indexed="true" stored="true" />
    <field name="content" type="mycore_string" indexed="true" stored="true"/>
    <field name="add_time" type="mycore_date" indexed="true" stored="true"/>

要使配置修改后能生效,一定要在后台reload一下这个core,所以,这里reload一下mycore

这里写图片描述

这里写图片描述

可以看到,数据也能导入进来,这里不是之前的数据,因为在导入的时候,clean已经勾选上了,勾选了clean的话,导入前,会把之前的数据给清空。同时commit也是默认勾选上,只有勾选了commit,对索引库的操作才能生效。

3、field

定义索引库里面某个字段的作用。

<field name="content" type="mycore_string" indexed="true" stored="true"/>

这里有四个属性,当然field能定义的属性不止四个。

3.1、field中的name属性

name属性要在managed-schema中唯一,同时会在查询的时候返回,可以看做一条信息的key。
比如把content改为content_core.
managed-schema文件:

!--自定义filedType-->
    <fieldType name="mycore_string" class="solr.StrField" sortMissingLast="true" docValues="true" />
    <fieldType name="mycore_int" class="solr.TrieIntField" docValues="true" precisionStep="0" positionIncrementGap="0"/>
    <fieldType name="mycore_date" class="solr.TrieDateField" docValues="true" precisionStep="0" positionIncrementGap="0"/>




 <!--column的id是数据库的id,name的id是managed_schema里面的id,id是必须,并且唯一的-->
    <field name="vip" type="mycore_string" indexed="true"  stored="true" />
    <field name="point" type="mycore_int" indexed="true" stored="true" />
    <field name="content_core" type="mycore_string" indexed="true" stored="true"/>
    <field name="add_time" type="mycore_date" indexed="true" stored="true"/>

data-config.xml也要对应该一下:

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/db_jx" user="root" password="root" batchSize="-1" />
  <document>
        <entity name="mycore_test" query="select id ,vip,point,content,add_time from solr_mycore">
             <!--column的id是数据库的id,name的id是managed_schema里面的id,id是必须,并且唯一的-->
            <field column="id" name="id" />
             <!--column的vip是数据库的vip字段,name的vip是managed_schema里面的vip,下面配置同理-->
            <field column="vip" name="vip" />
            <field column="point" name="point" />
            <field column="content" name="content_core" />
            <field column="add_time" name="add_time" />
        </entity>
    </document>
</dataConfig>

后台继续以下2步操作:

后台更新配置文件Core Admin—>mycore—>reload

重新导入数据:mycore—>Dataimport—>Entity—>mycore_test—>execute

然后查询就发现已经更改了,conent已经变为content_core

这里写图片描述

4、dynamicField

dynamicField是动态字段,solr考虑到managed-schema里面定义的field不够用,就有了这么一个字段。

部分字段代码:

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

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

可以看到name属性里面有个通配符*,就是说,这个可以是任意字符,在使用的时候,可以任意写,比如可以写成aaa_i,bbb_s

做个实验,managed-schema文件这次不用修改,需要修改data-config.xml文件。

data-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://127.0.0.1:3306/db_jx" user="root" password="root" batchSize="-1" />
  <document>
        <entity name="mycore_test" query="select id ,vip,point,content,add_time from solr_mycore">
             <!--column的id是数据库的id,name的id是managed_schema里面的id,id是必须,并且唯一的-->
            <field column="id" name="id" />
             <!--column的vip是数据库的vip字段,name的vip是managed_schema里面的vip,下面配置同理-->
            <field column="vip" name="aaa_i" />
            <field column="point" name="bbb_i" />
            <field column="content" name="ccc_s" />
            <field column="add_time" name="add_time" />
        </entity>
    </document>
</dataConfig>

可看到column为vip,point,content的name属性分别修改为aaa_i,bbb_i,ccc_s

后台继续以下2步操作,更新一下索引库:

后台更新配置文件Core Admin—>mycore—>reload

重新导入数据:mycore—>Dataimport—>Entity—>mycore_test—>execute

查询后结果如下,vip,point,content已经变了:

这里写图片描述

更多字段属性的意思http://lucene.apache.org/solr/guide/6_6/documents-fields-and-schema-design.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值