- 浏览: 244401 次
文章分类
最新评论
-
bluky999:
中间的兼职例子很逗 哈哈哈
tornado: web.py 之 Application -
flingfox63:
学习了,详细,赞个
Ruby变量作用域的类目录结构 -
zhou6711411:
不知是版本问题还是怎么的
class A
...
Ruby变量作用域的类目录结构 -
t284299773:
你在方法中定义方法就相当于在方法中调用lambda!
Ruby变量作用域的类目录结构(补二) -
lnj888:
很是有用 不错Powerpoint converter
一个简单的link_to,ROR到底在背后做了些什么?(未完)
原文出处:http://www.quarkruby.com/2007/9/14/advanced-acts_as_solr
This article extends our acts_as_solr : search and faceting tutorial and talks about how to manage rails associations, solr indexes and more with acts_as_solr.
Table Of Contents
- Rebuild Solr index
- Import existing Solr index or your custom Solr schema.xml
- Highlight search term
- Rails associations and acts_as_solr
- Tips
Rebuild Solr Index
rebuild_solr_index
is a class method to re-build your model indexes on import of external data.
For large tables rebuilding Solr index is a time consuming process. See the fifth line in the pseudo code below (index optimization call), it makes rebuild_solr_index a slow process. For large tables, you do not want optimization to take place for each object added to the table. Whereas, removing optimization calls slows down the process of updating solr index.
1 2 3 4 5 6 7 |
## pseudo code def rebuild_solr_index for_each_row_in_table do |doc| doc.save_to_solr_index index.optimize end end |
The solution to the problem is to use batch_size
in #rebuild_solr_index
. With batch size, say for example 100, the index optimization call is executed after indexing 100 rows.
If you do not want to index the complete data but only few rows based on conditions, use optional arguments batch_size
and finder
in rebuild_solr_index.
- batch_size : to optimize index after adding batch_size items. Default value is 1.
- finder : it is used for conditional indexation. It takes a method as argument which returns objects to be indexed. Default method is :
1 2 3
def finder(ar, options) ar.find (:all, options.merge({:order => self.primary_key})) end
Import existing Solr index or your custom Solr schema.xml
Assumption: Column names in table definition are same as the names of the fields using which index has been/will be created.
- Add your indexed fields to
vendor/plugins/acts_as_solr/solr/solr/conf/schema.xml
(continuing with example of our previous tutorial, we add name1, brand, resolution to schema.xml).
Remember, to copy them to text field using solr's copyfield directive. text is the default field/column in which search is made, so using copyField we add all data to default search field. The new schema.xml looks like...1 2 3 4 5 6 7 8
<field name="text" type="text" indexed="true" stored="true" multiValued="true"/> <field name="name1" type="text" indexed="true" stored="false" /> <field name="brand" type="string" indexed="true" stored="false" /> <field name="resolution" type="sfloat" indexed="true" stored="false" /> <copyField source="*_facet" dest="text"/> <copyField source="brand" dest="text"/> <copyField source="name1" dest="text"/>
- Apply our patch (download). For indexing and searching, acts_as_solr suffixes dataType (e.g. int, float, string etc) of column to the column name, this patch tells acts_as_solr not to do this since we have explicitly created entries for these columns in schema file:
1 2
cd /path/to/rails/dir patch -p0 custom-schema.patch
- Modify your acts_as_solr definition:
1 2 3
class Camera acts_as_solr :custom_schema=>true, :fields=>[:name,:brand,:resolution] end
- ... and it works!
Highlight search terms
Goal is to highlight search term(s) in search results. Follow the steps below :
- Modify solrConfig.xml to enable highlighting. Apply our patch (download) as:
1 2
cd /path/to/rails/dir patch -p0 highlight-solrconfig.patch
- acts_as_solr does not stores any of the field values as it is in its index. We need to modify the configuration file
vendor/plugins/acts_as_solr/solr/solr/conf/schema.xml
to enable storage of data, which is required to return the highlighted text data. For example, we modify configuration file to store the fields having text dataType to get highlighted matching terms for name1 field.1 2 3 4
<!-- Original config --> <dynamicField name="*_t" type="text" indexed="true" stored="false"/> <!-- New config --> <dynamicField name="*_t" type="text" indexed="true" stored="true"/>
-
Apply one more patch in the same way as the previous one. This patch enables acts_as_solr to handle the option of highlighting in find_by_solr queries and parses the results returned by solr. And now we are ready to roar...
- Lets see it in action with an example
1 2 3 4 5 6 7 8 9
@results = Camera.find_by_solr("canon powershot", :highlight=>{:fields=>"name1"}) @results.highlights ## returns hash of objects id and hash of the columns with matched text data {1=>{:name1=>"<em>Canon</em> <em>Powershot</em> sd1000"} ... } #You want to display names with highlighted terms: @results.docs.each do |doc| <%= @results.highlights[doc.id][:name1] -%><br/> end
- Options for highlighting (You can also refer them here)
fields
: columns to highlight terms for (default is none, and no highlighted text)prefix
: You do not want to have "<em>" tag around matched search term(s), tell your tag here. (eg. "<span class='highlight'>")suffix
: Ending tag for matched term(s)max_snippets
: stop searching the column after max_snippets matched terms found
Note: If you want both the functionalities Custom schema and Highlighting together, use these two patches. Patch 1 and Patch 2.
Rails associations and acts_as_solr
Lets say I have two models, User (columns: username, password) and UserProfile (columns: full name, location, favorite food .... and many more). These two models have one-to-one association (:has_one, :belongs_to) between them and I have two problems :
- When searching the User model, I also want to search UserProfile model.
- I want to allow faceting based on few columns in UserProfile lets say location and favorite food.
Fabio Confalonieri has written a small hack for the solution to both the problems along with good explanation.
Tips:
-
Wanna see the statistics for created index?
Download -
If your search results are not good enough because the text is not indexed properly, it might be due to presence of acronyms or words having apostrophes or html content in your data. Solr provides you with many options for analyzing and modifying text before it is indexed and searched. Read these options at Solr's Wiki page. Remember, you need to make these modifications in schema.xml file.
-
Note : The default behaviour of acts_as_solr is to index model objects automatically upon save or update of a record.
<!---->
this xsl file as luke.xsl tovendor/plugins/acts_as_solr/solr/solr/conf/xslt/
and then open url: http://localhost:8982/solr/admin/luke?wt=xslt&tr=luke.xsl
发表评论
-
(ZZ)Ror on svn
2007-12-20 19:34 1513正好需要,zz过来,抄袭自:http://www.surui. ... -
用GetText来进行ROR的国际化和本地化
2007-11-22 15:17 1444IBM developerWorks上的一篇文章,直接贴地址, ... -
act_as_solr
2007-10-31 19:39 1986原文出处:http://www.quarkruby.com/2 ... -
Ambition
2007-10-31 19:36 1365原文出处:http://railsontherun.com/2 ... -
使用Inkscape提供自己的pdf服务
2007-10-31 19:34 1551原文出处:http://www.thesatya.com/bl ... -
给will_paginate加上ajax效果
2007-10-31 19:30 2156原文出处:http://railsontherun.com/2 ... -
使用rails制作图表
2007-10-31 19:21 2815原文出处:http://www.railsontherun.c ... -
如果定制attachment_fu上传文件的路径和文件名
2007-10-31 16:59 2747原文出处:http://the.railsi.st/2007/ ... -
attachment_fu使用指南
2007-10-31 16:56 3204原文出处:http://clarkware.com/cgi/b ... -
(ZZ)Cache in Rails
2007-09-25 15:49 1515很经典的文章,留在blog里面做个收藏 Ruby on Rai ... -
(ZZ)Ruby on Rails Security Guide
2007-09-24 21:28 2680Ruby on Rails Security Gui ... -
学到三招
2007-09-24 01:54 1395第一招:用ruby-debug来调试rails程序 具体使用方 ... -
一个action的process过程
2007-09-17 00:11 2551ruby 代码 def process(req ... -
在线查看rails代码和edge rails api的网址,备份,以免忘记
2007-09-14 18:38 1332Edge Rails API: http://caboo.se ... -
总是看到returning,这到底是个什么东东,查了一下找到了源代码
2007-09-14 18:37 1387A Ruby-ized realization of the ... -
一个简单的link_to,ROR到底在背后做了些什么?(未完)
2007-09-14 18:20 3461滥用link_to会造成ror程序 ... -
学到关于include的一点儿知识
2007-08-23 18:09 1165ruby 代码 module Test ... -
在一个controller中render另外一个controller中view的时候出现问题
2007-08-21 18:27 2153我想在posts这个controller中的show.rh ... -
因为Rjs试用NetBeans
2007-06-20 09:44 1128因为昨天看Rails Recipe的时候提到了rjs,于是四处 ...
相关推荐
Solr8.4.0 是 Apache Solr 的一个版本,这是一个高度可配置、高性能的全文搜索和分析引擎,广泛用于构建企业级搜索应用。 在 Solr 中,ikanalyzer 是一个重要的组件,它通过自定义Analyzer来实现中文的分词处理。...
总之,"solr-config_solrj-demo.rar_DEMO_solr_solr的j"这个DEMO是一个全面了解和实践Solr配置及SolrJ使用的宝贵资源,它将引导你逐步掌握如何在实际项目中有效地运用Solr进行全文检索和数据分析。通过深入学习和...
CREATE EXTERNAL TABLE solr_table (field1 STRING, field2 INT) STORED BY 'org.apache.hive.storage.solr.SolrStorageHandler' TBLPROPERTIES ( 'solr.zookeeper'='zk_host:zk_port/solr', 'solr.collection'='...
Solr服务器是Apache Lucene项目的一个子项目,是一款开源的企业级搜索平台,专门用于处理大量文本数据的全文检索、搜索和分析。它基于Java开发,能够处理多种数据源,包括XML、JSON、CSV等,提供了高效、可扩展的...
**Solr:开源企业搜索引擎** Apache Solr,作为一款强大的开源全文搜索引擎,广泛应用于各种企业和组织,用于构建高效、可扩展的搜索解决方案。本教程旨在深入解析Solr的核心概念、功能以及实际应用,帮助读者掌握...
Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服务器。同时对其进行了扩展,提供了比Lucene更为丰富的查询语言,同时实现了可配置、可扩展并对查询性能进行了优化,并且提供了一个完善的功能管理界面,是一...
**Elasticsearch 与 Solr 比较详解** 在大数据和搜索引擎领域,Elasticsearch (ES) 和 Apache Solr 都是广泛使用的开源技术,它们都基于 Lucene 库,提供高性能、可扩展的全文搜索和分析能力。然而,两者在设计哲学...
windows环境php5.5的php_solr.dll
《ANSJ与Solr的深度整合:构建高效全文搜索引擎》 在信息爆炸的时代,如何高效地从海量数据中检索出我们需要的信息,成为了企业和个人都亟待解决的问题。这就是全文搜索引擎的作用所在。ANSJ(Automatic NLP ...
Apache Solr 是一个开源的企业级搜索平台,它允许快速、可扩展的全文检索,以及丰富的搜索功能和结果排序。在 PHP 中集成 Solr 扩展,可以极大地提高开发效率,使开发者能够轻松地在网站中实现高级搜索功能。 标题...
在搭建Solr环境时,`solr_Tomcat_lib`包扮演了关键角色,因为它是Solr在Tomcat容器中运行所需的库文件集合。 首先,让我们详细了解`lib`目录。这个目录通常包含Solr运行所必需的各种JAR文件,这些文件主要分为以下...
3. **app/models/solr_document.rb**:定义了Solr文档对象,用于映射Rails模型到Solr索引。 4. **config目录**:配置文件,如solr.yml,用于配置Solr服务器的连接信息。 5. **initializers/solr.rb**:初始化脚本,...
script/plugin install git://github.com/brauliobo/acts_as_solr_reloaded.git 下载Solr rake solr:download 要求 Oracle或OpenJDK的Java Runtime Environment(JRE)6.0(或更高版本) 配置 请参阅config
Apache Solr 是一个基于 Apache Lucene 的开源搜索引擎,为各种应用提供了搜索功能。它具有高效、可靠和强大的特点,广泛应用于企业级搜索、互联网搜索和数据分析。Solr 提供了包括全文搜索、高亮显示、自动拼写更正...
### Apache Solr 初级教程知识点总结 #### 一、Apache Solr 概述 - **全文检索技术的重要性**:随着互联网的发展,信息量日益膨胀,如何高效地从海量信息中提取有价值的内容变得至关重要。全文检索技术作为一种...
Apache Solr 是一个开源的企业级搜索平台,由Apache软件基金会开发。它基于Java,并且是Lucene库的一个高级封装,提供了更加便捷和可扩展的全文检索、数据分析和搜索功能。在1.4.0版本中,Solr已经相当成熟,为用户...
Lucene Solr 搜索引擎解密 ppt
在每个Solr节点上,我们需要配置solr.in.sh(在Unix/Linux系统中)或solr.in.cmd(在Windows系统中),设置`SOLR_HOME`、`JAVA_OPTS`等环境变量,并指定ZooKeeper集群的位置。 3. **启动SolrCloud**:启动每个Solr...
本文将基于"solr_开发入门例子"这一主题,详细解释Solr的基础知识,包括其核心概念、安装配置、索引创建与查询,以及相关的开发工具。 1. **Solr核心概念** - **索引**: Solr通过建立倒排索引来实现快速全文检索。...
Solr 是一个开源的全文搜索引擎,它提供了高效且可扩展的搜索和索引能力。在3.5版本中,Solr 已经成为一个成熟的技术,广泛应用于网站的全文检索、商品搜索、文档检索等多个场景。本文将深入探讨 Solr 3.5 的配置...