solr6.6 创建一个core,并导入索引数据

来源:互联网 发布:国外域名注册商性价比 编辑:程序博客网 时间:2024/06/05 17:21

1、什么是core

core是solr的一个索引库,可以理解为一个数据库,core可以根据需要,创建多个。

2、创建core

例如,创建一个core,名字叫mycore,就可以用一下命令:
E:\solr-6.6.0\solr-6.6.0\bin>solr.cmd create -c mycore

如果一个core创建成功之后,会有如下信息打印:

这里写图片描述

然后会在solr后台看到:

这里写图片描述

表示一个core已经创建成功!

3、core目录介绍

创建一个core之后,除了在后台看到结果,也会在E:\solr-6.6.0\solr-6.6.0\server\solr目录下创建了一个叫mycore的文件夹。

core里面默认创建如下目录和文件:

这里写图片描述

conf:是一个放置配置文件,里面有两个文件需要经常修改。
data:是索引数据的保存目录。
core.properties:当前core的属性文件。

4、给core创建索引数据,做个实验

创建一个例子,给core导入索引数据,用于后面的实验。

4.1 创建一个数据库

创建一个数据库,并创建几条数据,表结构:
id自增
vip表示是否vip
point表示点击次数
content随便填一些内容
add_time表示添加时间
这里写图片描述

添加几条测试数据,content字段整规律一点,用于后面的实验。
这里写图片描述

4.2 配置solrconfig.xml

sorlconfig.xml文件与managed-shema文件是经常要修改的文件。位于创建的core目录里面的config文件夹里。例如:
E:\solr-6.6.0\solr-6.6.0\server\solr\mycore\conf

在solrconfig.xml文件的后面配置如下信息:

<!--引入DataImportHandler类的jar--><lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar" /><requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  <lst name="defaults">    <str name="config">data-config.xml</str>  </lst></requestHandler>

这里的配置表示mycore的数据导入使用solr的DataImportHandler,而这个handler所在的jar位于E:\solr-6.6.0\solr-6.6.0\dist目录里面,解压的时候就有。通过配置lib节点来进行引入

其中data-config.xml 需要在solrconfig.xml同级目录下自己手动创建。

配置之后应该是这样的:

<?xml version="1.0" encoding="UTF-8" ?><config> ......篇幅有限,此处省略很多默认内容<!--引入DataImportHandler类的jar--><lib dir="${solr.install.dir:../../../..}/dist/" regex="solr-dataimporthandler-.*\.jar" /><requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  <lst name="defaults">    <str name="config">data-config.xml</str>  </lst></requestHandler></config>
4.2.1 创建data-config.xml 暂时不填写内容,后面再写.

这里写图片描述

4.3 编写managed_schema

managed_schema里面定义了很多域,其实是使用了lucene中的域。
什么是域?域的作用是定义一个solr索引里面的字段是什么类型,能做什么,怎么做。有点类似数据库中字段的类型。但表示的含义更加的丰富。

在managed_schema后面添加如下代码:

   <!--这里无需定义id,因为managed_schema文件已经在前面开头位置定义了,id是必须,并且唯一的--><field name="vip" type="string" indexed="true"  stored="true" /><field name="point" type="int" indexed="true" stored="true" /><field name="content" type="string" indexed="true" stored="true"/><field name="add_time" type="date" indexed="true" stored="true"/>

name是这个域的名称,在整个managed_schema文件里面需要唯一,不能重复,这里定义成跟数据库表字段的名称,方便使用。当然,也可以定义成其他名字。
type是表示这个字段的类型是什么,string是字符串类型,int是整形数据类型,date是时间类型,相当于数据库里面的timestamp。
indexed表示是否索引,索引的话就能查询到,否则,搜索的时候,不会出现。
stored表示是否存储到索引库里面。

添加之后的managed_schema是这样的:

