【Solr基础教程之X】关于VelocityResponseWriter

在Solr中,可以以多种方式返回搜索结果,如单纯的文本回复(XML、JSON、CSV等),也可以返回velocity,js等格式。而VelocityResponseWriter就是用于将返回velocity类型文本,以便直接用于结果呈现。

在Solr提供的example,其中的一个RequestHandler--/browse,使用了VelocityResponseWriter。其配置如下:

  <requestHandler name="/browse" class="solr.SearchHandler">
     <lst name="defaults">
       <str name="echoParams">explicit</str>

       <!-- VelocityResponseWriter settings -->
       <str name="wt">velocity</str>
       <str name="v.template">browse</str>
       <str name="v.layout">layout</str>
       <str name="title">Solritas_test</str>

       <!-- Query settings -->
       <str name="defType">edismax</str>
       <str name="qf">
          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
       </str>
       <str name="df">text</str>
       <str name="mm">100%</str>
       <str name="q.alt">*:*</str>
       <str name="rows">10</str>
       <str name="fl">*,score</str>

       <str name="mlt.qf">
         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
       </str>
       <str name="mlt.fl">text,features,name,sku,id,manu,cat,title,description,keywords,author,resourcename</str>
       <int name="mlt.count">3</int>

       <!-- Faceting defaults -->
       <str name="facet">on</str>
       <str name="facet.field">cat</str>
       <str name="facet.field">manu_exact</str>
       <str name="facet.field">content_type</str>
       <str name="facet.field">author_s</str>
       <str name="facet.query">ipod</str>
       <str name="facet.query">GB</str>
       <str name="facet.mincount">1</str>
       <str name="facet.pivot">cat,inStock</str>
       <str name="facet.range.other">after</str>
       <str name="facet.range">price</str>
       <int name="f.price.facet.range.start">0</int>
       <int name="f.price.facet.range.end">600</int>
       <int name="f.price.facet.range.gap">50</int>
       <str name="facet.range">popularity</str>
       <int name="f.popularity.facet.range.start">0</int>
       <int name="f.popularity.facet.range.end">10</int>
       <int name="f.popularity.facet.range.gap">3</int>
       <str name="facet.range">manufacturedate_dt</str>
       <str name="f.manufacturedate_dt.facet.range.start">NOW/YEAR-10YEARS</str>
       <str name="f.manufacturedate_dt.facet.range.end">NOW</str>
       <str name="f.manufacturedate_dt.facet.range.gap">+1YEAR</str>
       <str name="f.manufacturedate_dt.facet.range.other">before</str>
       <str name="f.manufacturedate_dt.facet.range.other">after</str>

       <!-- Highlighting defaults -->
       <str name="hl">on</str>
       <str name="hl.fl">content features title name</str>
       <str name="hl.encoder">html</str>
       <str name="hl.simple.pre"><b></str>
       <str name="hl.simple.post"></b></str>
       <str name="f.title.hl.fragsize">0</str>
       <str name="f.title.hl.alternateField">title</str>
       <str name="f.name.hl.fragsize">0</str>
       <str name="f.name.hl.alternateField">name</str>
       <str name="f.content.hl.snippets">3</str>
       <str name="f.content.hl.fragsize">200</str>
       <str name="f.content.hl.alternateField">content</str>
       <str name="f.content.hl.maxAlternateFieldLength">750</str>

       <!-- Spell checking defaults -->
       <str name="spellcheck">on</str>
       <str name="spellcheck.extendedResults">false</str>
       <str name="spellcheck.count">5</str>
       <str name="spellcheck.alternativeTermCount">2</str>
       <str name="spellcheck.maxResultsForSuggest">5</str>
       <str name="spellcheck.collate">true</str>
       <str name="spellcheck.collateExtendedResults">true</str>
       <str name="spellcheck.maxCollationTries">5</str>
       <str name="spellcheck.maxCollations">3</str>
     </lst>

     <!-- append spellchecking to our list of components -->
     <arr name="last-components">
       <str>spellcheck</str>
     </arr>
  </requestHandler>

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

  <!--
     Custom response writers can be declared as needed...
    -->
    <queryResponseWriter name="velocity" class="solr.VelocityResponseWriter" startup="lazy"/>

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

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

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

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

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

  <requestHandler name="/browse" class="solr.SearchHandler">
     <lst name="defaults">
       <str name="echoParams">explicit</str>
       <!-- VelocityResponseWriter settings -->
       <str name="wt">velocity</str>
       <str name="v.template">browse</str>
       <str name="v.layout">layout</str>
       <str name="title">Solritas_test</str>

