本文介绍了使用多个字段对solr搜索结果进行排序(solrj)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据两个因素对从apache solr返回的结果进行排序:

I need to sort the results I get back from apache solr based on two factors:

我们的系统中有三个实体被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; but I still want it to respect to score values for each of the types.

所以,例如:结果将是


  1. 数据集得分为0.325

  2. 得分为0.282的数据集

  3. 得分为0.200的数据集

  4. 得分为0.298的项目

  5. 得分为0.186的项目

  6. 得分为0.360的小组

  7. 得分为0.270的小组

  1. Dataset with score of 0.325
  2. Dataset with score of 0.282
  3. Dataset with score of 0.200
  4. Project with score of 0.298
  5. Project with score of 0.186
  6. Group with score of 0.360
  7. Group with score of 0.270

我在 java 中使用 solrj 来构建solr查询。问题是,当我尝试向SolrQuery对象添加2个排序字段时,它似乎只使用其中一个。同样在solr文档中,没有任何内容可以说明实体类型,但文档的ID以实体名称为前缀,因此我打算使用它。

I'm doing this in java and using solrj to construct the solr queries. The problem is that when I try to add 2 sort fields to the SolrQuery object it only seems to use one of them. 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.

如果有人对如何实现这一点有任何想法,我将非常感激,因为我已经坚持了一段时间了!

提前致谢,
-Jake。

Thanks in advance,-Jake.

推荐答案

如果看起来solrj只使用其中一个排序字段,那么你可能正在使用方法而不是 addSortField 像这样:

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:

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

为了按照您的描述对结果进行排序,更清洁的解决方案是在索引中添加一个字段包含一种基于实体类型的权重。然后你可以直接使用该字段排序: weight desc,得分desc 。在此之前,只需确保权重总是胜过solr得分,否则你应该努力影响solr得分。

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. 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搜索结果进行排序(solrj)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:12