solr多次排序不能按预期工作

时间:2013-08-06 14:05:47

标签: solr solr4

我的文档有2个索引属性 - 名称(字符串)和版本(编号)
我希望在相同的分数下,文件将按以下顺序显示

分数(降序),名称(降序),版本(降序)

因此我使用:

进行查询
http://localhost:8983/solr/vault/select?
           q=BOM&fl=*:score& 
           sort=score+desc,Name+desc,Version+desc

我在结果中得到以下内容:

<doc>
   <str name="Name">BOM Total test2</str>
   ...
   <int name="Version">2</int>
   ...
   <float name="score">2.2388418</float>
</doc>
<doc>
   <str name="Name">BOM Total test - Copy</str>
   ...
   <int name="Version">2</int>
   ...
   <float name="score">2.2388418</float>
</doc>
<doc>
  <str name="Name">BOM Total test2</str>
  ...
  <int name="Version">1</int>
  ...
  <float name="score">2.2388418</float>
</doc>

得分相等,但名称未排序。

我在这里做错了什么?

修改: 架构定义

 ....
 <field name="Name" type="text_en" indexed="true" stored="true" required="true"/>
 <field name="Version" type="int" indexed="true" stored="true" required="true"/>
 ....

1 个答案:

答案 0 :(得分:1)

如果您查看text_en fieldType中应用的所有令牌过滤器,分析器等,这使得它不太适合排序。对于字符串值的排序,最好使用特定的fieldType进行排序。在过去,我使用以下fieldType来排序字符串字段。

 <fieldType name="lowercase_sort" class="solr.TextField" positionIncrementGap="100">
    <analyzer>
        <tokenizer class="solr.KeywordTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
        <filter class="solr.TrimFilterFactory" />
    </analyzer>
 </fieldType>

Solr示例架构还包括以下用于排序的fieldType:

 <fieldType name="alphaOnlySort" class="solr.TextField" 
     sortMissingLast="true" omitNorms="true">
  <analyzer>
    <!-- KeywordTokenizer does no actual tokenizing, so the entire
         input string is preserved as a single token
      -->
    <tokenizer class="solr.KeywordTokenizerFactory"/>
    <!-- The LowerCase TokenFilter does what you expect, which can be
         when you want your sorting to be case insensitive
      -->
    <filter class="solr.LowerCaseFilterFactory" />
    <!-- The TrimFilter removes any leading or trailing whitespace -->
    <filter class="solr.TrimFilterFactory" />
    <!-- The PatternReplaceFilter gives you the flexibility to use
         Java Regular expression to replace any sequence of characters
         matching a pattern with an arbitrary replacement string, 
         which may include back references to portions of the original
         string matched by the pattern.

         See the Java Regular Expression documentation for more
         information on pattern and replacement string syntax.

         http://java.sun.com/j2se/1.6.0/docs/api/java/util/regex/package-summary.html
      -->
    <filter class="solr.PatternReplaceFilterFactory"
            pattern="([^a-z])" replacement="" replace="all"
    />
  </analyzer>
</fieldType>

然后定义一个用于排序的附加字段,可能如下所示:

 <field name="Name_Sort" type="lowercase_sort" indexed="true" stored="false"/>

使用copyField填充此字段

 <copyField src="Name" dest="Name_Sort"/>

然后对查询中的这个新Name_Sort字段进行排序。