先看下官方文檔的例子:
假設有如下表結構:
data-config.xml:一對多,多對多的關系寫法如下:
<dataConfig>
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />
<document name="products">
<entity name="item" query="select * from item">
<field column="ID" name="id" />
<field column="NAME" name="name" />
<field column="MANU" name="manu" />
<field column="WEIGHT" name="weight" />
<field column="PRICE" name="price" />
<field column="POPULARITY" name="popularity" />
<field column="INSTOCK" name="inStock" />
<field column="INCLUDES" name="includes" />
<entity name="feature" query="select description from feature where item_id='${item.ID}'">
<field name="features" column="description" />
</entity>
<entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
<entity name="category" query="select description from category where id = '${item_category.CATEGORY_ID}'">
<field column="description" name="description" />
</entity>
</entity>
</entity>
</document>
</dataConfig>
一對多寫法:
<entity name="feature" query="select description from feature where item_id='${item.id}'">
<field name="feature" column="description" />
</entity>
多對多寫法:
<entity name="item_category" query="select category_id from item_category where item_id='${item.id}'">
<entity name="category" query="select description from category where id = '${item_category.category_id}'">
<field column="description" name="description" />
</entity>
</entity>
本人使用的是以上的寫法,api中也給出了另一種寫法,但自己沒有測試過:
<dataConfig>
<dataSource driver="org.hsqldb.jdbcDriver" url="jdbc:hsqldb:/temp/example/ex" user="sa" />
<document>
<entity name="item" query="select * from item">
<entity name="feature" query="select description as features from feature where item_id='${item.ID}'"/>
<entity name="item_category" query="select CATEGORY_ID from item_category where item_id='${item.ID}'">
<entity name="category" query="select description as cat from category where id = '${item_category.CATEGORY_ID}'"/>
</entity>
</entity>
</document>
</dataConfig>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
剛開始配置完成後,得出的結果始終是一對一:
如上圖:假如:item表和item_category表是一對多,但我這邊始終得出的結果是一對一,如:name和description不是一個name對應多個description(實際資料庫的結果是一對多的)有點不解;
其實,除了data-config.xml檔案需要配置外,還有個重要的檔案,managed-schema檔案(本人使用的是solr7,老版本裡是schema.xml,操作都一樣,隻要名稱不同)也需要修改:
添加索引字段:
<field name="id" type="string" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="manu" type="string" indexed="true" stored="true" />
<field name="weight" type="string" indexed="true" stored="true" />
<field name="price" type="string" indexed="true" stored="true" />
<field name="popularity" type="string" indexed="true" stored="true" />
<field name="inStock" type="string" indexed="true" stored="true" />
<field name="includes" type="string" indexed="true" stored="true" />
<field name="features" type="string" indexed="true" stored="true" />
<field name="cat" type="string" indexed="true" stored="true" />
這個是最開始的配置,達不到理想的結果:
由于本人剛學習solr,不太熟悉,網上百度了一翻,才發現還有個:multiValued(多值的)屬性,想要的是一個name對應多個description,那麼就應該把description配置成多值的,是以添加multiValued屬性,修改managed-schema檔案:
<field name="id" type="string" indexed="true" stored="true" />
<field name="name" type="string" indexed="true" stored="true" />
<field name="manu" type="string" indexed="true" stored="true" />
<field name="weight" type="string" indexed="true" stored="true" />
<field name="price" type="string" indexed="true" stored="true" />
<field name="popularity" type="string" indexed="true" stored="true" />
<field name="inStock" type="string" indexed="true" stored="true" />
<field name="includes" type="string" indexed="true" stored="true" />
<field name="features" type="string" indexed="true" stored="true" multiValued="true"/>
<field name="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繼續學習中....