参考了springside的代码实现了高亮,其实也是很简单的。在原来的searchController里面用的是默认的CompassSearchHelper来搜索的。
http://svn.javascud.org/svn/springside/springside2/trunk/core/src/java/org/springside/components/compass/
这里有两个类AdvancedSearchCommand.java,CompassSearchService.java,CompassSearchService里面实现了高亮和排序,后来发现CompassSearchService和CompassSearchHelper长得很像,而且CompassSearchHelper也提供了两个抽象的方法给用户提供一个添加功能的机会。
1
if
(searchHelper
==
null
) {
2 searchHelper = new CompassSearchHelper(getCompass(), getPageSize());
3 }
4 CompassSearchCommand searchCommand = new CompassSearchCommand();
5 searchCommand.setPage( new Integer(currentPage - 1 ));
6 searchCommand.setQuery(query);
7 ModelAndView mv = new ModelAndView();
8 mv.addObject( " query " , query);
9 mv.addObject( " p " , currentPage);
10 List < String > errors = new ArrayList < String > ();
11 try {
12 CompassSearchResults searchResults = searchHelper.search(searchCommand);
13 mv.addObject(getSearchResultsName(), searchResults);
14 } catch (SearchEngineQueryParseException ex){
15 errors.add(TextUtil.escapeHTML(ex.getMessage()));
16 mv.addObject( " errors " , errors);
17 }
18
本来打算按照springside里面的封装方法。
2 searchHelper = new CompassSearchHelper(getCompass(), getPageSize());
3 }
4 CompassSearchCommand searchCommand = new CompassSearchCommand();
5 searchCommand.setPage( new Integer(currentPage - 1 ));
6 searchCommand.setQuery(query);
7 ModelAndView mv = new ModelAndView();
8 mv.addObject( " query " , query);
9 mv.addObject( " p " , currentPage);
10 List < String > errors = new ArrayList < String > ();
11 try {
12 CompassSearchResults searchResults = searchHelper.search(searchCommand);
13 mv.addObject(getSearchResultsName(), searchResults);
14 } catch (SearchEngineQueryParseException ex){
15 errors.add(TextUtil.escapeHTML(ex.getMessage()));
16 mv.addObject( " errors " , errors);
17 }
18
http://svn.javascud.org/svn/springside/springside2/trunk/core/src/java/org/springside/components/compass/
这里有两个类AdvancedSearchCommand.java,CompassSearchService.java,CompassSearchService里面实现了高亮和排序,后来发现CompassSearchService和CompassSearchHelper长得很像,而且CompassSearchHelper也提供了两个抽象的方法给用户提供一个添加功能的机会。
1
/**
2 * An option to perform any type of processing before the hits are detached.
3 */
4 protected void doProcessBeforeDetach(CompassSearchCommand searchCommand, CompassSession session, CompassHits hits,
5 int from, int size) {
6
7 }
8
9 /**
10 * An option to perform any type of processing after the hits are detached.
11 */
12 protected void doProcessAfterDetach(CompassSearchCommand searchCommand, CompassSession session,
13 CompassDetachedHits hits) {
14
15 }
16
所以我们只要实现一个自己的CompassSearchHelper覆盖这两个方法就可以达到实现高亮的目的了。
2 * An option to perform any type of processing before the hits are detached.
3 */
4 protected void doProcessBeforeDetach(CompassSearchCommand searchCommand, CompassSession session, CompassHits hits,
5 int from, int size) {
6
7 }
8
9 /**
10 * An option to perform any type of processing after the hits are detached.
11 */
12 protected void doProcessAfterDetach(CompassSearchCommand searchCommand, CompassSession session,
13 CompassDetachedHits hits) {
14
15 }
16
1
/**
2 * @author <a href="mailto:rory.cn@gmail.com">somebody</a>
3 * @since Aug 23, 2007 2:04:19 PM
4 * @version $Id AdvanceCompassSearchHelper.java$
5 */
6 public class AdvanceCompassSearchHelper extends CompassSearchHelper {
7
8 private String[] highlightFields;
9
10 public String[] getHighlightFields() {
11 return highlightFields;
12 }
13
14 public void setHighlightFields(String[] highlightFields) {
15 this .highlightFields = highlightFields;
16 }
17
18 /**
19 * @param compass
20 */
21 public AdvanceCompassSearchHelper(Compass compass) {
22 super (compass);
23 }
24
25
26 /* (non-Javadoc)
27 * @see org.compass.core.support.search.CompassSearchHelper#doProcessBeforeDetach(org.compass.core.support.search.CompassSearchCommand, org.compass.core.CompassSession, org.compass.core.CompassHits, int, int)
28 */
29 @Override
30 protected void doProcessBeforeDetach(CompassSearchCommand searchCommand,
31 CompassSession session, CompassHits hits, int from, int size) {
32 if (from < 0 ) {
33 from = 0 ;
34 size = hits.getLength();
35 }
36
37 if (highlightFields == null ) {
38 return ;
39 }
40 // highlight fields
41 for ( int i = from; i < size; i ++ ) {
42 for (String highlightField : highlightFields) {
43 hits.highlighter(i).fragment(highlightField);
44 }
45 }
46 }
47
ok,这样就可以实现高亮了。修改一下SearchController的配置。
2 * @author <a href="mailto:rory.cn@gmail.com">somebody</a>
3 * @since Aug 23, 2007 2:04:19 PM
4 * @version $Id AdvanceCompassSearchHelper.java$
5 */
6 public class AdvanceCompassSearchHelper extends CompassSearchHelper {
7
8 private String[] highlightFields;
9
10 public String[] getHighlightFields() {
11 return highlightFields;
12 }
13
14 public void setHighlightFields(String[] highlightFields) {
15 this .highlightFields = highlightFields;
16 }
17
18 /**
19 * @param compass
20 */
21 public AdvanceCompassSearchHelper(Compass compass) {
22 super (compass);
23 }
24
25
26 /* (non-Javadoc)
27 * @see org.compass.core.support.search.CompassSearchHelper#doProcessBeforeDetach(org.compass.core.support.search.CompassSearchCommand, org.compass.core.CompassSession, org.compass.core.CompassHits, int, int)
28 */
29 @Override
30 protected void doProcessBeforeDetach(CompassSearchCommand searchCommand,
31 CompassSession session, CompassHits hits, int from, int size) {
32 if (from < 0 ) {
33 from = 0 ;
34 size = hits.getLength();
35 }
36
37 if (highlightFields == null ) {
38 return ;
39 }
40 // highlight fields
41 for ( int i = from; i < size; i ++ ) {
42 for (String highlightField : highlightFields) {
43 hits.highlighter(i).fragment(highlightField);
44 }
45 }
46 }
47
1
<
bean
id
="advanceCompassSearchHelper"
class
="com.jdkcn.compass.AdvanceCompassSearchHelper"
>
2 < property name ="highlightFields" >
3 < list >
4 < value > content </ value >
5 </ list >
6 </ property >
7 < property name ="pageSize" >< value > 10 </ value ></ property >
8 < constructor-arg ref ="compass" />
9 </ bean >
10
11 < bean id ="searchController" class ="com.jdkcn.web.SearchController" parent ="baseController" >
12 < property name ="compass" >< ref bean ="compass" /></ property >
13 < property name ="searchView" >< value > search </ value ></ property >
14 < property name ="searchResultsView" >< value > search </ value ></ property >
15 < property name ="pageSize" >< value > 10 </ value ></ property >
16 < property name ="searchHelper" >< ref local ="advanceCompassSearchHelper" /></ property >
17 </ bean >
18
还不要忘了修改一下compass settings 修改一下高亮的样式,修改一下高亮的颜色。
2 < property name ="highlightFields" >
3 < list >
4 < value > content </ value >
5 </ list >
6 </ property >
7 < property name ="pageSize" >< value > 10 </ value ></ property >
8 < constructor-arg ref ="compass" />
9 </ bean >
10
11 < bean id ="searchController" class ="com.jdkcn.web.SearchController" parent ="baseController" >
12 < property name ="compass" >< ref bean ="compass" /></ property >
13 < property name ="searchView" >< value > search </ value ></ property >
14 < property name ="searchResultsView" >< value > search </ value ></ property >
15 < property name ="pageSize" >< value > 10 </ value ></ property >
16 < property name ="searchHelper" >< ref local ="advanceCompassSearchHelper" /></ property >
17 </ bean >
18
1
<
bean
id
="compass"
class
="org.compass.spring.LocalCompassBean"
>
2 < property name ="resourceDirectoryLocations" >
3 < list >
4 < value > classpath:com/jdkcn/compass </ value >
5 </ list >
6 </ property >
7 < property name ="connection" >
8 < value > /lucene/indexes </ value >
9 </ property >
10 < property name ="compassSettings" >
11 < props >
12 < prop key ="compass.transaction.factory" >
13 org.compass.spring.transaction.SpringSyncTransactionFactory
14 </ prop >
15 < prop key ="compass.engine.highlighter.default.formatter.simple.pre" >
16 <![CDATA[ <font color="red"><b> ]]>
17 </ prop >
18 < prop key ="compass.engine.highlighter.default.formatter.simple.post" >
19 <![CDATA[ </b></font> ]]>
20 </ prop >
21 </ props >
22 </ property >
23 < property name ="transactionManager" >
24 < ref bean ="transactionManager" />
25 </ property >
26 </ bean >
27
ok了,页面上输出一下就能看到最终效果了
2 < property name ="resourceDirectoryLocations" >
3 < list >
4 < value > classpath:com/jdkcn/compass </ value >
5 </ list >
6 </ property >
7 < property name ="connection" >
8 < value > /lucene/indexes </ value >
9 </ property >
10 < property name ="compassSettings" >
11 < props >
12 < prop key ="compass.transaction.factory" >
13 org.compass.spring.transaction.SpringSyncTransactionFactory
14 </ prop >
15 < prop key ="compass.engine.highlighter.default.formatter.simple.pre" >
16 <![CDATA[ <font color="red"><b> ]]>
17 </ prop >
18 < prop key ="compass.engine.highlighter.default.formatter.simple.post" >
19 <![CDATA[ </b></font> ]]>
20 </ prop >
21 </ props >
22 </ property >
23 < property name ="transactionManager" >
24 < ref bean ="transactionManager" />
25 </ property >
26 </ bean >
27
${hit.highlightedText['content']?if_exists}
本文转自:http://jdkcn.com/entry/howto-add-highlight-in-compass.html