先看下官方文档的例子:
假设有如下表结构:
data-config.xml:一对多,多对多的关系写法如下:
-
-
"org.hsqldb.jdbcDriver" url=
"jdbc:hsqldb:/temp/example/ex" user=
"sa" />
-
"products">
-
"item" query=
"select * from item">
-
"ID" name=
"id" />
-
"NAME" name=
"name" />
-
"MANU" name=
"manu" />
-
"WEIGHT" name=
"weight" />
-
"PRICE" name=
"price" />
-
"POPULARITY" name=
"popularity" />
-
"INSTOCK" name=
"inStock" />
-
"INCLUDES" name=
"includes" />
-
-
"feature" query=
"select description from feature where item_id='${item.ID}'">
-
"features" column=
"description" />
-
-
"item_category" query=
"select CATEGORY_ID from item_category where item_id='${item.ID}'">
-
"category" query=
"select description from category where id = '${item_category.CATEGORY_ID}'">
-
"description" name=
"description" />
-
-
-
-
-
一对多写法:
-
"feature" query=
"select description from feature where item_id='${item.id}'">
-
"feature" column=
"description" />
-
多对多写法:
-
"item_category" query=
"select category_id from item_category where item_id='${item.id}'">
-
"category" query=
"select description from category where id = '${item_category.category_id}'">
-
"description" name=
"description" />
-
-
本人使用的是以上的写法,api中也给出了另一种写法,但自己没有测试过:
-
-
"org.hsqldb.jdbcDriver" url=
"jdbc:hsqldb:/temp/example/ex" user=
"sa" />
-
-
"item" query=
"select * from item">
-
"feature" query=
"select description as features from feature where item_id='${item.ID}'"/>
-
"item_category" query=
"select CATEGORY_ID from item_category where item_id='${item.ID}'">
-
"category" query=
"select description as cat from category where id = '${item_category.CATEGORY_ID}'"/>
-
-
-
-
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
刚开始配置完成后,得出的结果始终是一对一:
如上图:假如:item表和item_category表是一对多,但我这边始终得出的结果是一对一,如:name和description不是一个name对应多个description(实际数据库的结果是一对多的)有点不解;
其实,除了data-config.xml文件需要配置外,还有个重要的文件,managed-schema文件(本人使用的是solr7,老版本里是schema.xml,操作都一样,只要名称不同)也需要修改:
添加索引字段:
-
"id" type=
"string" indexed=
"true" stored=
"true" />
-
"name" type=
"string" indexed=
"true" stored=
"true" />
-
"manu" type=
"string" indexed=
"true" stored=
"true" />
-
"weight" type=
"string" indexed=
"true" stored=
"true" />
-
"price" type=
"string" indexed=
"true" stored=
"true" />
-
"popularity" type=
"string" indexed=
"true" stored=
"true" />
-
"inStock" type=
"string" indexed=
"true" stored=
"true" />
-
"includes" type=
"string" indexed=
"true" stored=
"true" />
-
-
"features" type=
"string" indexed=
"true" stored=
"true" />
-
"cat" type=
"string" indexed=
"true" stored=
"true" />
这个是最开始的配置,达不到理想的结果:
由于本人刚学习solr,不太熟悉,网上百度了一翻,才发现还有个:multiValued(多值的)属性,想要的是一个name对应多个description,那么就应该把description配置成多值的,所以添加multiValued属性,修改managed-schema文件:
-
"id" type=
"string" indexed=
"true" stored=
"true" />
-
"name" type=
"string" indexed=
"true" stored=
"true" />
-
"manu" type=
"string" indexed=
"true" stored=
"true" />
-
"weight" type=
"string" indexed=
"true" stored=
"true" />
-
"price" type=
"string" indexed=
"true" stored=
"true" />
-
"popularity" type=
"string" indexed=
"true" stored=
"true" />
-
"inStock" type=
"string" indexed=
"true" stored=
"true" />
-
"includes" type=
"string" indexed=
"true" stored=
"true" />
-
-
"features" type=
"string" indexed=
"true" stored=
"true" multiValued=
"true"/>
-
"cat" type=
"string" indexed=
"true" stored=
"true" multiValued=
"true"/>
再次导入数据,得到想要的结果了
到此完成:
但是在使用中又遇到了一个问题,比如solr查询记录有5条,使用Java查询:
-
HttpSolrClient solrServer = createSolrServer(
"goods");
-
SolrQuery query =
new SolrQuery();
-
query.set(
"q",
"*:*");
-
// 设置分页参数
-
query.setStart(
1);
-
// 每一页多少值
-
query.setRows(
10);
-
QueryResponse response = solrServer.query(query);
-
SolrDocumentList solrDocumentList = response.getResults();
每页10条,从第一页开始,一切看着没问题,可是结果只有4条,solr查询少了一条记录,百思不得其解...
原来数据库默认分页limit(mysql)是1,但solr是0,所以需要把query.setStart(1)--修改为-->query.setStart(0),从第一页开始,才是正确的!!!
solr继续学习中....