从上述定义中开始分别查找显示层的内容(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
 *#

<html>
<head>
  #parse("head.vm")
</head>
  <body>
    <div id="admin"><a href="#url_root/#/#core_name">Solr Admin</a></div>
    <div id="header">
      #parse("header.vm")
    </div>
    <div id="tabs">
      #parse("tabs.vm")
    </div>
    <div id="content">
      $content
    </div>
    <div id="footer">
      #parse("footer.vm")
    </div>
  </body>
</html>

4、定位ResponseWriter

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

<queryResponseWriter name="velocity" class="solr.VelocityResponseWriter" startup="lazy"/>

即VelocityResponseWriter实现类,其定义如下:

public class VelocityResponseWriter
extends Object
implements QueryResponseWriter

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

以下是关于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

  1. Introduction
  2. Instructions to use, Solr 1.4+
    1. Sample Usage

      1. Using the VelocityResponseWriter
        in Solr Core
  3. Instructions to use, Solr 4.0+
    1. Sample Usage
  4. Options (All Versions)
  5. Velocity Context
  6. 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:

    <queryResponseWriter name="velocity" class="org.apache.solr.request.VelocityResponseWriter"/>

Set up a RequestHandler in
solrconfig.xml:

  <requestHandler name="/itas" class="solr.SearchHandler">
     <lst name="defaults">
       <str name="v.template">browse</str>
       <str name="v.properties">velocity.properties</str>
       <str name="v.contentType">text/html;charset=UTF-8</str>
       <str name="title">Solritas</str>

       <str name="wt">velocity</str>
       <str name="defType">dismax</str>
       <str name="q.alt">*:*</str>
       <str name="rows">10</str>
       <str name="fl">*,score</str>
       <str name="facet">on</str>
       <str name="facet.field">title</str>
       <str name="facet.mincount">1</str>
       <str name="qf">
          text^0.5 title^1.5
       </str>
     </lst>
     <!--<lst name="invariants">-->
       <!--<str name="v.base_dir">/solr/contrib/velocity/src/main/templates</str>-->
     <!--</lst>-->
  </requestHandler>

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:

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.<name>: 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

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
时间: 2024-10-11 11:48:11

【Solr基础教程之X】关于VelocityResponseWriter的相关文章

【Solr基础教程之X】配置文件:Solr.xml SolrConfig.xml Schema.xml

1.关于默认搜索域 If you are using the Lucene query parser, queries that don't specify a field name will use the defaultSearchField. The DisMax and Extended DisMax query parsers do not use this value. Use of the defaultSearchField element is deprecated in So

Solr基础教程之solrconfig.xml(三)

前面介绍过schema.xml的一些配置信息,本章介绍solrconfig.xml的配置,以及怎样安装smartcn分词器和IK分词器,并介绍主要的查询语法. 1. solr配置solrconfig.xml solrconfig.xml这个配置文件能够在你下载solr包的安装解压文件夹的D:\solr-4.10.4\example\solr\collection1\conf中找到,这个配置文件内容有点多,主要内容有:使用的lib配置,包括依赖的jar和Solr的一些插件;组件信息配置;索引配置和

Python基础教程之List对象 转

Python基础教程之List对象 时间:2014-01-19    来源:服务器之家    投稿:root 1.PyListObject对象typedef struct {    PyObject_VAR_HEAD    PyObject **ob_item;    Py_ssize_t allocated;} PyListObject; PyObject_VAR_HEAD中的obsize表示该list对象含有的元素个数, 而allocated表示该list对象占用的内存空间. ob_item

Linux入门基础教程之Linux下软件安装

Linux入门基础教程之Linux下软件安装 一.在线安装: sudo apt-get install 即可安装 如果在安装完后无法用Tab键补全命令,可以执行: source ~/.zshrc APT(Advanced Packaging Tool), 包括apt-get, apt-cache, apt-cdrom等工具,APT可以自动下载,配置,安装二进制或者源代码格式的软件包,因此简化了Unix系统上管理软件的过程,Ubuntu是Debian的发行版.Debian使用的包管理工具是dpkg

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务

OpenVAS漏洞扫描基础教程之OpenVAS概述及安装及配置OpenVAS服务 OpenVAS基础知识 OpenVAS(Open Vulnerability Assessment System)是开放式漏洞评估系统,其核心部分是一个服务器.该服务器包括一套网络漏洞测试程序,可以检测远程系统和应用程序中的安全问题.OpenVAS不同与传统的漏洞扫描软件.所有的OpenVAS软件都是免费的,而且还采用了Nessus(一款强大的网络扫描工具)较早版本的一些开放插件.虽然Nessus很强大,但是该工具

Python核心编程基础教程之Python运算符、运算符优先级、表达式简介--20150717

Python核心编程基础教程之Python运算符.运算符优先级.表达式简介 1.Python运算符与表达式: (1)认识Pyhton运算符1:什么是运算符 在Python运算中,有时候我们需要对一个或者多个数字或者一个或者多个字符串进行运算操作,*,+ (2)认识Pyhton运算符2:运算符有哪些以及运算符的使用方法 + :加 - :减 * :乘 / :除 ** :幂 < :小于 > :大于 != :不等于 // :求相除的整数部分 % :求相除的余数部分 & :按位与 | :按位或

基础教程之Running Man奔跑

曳步舞是以奔跑为基础的舞蹈,各位小伙伴在学习曳舞的过程中,一定要注重基础哦!只有奔跑灵活稳重,才会身轻如燕.各种花式都是奔跑的变形,例如侧拉.侧踢等等.他们都是在奔跑的某个动作之上,稍加修改,或转体.或打直.突出了某一个动作. 舞蹈精髓 用"写字'来打个比方,点.横.撇.捺.勾等是汉字的骨架,任何字都离不开他们.那么在曳舞里,奔跑.V字步.侧拉.侧滑等就是骨架了.我们在早期写字时,老师要求我们写字要一笔一画,端正整洁.那么,我们的曳舞在学习之初,也应该把动作做到位,90度的要求必须做到,习以为常

Python基础教程之第2章 列表和元组

D:\>python Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. #2.1序列概览 >>> edward=['Edward Gumby', 4

ruby基础教程之rails性能优化

在ruby on rails(http://www.maiziedu.com/course/ruby-px/)中,很多rails程序员会经常忘记考虑性能问题,这会导致网站速度下降,内存占用剧增,Apache频频重启,所以这时就需要进行rails性能优化.具体的优化内容如下: 1,使用Unicorn或者Thin服务器替代默认的webrick. 2,静态资源压缩合并,放到云存储上. 3,同时可以使用rails的Turbolinks,使用js替换title和body,但也带来了js逻辑的变化. 4,A