阻止SOLR多次索引相同的单词(或如何提升一个字段)

时间:2013-10-05 15:42:59

标签: solr django-haystack

所以我正在运行一个俚语字典类型的网站,之前一直使用mysql LIKE进行网站搜索。它运作正常。无论如何,现在我正在更新网站并且正在考虑使用带有SOLR的django-haystack(似乎是最好的搜索选项之一?)

我让它运行,但搜索结果没有任何好处。例如,搜索单词“LOL”会将“Flood”作为第一个结果,因为它还有一个充满“LOL LOL LOL LOL LOL LOL”的示例,而不是首先显示单词LOL。

因此只能从Flood示例中仅标记一个LOL(我是SOLR的新手,所以这可能是我错误的方式)。或者我可以只提高单词标题的值(所以搜索项与标题匹配的单词首先出现,而searchterm与示例匹配的单词排在第二位)?我已经尝试了django-haystack场地提升,但它似乎没有做太多。

提前致谢!

编辑:这是SOLR计划(它有点大,主要由Django-Haystack自动生成):

<?xml version="1.0" ?>
<schema name="default" version="1.1">
  <types>
    <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>

    <!-- Numeric field types that manipulate the value into
         a string value that isn't human-readable in its internal form,
         but with a lexicographic ordering the same as the numeric ordering,
         so that range queries work correctly. -->
    <fieldType name="sint" class="solr.SortableIntField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="slong" class="solr.SortableLongField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="sfloat" class="solr.SortableFloatField" sortMissingLast="true" omitNorms="true"/>
    <fieldType name="sdouble" class="solr.SortableDoubleField" sortMissingLast="true" omitNorms="true"/>

    <fieldType name="date" class="solr.DateField" sortMissingLast="true" omitNorms="true"/>

    <fieldType name="text" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <!-- in this example, we will only use synonyms at query time
        <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>
        -->
        <!-- find finnish ones <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt"/> -->
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
        <filter class="solr.SnowballPorterFilterFactory" language="Finnish" />
        <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front" />
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.SnowballPorterFilterFactory" language="Finnish" />
        <!-- <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/> -->
        <!-- find finnish ones <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt"/> -->
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
      </analyzer>
    </fieldType>

    <fieldType name="text_ws" class="solr.TextField" positionIncrementGap="100">
      <analyzer>
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
      </analyzer>
    </fieldType>

    <fieldType name="ngram" class="solr.TextField" >
      <analyzer type="index">
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.NGramFilterFactory" minGramSize="3" maxGramSize="15" />
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.KeywordTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

    <fieldType name="edge_ngram" class="solr.TextField" positionIncrementGap="1">
      <analyzer type="index">
        <tokenizer class="solr.WhitespaceTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1"/>
        <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="15" side="front" />
      </analyzer>
      <analyzer type="query">
        <tokenizer class="solr.WhitespaceTokenizerFactory" />
        <filter class="solr.LowerCaseFilterFactory" />
        <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="1"/>
      </analyzer>
    </fieldType>
  </types>

  <fields>   
    <!-- general -->
    <field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
    <field name="django_ct" type="string" indexed="true" stored="true" multiValued="false" />
    <field name="django_id" type="string" indexed="true" stored="true" multiValued="false" />

    <dynamicField name="*_i"  type="sint"    indexed="true"  stored="true"/>
    <dynamicField name="*_s"  type="string"  indexed="true"  stored="true"/>
    <dynamicField name="*_l"  type="slong"   indexed="true"  stored="true"/>
    <dynamicField name="*_t"  type="text"    indexed="true"  stored="true"/>
    <dynamicField name="*_b"  type="boolean" indexed="true"  stored="true"/>
    <dynamicField name="*_f"  type="sfloat"  indexed="true"  stored="true"/>
    <dynamicField name="*_d"  type="sdouble" indexed="true"  stored="true"/>
    <dynamicField name="*_dt" type="date"    indexed="true"  stored="true"/>


    <field name="rendered" type="string" indexed="false" stored="true" multiValued="false" />

    <field name="word" type="text" indexed="true" stored="true" multiValued="false" />

    <field name="author" type="text" indexed="true" stored="true" multiValued="false" />

    <field name="text" type="text" indexed="true" stored="true" multiValued="false" />

    <field name="explanation" type="text" indexed="true" stored="true" multiValued="false" />

    <field name="example" type="text" indexed="true" stored="true" multiValued="false" />

  </fields>

  <!-- field to use to determine and enforce document uniqueness. -->
  <uniqueKey>id</uniqueKey>

  <!-- field for the QueryParser to use when an explicit fieldname is absent -->
  <defaultSearchField>text</defaultSearchField>

  <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
  <solrQueryParser defaultOperator="AND" />
</schema>

1 个答案:

答案 0 :(得分:3)

你的最后评论为我解决了这个问题。您需要注意的是Relevance in general以及特殊情况下的Field Boosting。

为了在字段上使用该查询时增强,您需要使用Solr的DisMax Handler或其扩展eDisMax处理程序。你可以通过'qf parameter告诉该处理程序要搜索的字段以及每个字段的提升方式。

e.g。

qf="word^10.0 title^5.0 exmaple^0.5"
  • 如果文档与单词匹配匹配,则将该分数增加10倍
  • 如果文档与标题中的匹配匹配,则将该分数增加5
  • 如果文档与示例中的匹配匹配,则将该分数增加0.5,这相当于减少

您可以在发送给Solr的每个搜索查询中添加该qf参数,也可以在solrconfig.xml中对其进行配置。

<requestHandler name="standard" 
    class="solr.StandardRequestHandler" default="true">
    <!-- default values for query parameters -->
    <lst name="defaults">
        <str name="defType">edismax</str>
        <str name="q.alt">*:*</str>
        <str name="qf">word^10.0 title^5.0 exmaple^0.5</str>
        <str name="fl">*,score</str>
        <str name="mm">100%</str>
    </lst>
</requestHandler>

<queryParser name="edismax" 
    class="org.apache.solr.search.ExtendedDismaxQParserPlugin" />

进一步阅读