4

我在 Eclipse 上使用 Java 和 SolrJ。如何按某个字段上的值对 SolrQuery 的结果进行排序?例如,当我搜索特定作者的前 n 篇文章 ( docType=0 ) 时,我想按journal_facet字段(字符串类型)中值的频率对查询结果进行排序。

如果某个作者 X 写过:

  • 2 篇文章 (a0, a1) 在名为 J0 的期刊中
  • 在名为 J1 的期刊中发表了 3 篇文章(a2、a3、a4)
  • 1 篇文章 (a5) 在名为 J2 的期刊中

订单必须是 a2、a3、a4、a0、a1、a5,我想通过以下方式显示结果

<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a2</str>
 <str name="journal">J1</str>
</doc>
<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a3</str>
 <str name="journal">J1</str>
</doc>
<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a4</str>
 <str name="journal">J1</str>
</doc>
<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a0</str>
 <str name="journal">J0</str>
</doc>
<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a1</str>
 <str name="journal">J0</str>
</doc>
<doc>
 <arr name="author">
  <str>X</str>
 </arr>
 <str name="title">a5</str>
 <str name="journal">J2</str>
</doc>

我的查询是

SolrServer solrServer = new HttpSolrServer(urlString);
SolrQuery query = new SolrQuery();
query.set("q", "docType:0);
query.set("fq", "author:X");
query.set("fl", "author, title, journal");
query.setRows(n);
...
QueryResponse response = solrServer.query(query);
SolrDocumentList results = response.getResults();

在我的 Solr schema.xml 中有以下字段和类型

<types>
    ...
    <fieldType name="text_title" class="solr.TextField"
        positionIncrementGap="100">
        <analyzer type="index">
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <charFilter class="solr.HTMLStripCharFilterFactory" />
            <filter class="solr.ASCIIFoldingFilterFactory" />
            <filter class="solr.WordDelimiterFilterFactory"
                generateWordParts="1" generateNumberParts="1" catenateWords="1"
                catenateNumbers="1" catenateAll="1" splitOnCaseChange="1"
                stemEnglishPossessive="1" preserveOriginal="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front"/>
            <filter class="solr.KStemFilterFactory" />
            <filter class="solr.RemoveDuplicatesTokenFilterFactory" />
        </analyzer>
        <analyzer type="query">
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <filter class="solr.ASCIIFoldingFilterFactory" />
            <filter class="solr.WordDelimiterFilterFactory"
                generateWordParts="1" generateNumberParts="1" catenateWords="0"
                catenateNumbers="0" catenateAll="0" splitOnCaseChange="1"
                stemEnglishPossessive="1" preserveOriginal="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <filter class="solr.KStemFilterFactory" />
            <filter class="solr.RemoveDuplicatesTokenFilterFactory" />
        </analyzer>
    </fieldType>

    <fieldType name="text_name" class="solr.TextField"
        positionIncrementGap="100">
        <analyzer type="index">
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <charFilter class="solr.HTMLStripCharFilterFactory" />
            <filter class="solr.ASCIIFoldingFilterFactory" />
            <filter class="solr.WordDelimiterFilterFactory"
                generateWordParts="1" generateNumberParts="1" catenateWords="1"
                catenateNumbers="1" catenateAll="1" splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <!-- n-grams utile per la ricerca per prefisso" -->
            <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front"/>
            <!-- <filter class="solr.RemoveDuplicatesTokenFilterFactory"/> -->
        </analyzer>
        <analyzer type="query">
            <tokenizer class="solr.WhitespaceTokenizerFactory" />
            <filter class="solr.ASCIIFoldingFilterFactory" />
            <filter class="solr.WordDelimiterFilterFactory"
                generateWordParts="1" generateNumberParts="1" catenateWords="0"
                catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" />
            <filter class="solr.LowerCaseFilterFactory" />
            <!-- <filter class="solr.RemoveDuplicatesTokenFilterFactory"/> -->
        </analyzer>
    </fieldType>
</types>
<fields>
    <field name="docType" type="tint" indexed="true" stored="true"
        multiValued="false" required="true" />
    <field name="key" type="string" indexed="true" stored="true"
        multiValued="false" required="true" />
    <field name="mdate" type="date" indexed="true" stored="true"
        multiValued="false" required="true" />
    ...
    <field name="author" type="text_name" indexed="true" stored="true"
        multiValued="true" />
    ...
    <field name="journal" type="text_title" indexed="true" stored="true"
        multiValued="false" />
    <field name="title" type="text_title" indexed="true" stored="true"
        multiValued="false" />
    ...
    <field name="journal_facet" type="string" indexed="true" stored="true"
        multiValued="false" />
    ...
    <copyField dest="journal_facet" source="journal" />
    ...
</fields>

非常感谢你的帮助。

4

2 回答 2

0

编写自定义函数查询并按它排序怎么样:

http://localhost:8983/solr/select?q=*:*&sort=dist(2, point1, point2) desc

参考

于 2014-09-04T20:04:56.370 回答
0

如果您对结果facet.sort进行分面,只需使用按频率排序的分面:

https://wiki.apache.org/solr/SimpleFacetParameters#facet.sort

于 2014-12-19T16:00:02.540 回答