需求分析:
我不想使用solr默认的主键id,我想换成其他的,比如我的文章id为article_id,我想让article_id作为主键。
而且,我的主键是int类型,而solr的主键默认是string类型,我们还需要修改,修改后,还会报错,我们还需要来解决报错问题。
实践:
第一步:
我们需要打开C:\data\solr\collection1\conf\schema.xml
然后我们找到id的主键field
1
2
|
<
field
name
=
"id"
type
=
"String"
indexed
=
"true"
stored
=
"true"
required
=
"true"
multiValued
=
"false"
/>
|
然后我们修改为
1
2
|
<
field
name
=
"article_id"
type
=
"int"
indexed
=
"true"
stored
=
"true"
required
=
"true"
multiValued
=
"false"
/>
|
不光是要修改name值,还需要修改type值。
第二步:
我们在schema.xml中找到
1
2
|
<
uniqueKey
>id</
uniqueKey
>
|
我们修改为:
1
2
|
<
uniqueKey
>article_id</
uniqueKey
>
|
这样还不行,因为运行的时候,还是会报错,所以,我们还需要修改
C:\data\solr\collection1\conf\solrconfig.xml
找到这个位置:
1
2
3
4
5
6
7
|
<
searchComponent
name
=
"elevator"
class
=
"solr.QueryElevationComponent"
>
<
str
name
=
"queryFieldType"
>String</
str
>
<
str
name
=
"config-file"
>elevate.xml</
str
>
</
searchComponent
>
|
将其注释掉!
会变成这个样子:
1
2
3
4
5
6
7
8
|
<!--
<searchComponent name="elevator" class="solr.QueryElevationComponent" >
<str name="queryFieldType">String</str>
<str name="config-file">elevate.xml</str>
</searchComponent>
-->
|
然后,我们修改我们的test.class中的测试类,将id改为article_id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@Test
public
void
testAddDocument ()
throws
Exception {
//创建一个文档对象SolrInputDocument
SolrInputDocument document =
new
SolrInputDocument();
//向文档中添加域,必须有id域,域的名称必须在scheme.xml中定义
document.addField(
"article_id"
,
"2000"
);
document.addField(
"article_title"
,
"java是世界上最好的语言"
);
//把文档对象写入索引库
solrServer.add(document);
//提交(两种方式都要进行提交操作)
solrServer.commit();
}
|
我们junit运行一下,然后我们访问localhost:8080/solr,进行query搜索,查看一下是否id变为了int类型
这样就测试成功了!