简体   繁体   English

使用多个字段对Solr搜索结果进行排序(solrj)

[英]Sorting solr search results using multiple fields (solrj)

I need to sort the results I get back from apache solr based on two factors: 我需要基于两个因素对从apache solr返回的结果进行排序:

There are three entities in our system that are indexed by solr (groups, projects and datasets) and in the results I want datasets to be displayed first, followed by projects and then groups; 在我们的系统中,有三个由solr索引的实体(组,项目和数据集),在结果中我希望首先显示数据集,然后是项目,然后是组。 but I still want it to respect to score values for each of the types. 但我仍然希望它尊重每种类型的得分值。

So, for example: results would be 因此,例如:结果将是

  1. Dataset with score of 0.325 数据集得分为0.325
  2. Dataset with score of 0.282 数据集得分为0.282
  3. Dataset with score of 0.200 数据集得分为0.200
  4. Project with score of 0.298 得分为0.298的项目
  5. Project with score of 0.186 得分为0.186的项目
  6. Group with score of 0.360 得分为0.360的组
  7. Group with score of 0.270 得分为0.270的组

I'm doing this in java and using solrj to construct the solr queries. 我正在用Java执行此操作,并使用solrj构造solr查询。 The problem is that when I try to add 2 sort fields to the SolrQuery object it only seems to use one of them. 问题是,当我尝试向SolrQuery对象添加2个排序字段时,似乎只使用其中之一。 Also in the solr documents there isn't anything to state the entity type but the document's ID is prefixed with the entity name so I was planning to use that. 同样在solr文档中,没有任何东西可以说明实体类型,但是文档ID带有实体名称前缀,因此我打算使用它。

If anyone has got any ideas on how I could achieve this it would be greatly appreciated as I've been stuck on this for a while now! 如果有人对如何实现此目标有任何想法,将不胜感激,因为我已经坚持了一段时间!

Thanks in advance, -Jake. 预先感谢-杰克。

If it looks like solrj is using just one of the sort fields, maybe you're using the SolrQuery#setSortField method instead of the addSortField like this: 如果看起来solrj仅使用排序字段之一,则可能您使用的是SolrQuery#setSortField方法而不是addSortField如下所示:

solrQuery.addSortField("field1", ORDER.desc);
solrQuery.addSortField("field2", ORDER.desc);

In order to sort the results as you described, a cleaner solution would be adding a field to your index containing a kind of weight based on the entity type. 为了按照您所描述的那样对结果进行排序,一个更干净的解决方案是在索引中添加一个字段,该字段包含一种基于实体类型的权重。 Then you could directly sort using that field: weight desc, score desc . 然后,您可以使用该字段直接排序: weight desc, score desc Before doing this, just make sure the weight has always to win against the solr score, otherwise you should work on influencing the solr score. 在执行此操作之前,只需确保权重始终与Solr得分相抗衡,否则您应该努力影响Solr得分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM