设为首页 收藏本站
查看: 1081|回复: 0

[经验分享] 【solr专题之四】关于VelocityResponseWriter

[复制链接]

尚未签到

发表于 2015-7-19 08:05:20 | 显示全部楼层 |阅读模式
  


  一、关于Velocity的基本配置
  在Solr中,可以以多种方式返回搜索结果,如单纯的文本回复(XML、JSON、CSV等),也可以返回velocity,js等格式。而VelocityResponseWriter就是用于将返回velocity类型文本,以便直接用于结果呈现。
  在Solr提供的example,其中的一个RequestHandler--/browse,使用了VelocityResponseWriter。其配置如下:
  

  

explicit

velocity
browse
layout
Solritas_test

edismax

text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4
title^10.0 description^5.0 keywords^5.0 author^2.0 resourcename^1.0

text
100%
*:*
10
*,score

text^0.5 features^1.0 name^1.2 sku^1.5 id^10.0 manu^1.1 cat^1.4
title^10.0 description^5.0 keywords^5.0 author^2.0 resourcename^1.0

text,features,name,sku,id,manu,cat,title,description,keywords,author,resourcename
3

on
cat
manu_exact
content_type
author_s
ipod
GB
1
cat,inStock
after
price
0
600
50
popularity
0
10
3
manufacturedate_dt
NOW/YEAR-10YEARS
NOW
+1YEAR
before
after

on
content features title name
html


0
title
0
name
3
200
content
750

on
false
5
2
5
true
true
5
3



spellcheck


  


  关于velocity这个writer的定义如下:
  

  

  处理一个流程的步骤如下:
  


1、根据请求url查找相关的配置

请求的url为:http://localhost:8983/solr/browse

则在solrConfig.xml中查找  /browse的配置,可以得出上述所示的结果。




2、其中有关velocity的内容如下:

  

explicit

velocity
browse
layout
Solritas_test

从上述定义中开始分别查找显示层的内容(vm文件)与处理类的内容(wt的实现类。)




3、定位显示内容模板

根据v.template属性,定义到文件browse.vm,注意在配置中省略了后缀名vm。

由于存在v.layout属性,因此,此属性的值将作为模板,而v.template中的内容将作为$content的内容。




  • v.layout: Template name that wraps main template (v.template). Main template renders to a $content that can be used in layout template.


layout.vm的内容如下:

#**
*  Overall HTML page layout
*#


#parse("head.vm")


Solr Admin

#parse("header.vm")


#parse("tabs.vm")


$content


#parse("footer.vm")







4、定位ResponseWriter

从第2步的结果知道,使用的velocity,然后查找这个wt的定义,可以得到

   

即VelocityResponseWriter实现类,其定义如下:
public class VelocityResponseWriter
extends Object
implements QueryResponseWriter


与Solr返回Velocity相关的类只有4个:



  • PageTool
  • SolrParamResourceLoader
  • SolrVelocityResourceLoader
  • VelocityResponseWriter


  
  
  


  
  以下是关于VelocityResponseWriter的官方说明:http://wiki.apache.org/solr/VelocityResponseWriter
  

Introduction

  
VelocityResponseWriter (aka Solritas) enables
Solr to respond with content generated from Velocity templates. Along with technologies like SolrJS, this makes
Solr itself capable of driving sophisticated search interfaces without the need for an intermediate application server between the browser and Solr.
See SOLR-620 for more
information.





Contents


  • Introduction
  • Instructions to use, Solr 1.4+

    • Sample Usage

      • Using the VelocityResponseWriter
        in Solr Core




  • Instructions to use, Solr 4.0+

    • Sample Usage


  • Options (All Versions)
  • Velocity Context
  • TODO




Instructions to use, Solr 1.4+


These steps will get you up and running for the examples below:


  • Download and install Solr 1.4.x
  • Fire up Solr: cd example; java -Dsolr.solr.home=../contrib/velocity/src/main/solr/ -jar start.jar
  • Index sample docs: cd example/exampledocs; java -jar post.jar *.xml
  • Hit the examples below...



Sample Usage


http://localhost:8983/solr/itas


  • Renders browse.vm from conf/velocity. Faceted navigation included.

http://localhost:8983/solr/itas?v.template.header=Custom%20Header


  • Renders browse.vm, but overrides the header.vm from conf/velocity with the specified value.

http://localhost:8983/solr/itas?debugQuery=true


  • Renders browse.vm, adding in explanation views per hit, and a Velocity context dump at the end.




Using the VelocityResponseWriter in Solr Core



The VelocityResponseWriter is still a contrib component in Solr 1.4.x. In order to use it with the core distributions the following steps need to be followed:
The following jars need to be copied from contrib/velocity/src/main/solr/lib/ to $SOLR_HOME/lib:


  • apache-solr-velocity-1.4-dev.jar
  • velocity-1.6.1.jar
  • velocity-tools-2.0-beta3.jar
  • commons-beanutils-1.7.0.jar
  • commons-collections-3.2.1.jar

The VelocityResponseWriter uses a more recent version of the commons lang jar than the current version of Solr core, so the jar commons-lang-2.4.jar from .../contrib/velocity/src/main/solr/lib/
should replace $SOLR_HOME/lib/commons-lang-2.1.jar
Add some configuration for this ResponseWriter to
solrconfig.xml like this:


   

Set up a RequestHandler in
solrconfig.xml:


  
     
       browse
       velocity.properties
       text/html;charset=UTF-8
       Solritas

       velocity
       dismax
       *:*
       10
       *,score
       on
       title
       1
      
          text^0.5 title^1.5
      
     
     
      
     
  

Copy the .../contrib/velocity/src/main/solr/conf/velocity directory to $SOLR_HOME/conf/. This directory contains the Velocity templates that will be needed by the VelocityResponseWriter, and
also a style sheet, main.css. The templates and style sheet can be edited to customize the display.


Instructions to use, Solr 4.0+


These steps will get you up and running for the examples below:



  • Check out Solr trunk: svn co http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/
  • Build Solr: ant clean example
  • Fire up Solr: cd example; java -jar start.jar
  • Index sample docs: cd example/exampledocs; java -jar post.jar *.xml
  • Hit the examples below...



Sample Usage


http://localhost:8983/solr/browse


  • Renders browse.vm from conf/velocity. Faceted navigation included.

http://localhost:8983/solr/browse?v.template.header=Custom%20Header


  • Renders browse.vm, but overrides the header.vm from conf/velocity with the specified value.

http://localhost:8983/solr/browse?debugQuery=true


  • Renders browse.vm, adding in explanation views per hit, and a Velocity context dump at the end.



Options (All Versions)




  • v.template: template name to use, without the .vm suffix. If not specified, "default"[.vm] will be used.

  • v.template.: overrides a file system template

  • debugQuery: if true, default view displays explanations for each hit and additional debugging information in the footer.

  • v.json: Escapes and wraps Velocity generated response with v.json parameter as a JavaScript function.

  • v.layout: Template name that wraps main template (v.template). Main template renders to a $content that can be used in layout template.

  • v.base_dir: overwrites default template load path (conf/velocity/).

  • v.properties: specifies a Velocity properties file to be applied, found using the Solr resource loader mechanism. If not specified, no .properties file is loaded. Example: v.properties=velocity.properties
    where velocity.properties can be found using Solr's resource loader mechanism, for example in the conf/ directory (not conf/velocity which is for templates only). The .properties file could also be located inside a JAR in the lib/ directory, or other locations.

  • v.contentType: sets the value of the HTTP response's Content-Type header (in case (x)html pages should be UTF-8 (instead of ISO-8859-1) encoded, make sure you set this option to text/xml;charset=UTF-8 (for
    XHTML) and text/html;charset=UTF-8 (for HTML), respectively)



Velocity Context




  • esc: a Velocity EscapeTool instance

  • date: a Velocity ComparisonDateTool instance

  • list: a Velocity ListTool instance

  • math: a Velocity MathTool instance

  • number: a Velocity NumberTool instance

  • page: a PageTool instance.
    page only is added to the context when response is a QueryResponse.

  • request: a SolrQueryRequest

  • response: a QueryResponse most
    of the time, but in some cases where QueryResponse doesn't like the request handlers
    output (AnalysisRequestHandler, for example, causes a ClassCastException parsing
    "response") the response will be a SolrResponseBase object.

  • sort: a Velocity SortTool instance



TODO



  • Ajax suggest
  • Integrate/adapt to SolrJS
  • Tie in SIMILE Timeline and SIMILE Exhibit
  • Add links in default footer to this wiki page, the Solr request as XML format, and SOLR-620
  • Fix multi-valued fields issue, and fl parameter usage.
  • Work on "dist" target so this works easily with a nightly build.
  • Make Velocity tools and engine configuration pluggable



  二、Velocity文件定位过程
  1、根据上述分析,首先定位layout.xml
  



#parse("head.vm")


Solr Admin

#parse("header.vm")


#parse("tabs.vm")


$content


#parse("footer.vm")




2、其中content的内容即为browse.vm  
  


#parse("pagination_top.vm")

## Show Error Message, if any

#parse("error.vm")

## Render Results, actual matching docs

#parse("results_list.vm")


#parse("pagination_bottom.vm")


3、browse.vm中最主要的搜索结果为results_list.vm  
  

#**
*  Render the main Results List
*#
## Usually displayed inside
#if($response.response.get('grouped'))
#foreach($grouping in $response.response.get('grouped'))
#parse("hit_grouped.vm")
#end
#else
#foreach($doc in $response.results)
#parse("hit.vm")
## Can get an extremely simple view of the doc
## which might be nicer for debugging
##parse("hit_plain.vm")
#end
#end

  
  一般情况下使用hit.vm作呈现,它对页面作了一些美工。
  在某些情况下,如debug的时候,就使用hit_plain.vm进行呈现,此时将所有的属性呈现出来。
  二者的对比效果如下:



  



