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

[经验分享] advanced act_as_solr

[复制链接]

尚未签到

发表于 2016-12-15 06:54:08 | 显示全部楼层 |阅读模式
  原文出处: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
    Currenlty acts_as_solr does not implements these options at each column level, so you cannot ask to apply some of the options to only one (or few) columns. For e.g. you cannot ask acts_as_solr to use <em> tags for one field and <span class='highlight'> for other field.
  

  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 to vendor/plugins/acts_as_solr/solr/solr/conf/xslt/ and then open url: http://localhost:8982/solr/admin/luke?wt=xslt&tr=luke.xsl

运维网声明 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-314310-1-1.html 上篇帖子: 在 Tomcat 中配置单实例 Solr 下篇帖子: solr学习(一)术语翻译
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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