给Compass搜索添加高亮(highlight)

http://jdkcn.com/entry/howto-add-highlight-in-compass.html
在myblog 1.5 P1版中我加入了Compass搜索的高亮支持。我参考了springside的代码实现了高亮,其实也是很简单的。在原来的searchController里面用的是默认的CompassSearchHelper来搜索的。
        if (searchHelper == null) {
            searchHelper = new CompassSearchHelper(getCompass(), getPageSize());
        }
        CompassSearchCommand searchCommand = new CompassSearchCommand();
        searchCommand.setPage(new Integer(currentPage-1));
        searchCommand.setQuery(query);
        ModelAndView mv = new ModelAndView();
        mv.addObject("query", query);
        mv.addObject("p", currentPage);
        List<String> errors = new ArrayList<String>();
        try{
            CompassSearchResults searchResults = searchHelper.search(searchCommand);
            mv.addObject(getSearchResultsName(), searchResults);
        }catch (SearchEngineQueryParseException ex){
            errors.add(TextUtil.escapeHTML(ex.getMessage()));
            mv.addObject("errors", errors);
        }

本来打算按照springside里面的封装方法。http://svn.javascud.org/svn/springside/springside2/trunk/core/src/java/org/springside/components/compass/
这里有两个类AdvancedSearchCommand.java,CompassSearchService.java,CompassSearchService里面实现了高亮和排序,后来发现CompassSearchService和CompassSearchHelper长得很像,而且CompassSearchHelper也提供了两个抽象的方法给用户提供一个添加功能的机会。

    /**
     * An option to perform any type of processing before the hits are detached.
     */
    protected void doProcessBeforeDetach(CompassSearchCommand searchCommand, CompassSession session, CompassHits hits,
                                         int from, int size) {

    }

    /**
     * An option to perform any type of processing after the hits are detached.
     */
    protected void doProcessAfterDetach(CompassSearchCommand searchCommand, CompassSession session,
                                        CompassDetachedHits hits) {

    }

所以我们只要实现一个自己的CompassSearchHelper覆盖这两个方法就可以达到实现高亮的目的了。
/**
* @author <a href="mailto:[email protected]">somebody</a>
* @since Aug 23, 2007 2:04:19 PM
* @version $Id AdvanceCompassSearchHelper.java$
*/
public class AdvanceCompassSearchHelper extends CompassSearchHelper {

    private String[] highlightFields;

    public String[] getHighlightFields() {
        return highlightFields;
    }

    public void setHighlightFields(String[] highlightFields) {
        this.highlightFields = highlightFields;
    }

    /**
     * @param compass
     */
    public AdvanceCompassSearchHelper(Compass compass) {
        super(compass);
    }
   

    /* (non-Javadoc)
     * @see org.compass.core.support.search.CompassSearchHelper#doProcessBeforeDetach(org.compass.core.support.search.CompassSearchCommand, org.compass.core.CompassSession, org.compass.core.CompassHits, int, int)
     */
    @Override
    protected void doProcessBeforeDetach(CompassSearchCommand searchCommand,
            CompassSession session, CompassHits hits, int from, int size) {
        if (from < 0) {
            from = 0;
            size = hits.getLength();
        }

        if (highlightFields == null) {
            return;
        }
        // highlight fields
        for (int i = from; i < size; i++) {
            for (String highlightField : highlightFields) {
                hits.highlighter(i).fragment(highlightField);
            }
        }
    }

ok,这样就可以实现高亮了。修改一下SearchController的配置。
    <bean id="advanceCompassSearchHelper" class="com.jdkcn.compass.AdvanceCompassSearchHelper">
        <property name="highlightFields">
            <list>
                <value>content</value>
            </list>
        </property>
        <property name="pageSize"><value>10</value></property>
        <constructor-arg ref="compass"/>
    </bean>
    
    <bean id="searchController" class="com.jdkcn.web.SearchController" parent="baseController">
        <property name="compass"><ref bean="compass"/></property>
        <property name="searchView"><value>search</value></property>
        <property name="searchResultsView"><value>search</value></property>
        <property name="pageSize"><value>10</value></property>
        <property name="searchHelper"><ref local="advanceCompassSearchHelper"/></property>
    </bean>

还不要忘了修改一下compass settings 修改一下高亮的样式,修改一下高亮的颜色。
    <bean id="compass" class="org.compass.spring.LocalCompassBean">
        <property name="resourceDirectoryLocations">
            <list>
                <value>classpath:com/jdkcn/compass</value>
            </list>
        </property>
        <property name="connection">
            <value>/lucene/indexes</value>
        </property>
        <property name="compassSettings">
            <props>
                <prop key="compass.transaction.factory">
                    org.compass.spring.transaction.SpringSyncTransactionFactory
                </prop>
                <prop key="compass.engine.highlighter.default.formatter.simple.pre">
                    <![CDATA[<font color="red"><b>]]>
                </prop>
                <prop key="compass.engine.highlighter.default.formatter.simple.post">
                    <![CDATA[</b></font>]]>
                </prop>
            </props>
        </property>
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
    </bean>

ok了,页面上输出一下就能看到最终效果了
${hit.highlightedText['content']?if_exists}

最终效果展示请看

你可能感兴趣的:(spring,html,SVN,Lucene)