2013-01-02 46 views
0

我正在使用SolrNet在一组表上搜索数据。如何在SolrNet的多个表上执行索引/搜索

我有两个表分类和项目。

这两个表具有相同的字段,因此我有一个基础映射类形式,我导出。索引分类数据

[Serializable] 
    [XmlRoot(ElementName = "SolrSearchEntity", Namespace = "")] 
    [DataContract(Name = "SolrSearchEntity", Namespace = "")] 
    public class SolrSearchEntity 
    { 
     [XmlElement(ElementName = "Id")] 
     [DataMember(Name = "Id", IsRequired = true)] 
     [SolrUniqueKey("id")] 
     public string Id { get; set; } 

     [XmlElement(ElementName = "Name")] 
     [DataMember(Name = "Name", IsRequired = true)] 
     [SolrField("name")] 
     public string Name { get; set; } 
    } 

public class Category : SolrSearchEntity 
    {   
    } 

    public class Item : SolrSearchEntity 
    { 
    } 

代码块

using (SolrBaseRepository.Instance<Category> repository = new SolrBaseRepository.Instance<Category>()) 
      { 
       var output = ItemStoreDataManager.GetAllCategoryNames(dataAdapter); 
       repository.Start(); 
       var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Category>>(); 
       solr.AddRange(output); 
       solr.Commit(); 
      } 

索引项数据代码块

using (SolrBaseRepository.Instance<Item> repository = new SolrBaseRepository.Instance<Item>()) 
       { 
        var output = ItemStoreDataManager.GetAllItemNames(dataAdapter); 
        repository.Start(); 
        var solr = ServiceLocator.Current.GetInstance<ISolrOperations<Item>>(); 
        solr.AddRange(output); 
        solr.Commit(); 
       } 

我Schema.xml的具有

<fields> 
    <!-- declare fields of entity class --> 
    <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
    <field name="name" type="text_general" indexed="true" stored="true" omitNorms="true"/> 

    <field name="text" type="text_general" indexed="true" stored="false" multiValued="true"/> 
    <field name="_version_" type="long" indexed="true" stored="true"/> 

    </fields> 

查询的类别数据

var entities = ItemStoreManager<Category>.Search(keyword); which will internally execute this. 

    new SolrBaseRepository.Instance<T>().Start(); 
    var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>(); 
    var results = solr.Query(keyword); 

奇怪的是,我从项目表中获取的记录。

我该如何告诉SolrNet(搜索引擎)来查看我指定的类型。

或者我在第一个地方正确编制索引?

请帮忙。

感谢,

回答

0

除了由库克提到的建议,

添加GUID场SolrSearchEntity

[XmlElement(ElementName = "UId")] 
[DataMember(Name = "UId", IsRequired = true)] 
[SolrUniqueKey("uid")] 
public Guid UId { get; set; } 

初始化自身UID与构造

public Item() 
{ 
    Type = "Item"; 
    UId = Guid.NewGuid(); 
} 

变化schema.xml中

<fieldType name="uuid" class="solr.UUIDField" indexed="true" /> 

<!-- unique field --> 
<field name="uid" type="uuid" indexed="true" stored="true" /> 

<uniqueKey>uid</uniqueKey> 

现在索引不会重叠或不一致,搜索将缩小到指定的类型。

0

对于从数据库索引多个表,我宁愿去Solr DIH其灵活性,而不是任何客户端库。

1

当您将数据添加到索引并正在基于客户端上的已知类型进行查询时将数据分开,您将它们全部存储在Solr中的相同模式中,并且没有指定任何方式来区分项目Solr中的类别记录。我建议你修改你的架构,包括一个型字段,它可以只是一个简单的字符串,如下列:

<field name="type" type="string" indexed="true" stored="true" /> 

然后,你需要将类型字段添加到您的SolrSearchEntity基类,并将其设置适当地在类别和项目类别中。像下面这样:

[Serializable] 
[XmlRoot(ElementName = "SolrSearchEntity", Namespace = "")] 
[DataContract(Name = "SolrSearchEntity", Namespace = "")] 
public class SolrSearchEntity 
{ 
    [XmlElement(ElementName = "Id")] 
    [DataMember(Name = "Id", IsRequired = true)] 
    [SolrUniqueKey("id")] 
    public string Id { get; set; } 

    [XmlElement(ElementName = "Name")] 
    [DataMember(Name = "Name", IsRequired = true)] 
    [SolrField("name")] 
    public string Name { get; set; } 

    [SolrField("type")] 
    public string Type {get; set;} 
} 

public class Category : SolrSearchEntity 
{   
    public Category() 
    { 
     Type = "Category"; 
    } 
} 

public class Item : SolrSearchEntity 
{ 
    public Item() 
    { 
     Type = "Item"; 
    } 
} 

然后当你,如果你想通过一个特定的类型以限制查询执行搜索,你可以这样添加过滤器查询仅搜索项目:

var entities = ItemStoreManager<Category>.Search(keyword); 
new SolrBaseRepository.Instance<T>().Start(); 
var solr = ServiceLocator.Current.GetInstance<ISolrOperations<T>>(); 
var options = new QueryOptions{ 
    FilterQueries = new ISolrQuery[] { new SolrQueryByField("type","Item")}}; 
var results = solr.Query(keyword, options); 

此外,请注意您的ID值在数据库中不重叠,因为这会导致您仅存储实体的一个实例(类别或项目,以最后索引为准)。

最后,我觉得你现在的索引策略是合理的,不会推荐使用DIH。国际海事组织,您将拥有使用SolrNet客户端库的更多控制和灵活性。过去我遇到过数据导入处理程序的问题,并发现SolrNet的使用更容易管理和维护。

+0

谢谢厨师。我采取了你所建议的方法。但是,表格的id值与正在运行的数字相似(重叠)。因此,我收到不正确的数据,查询类别退货项目数据。如何处理这个? – Prasad

+0

谢谢厨师。我采取了你所建议的方法。但是,表格的id值与正在运行的数字相似(重叠)。因此,我收到不正确的数据,查询类别退货项目数据。如何处理这个? – Prasad