4、先查看hit_plain.vm  

#**
*  An extremely plain / debug version of hit.vm
*#

## For each field
#foreach( $fieldName in $doc.fieldNames )
## For each value
#foreach( $value in $doc.getFieldValues($fieldName) )

## Field Name

#if( $foreach.count == 1 )
$fieldName:
#end

## Field Value(s)

$esc.html($value)


#end     ## end for each value
#end       ## end for each field

就是将属性名与属性值呈现出来。  
  


  5、再看看hit.vm
  

#**
*  Called for each matching document but then
*  calls one of product_doc, join_doc or richtext_doc
*  depending on which fields the doc has
*#
#set($docId = $doc.getFieldValue('id'))

## Has a "name" field ?
#if($doc.getFieldValue('name'))
#parse("product_doc.vm")
## Has a "compName_s" field ?
#elseif($doc.getFieldValue('compName_s'))
#parse("join_doc.vm")
## Fallback to richtext_doc
#else
#parse("richtext_doc.vm")
#end

  
  



6、可以直接看richtest_doc.vm

#**
*  Render a complex document in the results list
*#
## Load Mime-Type List and Mapping
#parse('mime_type_lists.vm')
## Sets:
## * supportedMimeTypes, AKA supportedtypes
## * mimeExtensionsMap, AKA extMap
## Title
#if($doc.getFieldValue('title'))
#set($title = $esc.html($doc.getFirstValue('title')))
#else
#set($title = "["+$doc.getFieldValue('id')+"]")
#end
## URL
#if($doc.getFieldValue('url'))
#set($url = $doc.getFieldValue('url'))
#elseif($doc.getFieldValue('resourcename'))
#set($url = "file:///$doc.getFieldValue('resourcename')")
#else
#set($url = "$doc.getFieldValue('id')")
#end
## Sort out Mime-Type
#set($ct = $list.get($doc.getFirstValue('content_type').split(";"),0))
#set($filename = $doc.getFieldValue('resourcename'))
#set($filetype = false)
#set($filetype = $mimeExtensionsMap.get($ct))
## TODO: falling back to file extension is convenient,
## except when you don't have an icon for that extension
## example "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
## document with a .docx extension.
## It'd be nice to fall back to an "unknown" or the existing "file" type
## We sort of do this below, but only if the filename has no extension
## (anything after the last dot).
#if(!$filetype)
#set($filetype = $filename.substring($filename.lastIndexOf(".")).substring(1))
#end
## #if(!$filetype)
##   #set($filetype = "file")
## #end
## #if(!$supportedMimeTypes.contains($filetype))
##   #set($filetype = "file")
## #end
## Row 1: Icon and Title and mlt link

## Icon
## Small file type icons from http://www.splitbrain.org/projects/file_icons (public domain)

## Title, hyperlinked

$title
## Link for MLT / More Like This / Find Similar

#if($params.getBool('mlt', false) == false)

More Like This
#end


## Row 2?: ID / URL

#Id: #field('id')
##自己修改
##Time: #field('tstamp')

## Resource Name

#if($doc.getFieldValue('resourcename'))
Resource name: $filename
#elseif($url)
URL: $url
#end
#if($ct)
($ct)
#end

## Author
#if($doc.getFieldValue('author'))

Author: #field('author')

#end
## Last_Modified Date
#if($doc.getFieldValue('last_modified'))

last-modified:
#field('last_modified')

#end
## Main content of doc

#field('content')

## Display Similar Documents / MLT = More Like This

#set($mlt = $mltResults.get($docId))
#set($mltOn = $params.getBool('mlt'))
#if($mltOn == true)

Similar Items

#end
## If has MLT enabled An Entries to show
#if ($mltOn && $mlt && $mlt.size() > 0)

#foreach($mltHit in $mlt)
#set($mltId = $mltHit.getFieldValue('id'))



$mltId



Title:

$mltHit.getFieldValue('title')



Author:

$mltHit.getFieldValue('author')

Description:

$mltHit.getFieldValue('description')


#end    ## end for each mltHit in $mlt

## Else MLT Enabled but no mlt results for this query
#elseif($mltOn && $mlt.size() == 0)
No Similar Items Found
#end
  ## div class=mlt
#parse('debug.vm')
由于本文件是example自带的呈现文件,其属性也按照自带的schemal.xml定义,并不适用于nutch的schema。  因此,若要改变呈现的内容,可以直接修改此文件。如将
  

## Row 2?: ID / URL

#Id: #field('id')
改为:  
  

## Row 2?: ID / URL

##自己修改
#Time: #field('tstamp')
则在页面不再显示id,而是显示时间。




  
  


  


  






  


  


  


  


  


  


  


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-88107-1-1.html 上篇帖子: 第一次接触solr的过程记录 下篇帖子: jquery 请求apache solr 跨域解决方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表