Solr 3.3.0 - 增加搜索中的字段权重 - 提升字段优先级

时间:2011-10-12 08:58:39

标签: solr field edismax

我正在使用solr 3.3.0索引这些文档:

<book id="bk101">
        <keywords>----</keywords>
        <title>----</title>
        <owner>----</owner>
        <artist>abcde</artist>
    </book>
    <book id="bk102">
        <keywords>----</keywords>
        <title>----</title>
        <owner>abcde</owner>
        <artist>----</artist>
    </book>
    <book id="bk103">
        <keywords>----</keywords>
        <title>----</title>
        <owner>----</owner>
        <artist>abc</artist>
    </book>
    <book id="bk104">
        <keywords>----</keywords>
        <title>----</title>
        <owner>abc</owner>
        <artist>----</artist>
    </book>
    <book id="bk105">
        <keywords>abcde</keywords>
        <title>----</title>
        <owner>----</owner>
        <artist>----</artist>
    </book>
    <book id="bk106">
        <keywords>abc</keywords>
        <title>----</title>
        <owner>----</owner>
        <artist>----</artist>
    </book>
    <book id="bk107">
        <keywords>----</keywords>
        <title>abcde</title>
        <owner>----</owner>
        <artist>----</artist>
    </book>
    <book id="bk108">
        <keywords>----</keywords>
        <title>abc</title>
        <owner>----</owner>
        <artist>----</artist>
    </book>

Schema.xml的

<types>
    <fieldType name="text" class="solr.TextField" positionIncrementGap="100" omitNorms="false">
        <tokenizer class="solr.WhitespaceTokenizerFactory" />
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="0" splitOnNumerics="0" />
        <filter class="solr.LowerCaseFilterFactory" />
        <filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true" />
        <filter class="solr.ISOLatin1AccentFilterFactory" />
        <filter class="solr.RemoveDuplicatesTokenFilterFactory" />
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="15" side="front" />
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="15" side="back" />
    </fieldType>

    <fieldType name="text_original" class="solr.TextField" positionIncrementGap="100" omitNorms="false">
        <tokenizer class="solr.KeywordTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
    </fieldType>

    <fieldType name="uuid" class="solr.UUIDField" indexed="true" />
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true" />
    <fieldType name="float" class="solr.FloatField" omitNorms="true" />
    <fieldType name="date" class="solr.DateField" />
</types>

<fields>
    <field name="id" type="uuid" indexed="true" stored="true" default="NEW" />
    <field name="book_id" type="string" indexed="true" stored="true" />
    <field name="keywords" type="text" indexed="true" stored="true" />
    <field name="keywords_ex" type="text_original" indexed="true" stored="true" />
    <field name="title" type="text" indexed="true" stored="true" />
    <field name="title_ex" type="text_original" indexed="true" stored="true" />
    <field name="owner" type="text" indexed="true" stored="true" />
    <field name="owner_ex" type="text_original" indexed="true" stored="true" />
    <field name="artist" type="text" indexed="true" stored="true" />
    <field name="artist_ex" type="text_original" indexed="true" stored="true" />

    <copyField source="title" dest="title_ex" />
    <copyField source="keywords" dest="keywords_ex" />
    <copyField source="owner" dest="owner_ex" />
    <copyField source="artist" dest="artist_ex" />

</fields>

正如你所看到的,如果我搜索'abc',solr将返回所有文档(我正在使用solr.EdgeNGramFilterFactory进行索引);我的成就是使用这个逻辑对文档进行排序:

  1. title_ex
  2. 标题
  3. keywords_ex
  4. 关键字
  5. owner_ex
  6. artist_ex
  7. 所有者
  8. 艺术家
  9. 我尝试了“^”lucene sintax和dismax,但响应列表不是我想要的那个。

    `的http://本地主机:8080 / solr33a / CORE0 /选择Q = ABC&安培; DEFTYPE = edismax&安培; QF = title_ex ^ 10%20title ^ 8.0%20keywords_ex ^ 6%20keywords ^ 5.5%20owner_ex ^ 1.2%20artist_ex ^ 0.8%20owner ^ 0.5%20artist ^ 0.2安培; FL = *,得分

    结果:

    <doc>
                <float name="score">2.3862944</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk108</str>
                <str name="id">2cc5d478-6901-4777-abc9-680fd959ef90</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">abc</str>
                <str name="title_ex">abc</str>
            </doc>
            <doc>
                <float name="score">1.4317766</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk106</str>
                <str name="id">e12683a2-faff-4d86-8107-7406491f4f89</str>
                <str name="keywords">abc</str>
                <str name="keywords_ex">abc</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
            <doc>
                <float name="score">0.3288517</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk107</str>
                <str name="id">a6a4a014-ce94-4257-a215-c1a64aa41cf5</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">abcde</str>
                <str name="title_ex">abcde</str>
            </doc>
            <doc>
                <float name="score">0.28635535</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk104</str>
                <str name="id">9b909c65-e56a-4407-a789-53a570a7ae40</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">abc</str>
                <str name="owner_ex">abc</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
            <doc>
                <float name="score">0.22608554</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk105</str>
                <str name="id">dead87cc-f93b-4562-af32-4d9fb2613c7f</str>
                <str name="keywords">abcde</str>
                <str name="keywords_ex">abcde</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
            <doc>
                <float name="score">0.19090356</float>
                <str name="artist">abc</str>
                <str name="artist_ex">abc</str>
                <str name="book_id">bk103</str>
                <str name="id">306a252c-a0b5-474d-b55d-a25740d063b4</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
            <doc>
                <float name="score">0.020553231</float>
                <str name="artist">----</str>
                <str name="artist_ex">----</str>
                <str name="book_id">bk102</str>
                <str name="id">a684de0c-b286-4d9e-bd68-d5305afeee76</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">abcde</str>
                <str name="owner_ex">abcde</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
            <doc>
                <float name="score">0.008221293</float>
                <str name="artist">abcde</str>
                <str name="artist_ex">abcde</str>
                <str name="book_id">bk101</str>
                <str name="id">30a0f9de-1224-49d2-90aa-41f57af4956c</str>
                <str name="keywords">----</str>
                <str name="keywords_ex">----</str>
                <str name="owner">----</str>
                <str name="owner_ex">----</str>
                <str name="title">----</str>
                <str name="title_ex">----</str>
            </doc>
    

1 个答案:

答案 0 :(得分:1)

如果要搜索具有可变权重的多个字段,请将请求处理程序配置为使用edismax查询解析器。
dismax允许您搜索字段并为每个字段添加权重。

实施例 标题匹配的权重为1,而匹配作者为0.8,因此具有标题匹配的文档将显示在顶部。

<requestHandler name="search" class="solr.SearchHandler" default="true">
 <lst name="defaults">
   <str name="echoParams">explicit</str>
   <str name="defType">edismax</str>
   <str name="qf">
      title^1 author^0.8
   </str>
   <str name="q.alt">*:*</str>
   <str name="rows">10</str>
   <str name="fl">*,score</str>
 </lst>
</requestHandler>