如何获取查询结果项的集合名称?

时间:2016-03-04 03:21:44

标签: solr

我有一个多集合solrcloud,需要使用相同的别名查询所有集合,如何获取结果项的集合名称?或者我必须把它放在一个领域?

这是一个asp.net mvc项目,我使用SolrNet连接到solrcloud。我用CastleContainer初始化了SolrNet。这是配置。 Core1,core2和core3的别名为coreAll。

<castle><facilities><facility id = 'solr' type = '***'>
  <solrURL> http://***.***.***.***:****/solr/</solrURL>
  <cores>
    <core id = 'coreAll'>
      <documentType> *** </documentType>
      <url> http://***.***.***.***:****/solr/coreAll</url>
    </core>
    <core id = 'core1'>
      <documentType> *** </documentType>
      <url> http://***.***.***.***:****/solr/core1</url>
    </core>
    <core id = 'core2'>
      <documentType> *** </documentType>
      <url> http://***.***.***.***:****/solr/core2</url>
    </core>
    <core id = 'core3'>
      <documentType> *** </documentType>
      <url> http://***.***.***.***:****/solr/core3</url>
    </core>
  </cores>
</facility></facilities></castle>

这是代码。我使用“coreAll”配置来构造这样的ISolrOperation对象。

var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(config)));
var operation = container.Resolve<ISolrOperations<Dictionary<string, object>>>("coreAll");

然后向Solr发送查询以查询所有三个集合。

    public static IEnumerable<string> GetSimilarArticles(ISolrOperations<Dictionary<string, object>> operation, string id, string[] keywords, int keywordCount = 2, int rowCount = 10, bool andQueryLogic = false)
    {
        if (String.IsNullOrWhiteSpace(id) || keywords.All(String.IsNullOrWhiteSpace))
            return new List<string>();

        var filtedKeywords = keywords.Where(k => !String.IsNullOrWhiteSpace(k));
        var query = new SolrQuery(String.Join(" ", filtedKeywords.Take(keywordCount).Select(k => "\"" + k + "\"")));
        var option = new QueryOptions
        {
            Rows = rowCount
        };
        if (andQueryLogic)
        {
            option.ExtraParams = new List<KeyValuePair<string, string>>
                    {
                        new KeyValuePair<string, string>("q.op", "AND")
                    };
        }
        var rst = operation.Query(query, option);
        var similars = rst == null || rst.Count == 0
                           ? new List<string>()
                           : rst.Where(i => i.ContainsKey("Id"))
                                .Select(i => i["Id"].ToString())
                                .Except(new[] { id });

        return similars;
    }

所以我的问题是我无法获得第一个项目来自哪个集合。现在我可以为每个集合添加一个包含集合名称的字段来解决这个问题,但是有更好的方法吗?

另外,我的solr版本是5.5.0。

0 个答案:

没有答案
相关问题