偷瓜的贼 发表于 2015-11-14 08:39:03

【Apache Solr系列】Solr QueryElevationComponent--实现竞价排名(手动干预)

  转载请注明源地址:http://blog.iyunv.com/weijonathan/article/details/39554579


  


  我们知道。在很多搜索网站上都有这样的一个功能,就是竞价排名,绑定某个关键字,给钱了那我给你排前面。
  包括很多视频网站的一些站内搜索功能也有这样的需求;
  最近在看solr官网,solr的查询功能中就包含了这一功能。参考地址如下:
  https://wiki.apache.org/solr/QueryElevationComponent#elevateIds.2FexcludeIds


  下面这个网址包含了solr所有功能;对solr感兴趣的可以收藏下!
  https://cwiki.apache.org/confluence/display/solr/Spell+Checking


  那么接下来我们来实现我们的功能把。
  solrconfig.xml
  

<searchComponent name=&quot;elevator&quot;
class=&quot;org.apache.solr.handler.component.QueryElevationComponent&quot;>
<!-- pick a fieldType to analyze queries -->
<str name=&quot;queryFieldType&quot;>string</str>
<str name=&quot;config-file&quot;>elevate.xml</str>
</searchComponent>
<!-- A request handler for demonstrating the elevator component -->
<requestHandler name=&quot;/elevate&quot;
class=&quot;org.apache.solr.handler.component.SearchHandler&quot; startup=&quot;lazy&quot;>
<lst name=&quot;defaults&quot;>
<str name=&quot;echoParams&quot;>explicit</str>
<!-- <str name=&quot;df&quot;>text</str> -->
</lst>
<arr name=&quot;last-components&quot;>
<str>elevator</str>
</arr>
</requestHandler>
  
  config-file:elevate.xml,这个文件solr默认已经存在,需要自己配置;默认跟solrconfig.xml同一目录,如无特殊要求可不做修改
  searchComponent的name属性是和requestHandler有一个绑定关系的,大家注意一下;
  还有就是,requestHandler定义的名称/elevate是我们后面需要用到的节点名,就像selelct一样。使用这一功能的时候把select换成elevate。

requestHandler标签中的defaults-->df是定义了你要做竞价排名,或者说人工干预的查询字段;如果此处不指定的话,需要在查询的时候指定;


  



<elevate>
<query text=&quot;isle&quot;>
<doc id=&quot;6493&quot; /><!-- put the actual ipod at the top -->
<doc id=&quot;3971&quot; exclude=&quot;true&quot; /> <!-- exclude this cable -->
</query>
</elevate>以上是一个简单的示例,当用户查询isle时,docID为6493的文档会被提到最前。而这里的docID为3971的文档将会被提出到查询的结果之外,因为配置了exclude属性为true。  
  如果不配置默认为false,即不排除!
  接下来我们查询下看看。这里先看下正常的查询结果。这里我们把enableElevation设置为false即可
  

