具有结果分组和突出显示的SolrNet传统映射

时间:2016-08-23 21:54:39

标签: c# solr grouping highlighting solrnet

SolrNet在QueryOptions中对结果进行分组和突出显示没有检索到任何结果。我认为我的问题可能是我正在使用的传统映射。 下边是 核心添加为:

windsorSolrStartUp.AddCore(suggestionsIndex.IndexName,typeof(SuggestionsIndexMapper), ConfigurationSettings.ContentSearch_Solr_ServiceBaseAddress + "/" + suggestionsIndex.IndexName);

SuggestionsIndexMapper.cs

  public class SuggestionsIndexMapper: ISuggestionsIndexMapper
  {
    public SuggestionsIndexMapper()
    {
    }

    /// <summary>
    /// Composite ID: contains SuggestionType,SitecoreItemID,Site,Language
    /// Created on fly during indexing in solr. 
    /// Checkout schema.xml and solrconfig.xml
    /// </summary>
    [SolrUniqueKey("compositeId")]
    public string CompositeId { get; set; }

    /// <summary>
    /// SitecoreId
    /// </summary>
    [SolrField("sitecoreId")]
    public string SitecoreId { get; set; }

    /// <summary>
    /// DisplayName
    /// </summary>
    [SolrField("name")]
    public string Name { get; set; }

    /// <summary>
    /// Imageurl
    /// </summary>
    [SolrField("imageUrl")]
    public string ImageUrl { get; set; }

    /// <summary>
    /// Url
    /// </summary>
    [SolrField("url")]
    public string Url { get; set; }
  }

SolrSearchContext如下所示:

 public class SolrSearchContext<T> : ISolrSearchContext<T>
{
    public readonly ISolrOperations<T> IndexOperations;

    public SolrSearchContext() {}

    /// <summary>
    /// Setting the indexOperations for given index
    /// </summary>
    /// <param name="indexName"></param>
    public SolrSearchContext(string indexName)
    {
        IOC ioc = new IOC();
        IndexOperations = ioc.GetContainer().Resolve<ISolrOperations<T>>(indexName);
    }

    /// <summary>
    /// Get IndexOperations for index
    /// </summary>
    /// <returns></returns>
    public ISolrOperations<T> GetIndexOperations()
    {
        return IndexOperations;
    }
public SolrQueryResults<T> ExecuteQuery(ISolrQuery query, QueryOptions queryOptions = null)
    {
        try
        {
            Logger.Debug(string.Format("SolrSearchContext - ExecuteQuery: Executing your query {0}", query));
            if (queryOptions != null)
            {
                return IndexOperations.Query(query, queryOptions);
            }
            return IndexOperations.Query(query);
        }
        catch(Exception e)
        {
            Logger.Debug(string.Format("SolrSearchContext - ExecuteQuery: Error while executing the query - {0} is {1}", query, e));
            return new SolrQueryResults<T>();
        }
    }
}

索引是:

public class SuggestionsIndex: SearchIndex<SuggestionsIndexMapper>
{
    public override string IndexName
    {
        get { return "suggestions_index"; }
    }

    public override string SwapIndexName
    {
        get { return "suggestions_index_swap"; }
    }
}

public class SearchIndex<T> : ISearchIndex<T>
{
    public ISolrSearchContext<T> CreateSearchContext()
    {
        return new SolrSearchContext<T>(IndexName);
    }

    public virtual string IndexName { get; set; }
    public virtual string SwapIndexName { get; set; }
}

因此,对于每个索引,我们将SolrSearchContext初始化为其Mapping类,在本例中为SuggestionsIndexMapper。

现在查询Solr时,

  ISolrQuery query = QueryBuilder(site, language, term);
        QueryOptions queryOptions = new QueryOptions()
        {
            Highlight = new HighlightingParameters()
            {
                Fields = new[] {"uvw"} 
            },
            Grouping = new GroupingParameters()
            {
                Fields = new[] { "xyz"},
                Format = GroupingFormat.Grouped,
                Limit = 3
            }
        };
  SuggestionsIndex index = new SuggestionsIndex();
  ISolrSearchContext<SuggestionsIndexMapper> searchContext = index.CreateSearchContext();
  var results = searchContext.ExecuteQuery(query, queryOptions).ToList();

结果为空。如果我不使用Result分组或突出显示,它们会正确映射到List of SuggestionsIndexMapper类型的结果变量。

我应该如何映射数据,以便可以访问solr结果中的各个组以及高亮显示结果?

任何帮助都会很棒。 谢谢,

1 个答案:

答案 0 :(得分:0)

通过不执行.toList()来获取executeQuery的结果,我能够访问结果分组和突出显示。 代替:      var results = searchContext.ExecuteQuery(query,queryOptions).ToList(); 基本上只做:      var results = searchContext.ExecuteQuery(query,queryOptions);      Var highlight = results.higlights;      var grouping = results.grouping;

这给出了solr的全部结果。分组和亮点。