3

I am using Solr to index documents and now I need to search those documents for an exact phrase and sort the results by the number of times this phrase appears on the document. I also have to present the number of times the phrase is matched back to the user.

I was using the following query (here I am searching by the word SAP):

{
    :params => {
            :wt => "json",
        :indent => "on",
          :rows => 100,
         :start => 0,
             :q => "((content:SAP) AND (doc_type:ClientContact) AND (environment:production))",
          :sort => "termfreq(content,SAP) desc",
            :fl => "id,termfreq(content,SAP)"
    }
}

Of course this is a representation of the actual query, that is done by transforming this hash into a query string at runtime.

I managed to get the search working by using content:"the query here" instead of content:the query here, but the hard part is returning and sorting by the termfreq.

Any ideas on how I could make this work?

Obs: I am using Ruby but this is a legacy application and I can't use any RubyGems, I am using the HTTP interface to Solr here.

4

2 回答 2

2

我能够使它工作添加一个ShingleFilter到我的schema.xml

就我而言,我开始使用 SunSpot,所以我只需要进行以下更改:

<!-- *** This fieldType is used by Sunspot! *** -->
<fieldType name="text" class="solr.TextField" omitNorms="false">
  <analyzer>
    <tokenizer class="solr.StandardTokenizerFactory"/>
    <filter class="solr.StandardFilterFactory"/>
    <filter class="solr.LowerCaseFilterFactory"/>
    <!-- This is the line I added -->
    <filter class="solr.ShingleFilterFactory" maxShingleSize="4" outputUnigrams="true"/>
  </analyzer>
</fieldType>

在进行该更改、重新启动 Solr 并重新编制索引之后,我能够termfreq(content, "the query here")在查询 ( q=)、返回字段 ( fl=) 甚至排序 ( sort=) 上都使用它们。

于 2013-04-04T02:25:08.827 回答
0

放在debug=resultssolr url 的末尾,它也会给你短语 freq。

于 2018-02-27T11:37:26.240 回答