solr中一对多,多对多关系

先看下官方文档的例子:

假设有如下表结构:

data-config.xml:一对多,多对多的关系写法如下:


   
   
   
   
  1. "org.hsqldb.jdbcDriver" url= "jdbc:hsqldb:/temp/example/ex" user= "sa" />
  2. "products">
  3. "item" query= "select * from item">
  4. "ID" name= "id" />
  5. "NAME" name= "name" />
  6. "MANU" name= "manu" />
  7. "WEIGHT" name= "weight" />
  8. "PRICE" name= "price" />
  9. "POPULARITY" name= "popularity" />
  10. "INSTOCK" name= "inStock" />
  11. "INCLUDES" name= "includes" />
  12. "feature" query= "select description from feature where item_id='${item.ID}'">
  13. "features" column= "description" />
  14. "item_category" query= "select CATEGORY_ID from item_category where item_id='${item.ID}'">
  15. "category" query= "select description from category where id = '${item_category.CATEGORY_ID}'">
  16. "description" name= "description" />

一对多写法:


   
   
   
   
  1. "feature" query= "select description from feature where item_id='${item.id}'">
  2. "feature" column= "description" />

多对多写法:


   
   
   
   
  1. "item_category" query= "select category_id from item_category where item_id='${item.id}'">
  2. "category" query= "select description from category where id = '${item_category.category_id}'">
  3. "description" name= "description" />

本人使用的是以上的写法,api中也给出了另一种写法,但自己没有测试过:


   
   
   
   
  1. "org.hsqldb.jdbcDriver" url= "jdbc:hsqldb:/temp/example/ex" user= "sa" />
  2. "item" query= "select * from item">
  3. "feature" query= "select description as features from feature where item_id='${item.ID}'"/>
  4. "item_category" query= "select CATEGORY_ID from item_category where item_id='${item.ID}'">
  5. "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,操作都一样,只要名称不同)也需要修改:

添加索引字段:


   
   
   
   
  1. "id" type= "string" indexed= "true" stored= "true" />
  2. "name" type= "string" indexed= "true" stored= "true" />
  3. "manu" type= "string" indexed= "true" stored= "true" />
  4. "weight" type= "string" indexed= "true" stored= "true" />
  5. "price" type= "string" indexed= "true" stored= "true" />
  6. "popularity" type= "string" indexed= "true" stored= "true" />
  7. "inStock" type= "string" indexed= "true" stored= "true" />
  8. "includes" type= "string" indexed= "true" stored= "true" />
  9. "features" type= "string" indexed= "true" stored= "true" />
  10. "cat" type= "string" indexed= "true" stored= "true" />

这个是最开始的配置,达不到理想的结果:

由于本人刚学习solr,不太熟悉,网上百度了一翻,才发现还有个:multiValued(多值的)属性,想要的是一个name对应多个description,那么就应该把description配置成多值的,所以添加multiValued属性,修改managed-schema文件:


   
   
   
   
  1. "id" type= "string" indexed= "true" stored= "true" />
  2. "name" type= "string" indexed= "true" stored= "true" />
  3. "manu" type= "string" indexed= "true" stored= "true" />
  4. "weight" type= "string" indexed= "true" stored= "true" />
  5. "price" type= "string" indexed= "true" stored= "true" />
  6. "popularity" type= "string" indexed= "true" stored= "true" />
  7. "inStock" type= "string" indexed= "true" stored= "true" />
  8. "includes" type= "string" indexed= "true" stored= "true" />
  9. "features" type= "string" indexed= "true" stored= "true" multiValued= "true"/>
  10. "cat" type= "string" indexed= "true" stored= "true" multiValued= "true"/>

再次导入数据,得到想要的结果了

到此完成:

但是在使用中又遇到了一个问题,比如solr查询记录有5条,使用Java查询:


   
   
   
   
  1. HttpSolrClient solrServer = createSolrServer( "goods");
  2. SolrQuery query = new SolrQuery();
  3. query.set( "q", "*:*");
  4. // 设置分页参数
  5. query.setStart( 1);
  6. // 每一页多少值
  7. query.setRows( 10);
  8. QueryResponse response = solrServer.query(query);
  9. SolrDocumentList solrDocumentList = response.getResults();

每页10条,从第一页开始,一切看着没问题,可是结果只有4条,solr查询少了一条记录,百思不得其解...

原来数据库默认分页limit(mysql)是1,但solr是0,所以需要把query.setStart(1)--修改为-->query.setStart(0),从第一页开始,才是正确的!!!

solr继续学习中....

你可能感兴趣的:(Java,项目开发)