<?xml version="1.0" encoding="UTF-8" ?><!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.  You may obtain a copy of the License at     http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.--><!--   This is the Solr schema file. This file should be named "schema.xml" and should be in the conf directory under the solr home (i.e. ./solr/conf/schema.xml by default)  or located where the classloader for the Solr webapp can find it. This example schema is the recommended starting point for users. It should be kept correct and concise, usable out-of-the-box. For more information, on how to customize this file, please see http://wiki.apache.org/solr/SchemaXml PERFORMANCE NOTE: this schema includes many optional features and should not be used for benchmarking.  To improve performance one could  - set stored="false" for all fields possible (esp large fields) when you    only need to search on the field but don't need to return the original    value.  - set indexed="false" if you don't need to search on the field, but only    return the field as a result of searching on other indexed fields.  - remove all unneeded copyField statements  - for best index size and searching performance, set "index" to false    for all general text fields, use copyField to copy them to the    catchall "text" field, and use that for searching.  - For maximum indexing performance, use the ConcurrentUpdateSolrServer    java client.  - Remember to run the JVM in server mode, and use a higher logging level    that avoids logging every request--><schema name="example-data-driven-schema" version="1.6">    ......篇幅有限,此处省略很多默认的内容    <field name="vip" type="string" indexed="true"  stored="true" />    <field name="point" type="int" indexed="true" stored="true" />    <field name="content" type="string" indexed="true" stored="true"/>    <field name="add_time" type="date" indexed="true" stored="true"/></schema>

4.4 编写之前创建的data-config.xml

之所以现在才写data-config.xml是因为这个文件需要managed_schema里面的域与数据库字段进行映射。

<?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" />            <field column="add_time" name="add_time" />        </entity>    </document></dataConfig>

dataSource配置数据库信息
document配置数据库查询语句与managed_schema域的对应关系。目的是,在core导入数据的时候,会先通过该配置信息链接到数据库通过查询语句把数据查询出来,通过数据库字段与managed_schema域关联关系创建索引

4.5 开始导入数据

配置好了前面的信息,就可以在后台导入数据,配置信息需要reload一下core才能生效。如果配置文件出现错误,reload的时候也会有错误信息提示。

这里写图片描述

reload完之后,开始导入。

这里写图片描述

查询一下,发现已经导入成功

这里写图片描述

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 苹果6换了电池触屏不好用了怎么办 律协以律所被投诉不批准实习怎么办 提车时间没有写4s不给车怎么办 全店的人都知道我坐过牢怎么办 找了一个长得帅玩心重的老公怎么办 汽车左后轮油封漏油换了也漏怎么办 星露谷物语不小心把任务删了怎么办 饿了么商家版账号和密码忘了怎么办 红米4x手机里的照片全删了怎么办 微信表情包里编辑软文的数字怎么办 在转转上卖东西下单了想取消怎么办 丈夫的前儿子偷了我的钱我该怎么办 从日本寄的邮包被海关扣下了怎么办 卖家要求退货寄过去的货坏了怎么办 寄快递发货单丢了货发出去了怎么办 酷派手机电源键坏了开不了机怎么办 红米4a进水后开机就黑屏了怎么办 红米1s进水后开机了黑屏了怎么办 魅族手机已锁定魅族账号忘了怎么办 魅族5糸统升级后开不了机了怎么办 如果别人用电脑登了你的微信怎么办 消逝的光芒买错了买的普通版怎么办 在人人车卖了个车买家不过户怎么办 应用锁密码和密保问题都忘了怎么办 不小心在微信公众号发了消息怎么办 微信漂流瓶不能用了被投诉了怎么办 货物少了拉货的不承认少了怎么办 寄的快递号码留错了已经寄走怎么办 网购快递放在单位门卫室丢了怎么办 顺丰生鲜速配时效内食物坏了怎么办 竟尤理财跑路怎么办钱追的回来吗 博贝游戏用支付宝提不了现该怎么办 陌陌钱包没绑支付宝就体现了怎么办 鞋子让太阳晒的一只大一只小怎么办 美团不让上饿了么平台们商家怎么办 在汇通信诚租贷款买车被骗后怎么办 我的网银账户里的钱被盗了怎么办 老赖跑到国外去了还换了国籍怎么办 内裤把屁股两边磨得又肿又疼怎么办 京东在面临供货商不供货时怎么办的 打错的消息想撤回但按了删除怎么办