<response>
<lst name=&quot;responseHeader&quot;>
<int name=&quot;status&quot;>0</int>
<int name=&quot;QTime&quot;>1</int>
<lst name=&quot;params&quot;>
<str name=&quot;enableElevation&quot;>false</str>
<str name=&quot;df&quot;>name</str>
<str name=&quot;q&quot;>isle</str>
<str name=&quot;forceElevation&quot;>true</str>
</lst>
</lst>
<result name=&quot;response&quot; numFound=&quot;6&quot; start=&quot;0&quot;>
<doc>
<str name=&quot;id&quot;>302</str>
<str name=&quot;name&quot;>The Isle</str>
<str name=&quot;year&quot;>2001</str>
<long name=&quot;_version_&quot;>1480190152307376130</long>
</doc>
<doc>
<str name=&quot;id&quot;>2</str>
<str name=&quot;name&quot;>Isle of Man TT 2004 Review</str>
<str name=&quot;year&quot;>2004</str>
<long name=&quot;_version_&quot;>1480190152202518528</long>
</doc>
<doc>
<str name=&quot;id&quot;>6493</str>
<str name=&quot;name&quot;>Val Lewton: Isle of the Dead / Bedlam</str>
<str name=&quot;year&quot;>1945</str>
<long name=&quot;_version_&quot;>1480190153170354189</long>
</doc>
<doc>
<str name=&quot;id&quot;>3871</str>
<str name=&quot;name&quot;>The Who: Live at the Isle of Wight Festival</str>
<str name=&quot;year&quot;>1970</str>
<long name=&quot;_version_&quot;>1480190152951201796</long>
</doc>
<doc>
<str name=&quot;id&quot;>9409</str>
<str name=&quot;name&quot;>Isle of Wight Festival: Message to Love: 1970</str>
<str name=&quot;year&quot;>1970</str>
<long name=&quot;_version_&quot;>1480190153367486468</long>
</doc>
<doc>
<str name=&quot;id&quot;>13617</str>
<str name=&quot;name&quot;>
Jethro Tull: Nothing Is Easy: Live at the Isle of Wight 1970
</str>
<str name=&quot;year&quot;>2005</str>
<long name=&quot;_version_&quot;>1480190153579298828</long>
</doc>
</result>
</response>

接下来我们把enableElevation设置为true,查询做下对比  
  http://localhost:8081/solr/collection1/elevate?q=isle&enableElevation=true&forceElevation=true


  查询结果如下:’
  

<response>
<lst name=&quot;responseHeader&quot;>
<int name=&quot;status&quot;>0</int>
<int name=&quot;QTime&quot;>18</int>
<lst name=&quot;params&quot;>
<str name=&quot;enableElevation&quot;>true</str>
<str name=&quot;df&quot;>name</str>
<str name=&quot;q&quot;>isle</str>
<str name=&quot;forceElevation&quot;>true</str>
</lst>
</lst>
<result name=&quot;response&quot; numFound=&quot;6&quot; start=&quot;0&quot;>
<doc>
<str name=&quot;id&quot;>6493</str>
<str name=&quot;name&quot;>Val Lewton: Isle of the Dead / Bedlam</str>
<str name=&quot;year&quot;>1945</str>
<long name=&quot;_version_&quot;>1480190153170354189</long>
</doc>
<doc>
<str name=&quot;id&quot;>302</str>
<str name=&quot;name&quot;>The Isle</str>
<str name=&quot;year&quot;>2001</str>
<long name=&quot;_version_&quot;>1480190152307376130</long>
</doc>
<doc>
<str name=&quot;id&quot;>2</str>
<str name=&quot;name&quot;>Isle of Man TT 2004 Review</str>
<str name=&quot;year&quot;>2004</str>
<long name=&quot;_version_&quot;>1480190152202518528</long>
</doc>
<doc>
<str name=&quot;id&quot;>3871</str>
<str name=&quot;name&quot;>The Who: Live at the Isle of Wight Festival</str>
<str name=&quot;year&quot;>1970</str>
<long name=&quot;_version_&quot;>1480190152951201796</long>
</doc>
<doc>
<str name=&quot;id&quot;>9409</str>
<str name=&quot;name&quot;>Isle of Wight Festival: Message to Love: 1970</str>
<str name=&quot;year&quot;>1970</str>
<long name=&quot;_version_&quot;>1480190153367486468</long>
</doc>
<doc>
<str name=&quot;id&quot;>13617</str>
<str name=&quot;name&quot;>
Jethro Tull: Nothing Is Easy: Live at the Isle of Wight 1970
</str>
<str name=&quot;year&quot;>2005</str>
<long name=&quot;_version_&quot;>1480190153579298828</long>
</doc>
</result>
</response>可以看到docID为6493的文档被提到最前。而docID为3971的文档被剔除结果集。


  
  好了,到这基本上功能就完成了。有什么不对的地方请多加指点;
  



版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: 【Apache Solr系列】Solr QueryElevationComponent--实现竞价排名(手动干预)