2011-09-14 76 views
5

我想爲產品和供應商之間存在多對多關係的場景設計模式。可以通過以產品爲中心的方式或以供應商爲中心的方式進行搜索。一個產品可以由許多供應商提供,供應商將有許多產品。以下是我在想的解決方案,但似乎現場定義中有很多冗餘,是否需要2個實體定義來支持產品或供應商中心搜索。看起來不太合適。針對多對多實體定義的solr模式設計

在做一個供應商的搜索,「產品」可以被定義爲「多值=真」在做了一個產品的搜索,「供應商」可以被定義爲「多值=真正的」

<!-- Field definitions to support supplier search --> 
<field name="s_supplier" type="string" indexed="true" stored="true" > 
<field name="s_product" type="string" indexed="true" stored="true" multiValue="true"> 

<!-- Field definition to support product search --> 
<field name="p_product" type="string" indexed="true" stored="true" > 
<field name="p_supplier" type="string" indexed="true" stored="true" multiValue="true"> 

實體在DataHandler的定義是

<entity name="products" ....> 
    <field name="p_product" column=""> 
    <entity name="suppliers"> 
     <field name="p_supplier"> 
    </entity> 
</entity> 

<entity name="suppliers" ....> 
    <field name="s_supplier" column=""> 
    <entity name="products"> 
     <field name="s_product" column=""> 
    </entity> 
</entity> 

回答

9

Solr的搜索引擎的好處是,你可以隨便挑一個schema定義,無論是產品或供應商爲中心,然後利用的Solr的力量來達到你想要的結果。比方說,你去與產品中心之一,採用了以下內容:

<field name="product" type="string" indexed="true" stored="true" > 
<field name="supplier" type="string" indexed="true" stored="true" multiValue="true"> 

現在,您可以搜索產品剛剛通過運行鍼對該產品領域product:my product查詢,然後,如果你要搜索一個特定的供應商,您只需使用supplier:my supplier,並且由於供應商字段是與每個產品關聯的多值字段,因此您會將所有產品返回到該供應商關聯的位置。

更大的靈活性,另一種選擇是利用了在該示例Schema.xml文件中定義的text字段並使用'copyfield功能,供應商和產品的值複製到一個字段,然後你可以搜索它要麼和將返回與供應商或產品字段上的查詢字詞匹配的所有文檔。

這裏是一個例子,仍然使用上面定義的字段。

<field name="text" type="text" indexed="true" stored="false" multiValued="true"/> 
<copyField source="product" dest="text" /> 
<copyField source="supplier" dest="text" /> 

然後,如果你搜索text:my term它可以是產品或供應商,並在指數匹配字段將返回所有文檔。請注意,文本字段具有特定的索引和查詢時間分析器,因此您應該瞭解正在應用的內容。

此外,如果您需要生成唯一供應商列表,則可以利用Solr Faceting從索引中的所有文檔獲取該列表,或僅與當前搜索條件相關。

請看下面一些參考資料,詳細瞭解這些主題:

+0

感謝精心答案。我正在嘗試這些,將發佈結果。 – tech20nn