COMPASS應用(轉)

          一個簡單的COMPASS應用

          首先你要下載Compass framework: Download Compass.
          你需要在你的class path 中添加4個jarcompass-x/modules/core/compass-core-x.jar, compass/modules/core/lib/commons-logging-x.jar, compass-x/modules/core/lib/log4j-x.jar, compass-x/modules/core/lib/lucene-core-x-rc1-dev.jar.
          在你的項目中創建下面的目錄(可以根據自己的定義來改動):

          log4j.properties
          - org
          ?? - compassframework
          ???? - sample
          ?????? - example
          ?????????? alias.cmd.xml
          ?????????? compass.cfg.xml
          ?????????? CompassExample.java
          ?????????? Phrase.cpm.xml
          ?????????? Phrase.java
          下面說一下幾個重要的配置文件

          ?compass.cfg.xmlcode

          指定的target/index?是一個存儲目錄放索引文件的

          (這個類必須有個無參數的構造和id屬性)

          xml 代碼
          1. <!--CTYPE?compass-core-configuration?PUBLIC"-//Compass/Compass?Core?Configuration?DTD?1.0//EN"? ??</sp-->
          2. ??
          3. ??
          4. "http://static.compassframework.org/dtd/compass-core-configuration-1.0.dtd" > ? ??
          5. ??
          6. ??
          7. ??
          8. < ??
          9. ??
          10. compass-core-configuration > ? ??
          11. < compass > ??
          12. ??
          13. < setting ? name = "compass.engine.connection" > target/index setting > ??
          14. ??
          15. < meta-data ? resource = "org/compassframework/sample/example/alias.cmd.xml" ? /> ??
          16. ??
          17. compass > ??
          18. ??
          19. compass-core-configuration > ??
          20. ??

          ?alias.cmd.xml:

          xml 代碼
          1. <!--sp-->??
          2. ??
          3. xml ? version = "1.0" ?> ? ??
          4. <!--CTYPE?compass-core-meta-data?PUBLIC? ??</sp-->
          5. "-//Compass/Compass?Core?Meta?Data?DTD?1.0//EN"? ??
          6. "http://static.compassframework.org/dtd/compass-core-meta-data-1.0.dtd" > ? ??
          7. < compass-core-meta-data > ? ??
          8. < meta-data-group ? id = "example" ? displayName = "Example?Meta?Data" > ??
          9. ??
          10. < description > Example?Meta?Data description > ??
          11. ??
          12. < uri > http://compass/example uri > ??
          13. ??
          14. ??
          15. < alias ? id = "phrase" ? displayName = "Phrase" > ??
          16. ??
          17. < description > Phrase?alias description > ??
          18. ??
          19. < uri > http://compass/example/phrase uri > ??
          20. ??
          21. < name > phrase name > ??
          22. ??
          23. alias > ??
          24. ??
          25. < meta-data ? id = "author" ? displayName = "Author" > ??
          26. ??
          27. < description > Author?alias description > ??
          28. ??
          29. < uri > http://compass/example/author uri > ??
          30. ??
          31. < name > author name > ??
          32. ??
          33. meta-data > ??
          34. ??
          35. < meta-data ? id = "text" ? displayName = "Text" > ??
          36. ??
          37. < description > Text?alias description > ??
          38. ??
          39. < uri > http://compass/example/text uri > ??
          40. ??
          41. < name > text name > ??
          42. ??
          43. meta-data > ??
          44. ??
          45. meta-data-group > ??
          46. ??
          47. compass-core-meta-data > ??

          Phrase.java

          java 代碼
          1. package ?org.compassframework.sample.example; ??
          2. ??
          3. public ? class ?Phrase?{ ??
          4. ??
          5. ? private ?String?author; ??
          6. ??
          7. ? private ?String?text; ??
          8. ??
          9. ? private ?Long?id; ??
          10. ??
          11. ? public ?Phrase()?{ ??
          12. ??
          13. ?} ??
          14. ??
          15. ? public ?String?getAuthor()?{ ??
          16. ????? return ?author; ??
          17. ?} ??
          18. ??
          19. ? public ? void ?setAuthor(String?author)?{ ??
          20. ????? this .author?=?author; ??
          21. ?} ??
          22. ??
          23. ? public ?String?getText()?{ ??
          24. ????? return ?text; ??
          25. ?} ??
          26. ??
          27. ? public ? void ?setText(String?text)?{ ??
          28. ????? this .text?=?text; ??
          29. ?} ??
          30. ??
          31. ? public ?Long?getId()?{ ??
          32. ????? return ?id; ??
          33. ?} ??
          34. ??
          35. ? public ? void ?setId(Long?id)?{ ??
          36. ????? this .id?=?id; ??
          37. ?} ??
          38. ??
          39. ? public ?String?toString()?{ ??
          40. ??
          41. ????? return ?text; ??
          42. ?} ??
          43. ??
          44. }? ??
          Phrase.cpm.xml

          ?

          xml 代碼
          1. <!--sp-->xml?version="1.0"?>? ??
          2. <!--CTYPE?compass-core-mapping?PUBLIC? ??</sp-->
          3. "-//Compass/Compass?Core?Mapping?DTD?1.0//EN"? ??
          4. "http://static.compassframework.org/dtd/compass-core-mapping-1.0.dtd" > ? ??
          5. < compass-core-mapping ? package = "org.compassframework.sample.example" > ? ??
          6. < class ? name = "Phrase" ? alias = "${example.phrase}" > ??
          7. ??
          8. < id ? name = "id" ? /> ??
          9. ??
          10. < property ? name = "author" > ??
          11. ??
          12. < meta-data > ${example.author} meta-data > ??
          13. ??
          14. property > ??
          15. ??
          16. < property ? name = "text" > ??
          17. ??
          18. < meta-data > ${example.text} meta-data > ??
          19. ??
          20. property > ??
          21. ??
          22. class > ??
          23. ??
          24. compass-core-mapping > ??
          CompassExample.java

          java 代碼
          1. package ?org.compassframework.sample.example; ??
          2. ??
          3. import ?org.apache.log4j.Logger; ??
          4. ??
          5. import ?org.compassframework.core.Compass; ??
          6. import ?org.compassframework.core.Compass; ??
          7. import ?org.compassframework.core.CompassSession; ??
          8. import ?org.compassframework.core.CompassTransaction; ??
          9. import ?org.compassframework.core.config.CompassConfiguration; ??
          10. ??
          11. public ? class ?CompassExample?{ ??
          12. ??
          13. ? private ? static ? final ?Logger?logger?=?Logger.getLogger(CompassExample. class ); ??
          14. ? ??
          15. ? private ?compasscompass; ??
          16. ? ??
          17. ? public ? static ? void ?main(String[]?args){ ??
          18. ????? new ?CompassExample().process(); ??
          19. ?} ??
          20. ? ??
          21. ? private ? void ?process(){ ??
          22. ?????CompassConfiguration?config?=? new ?CompassConfiguration(); ??
          23. ?????config.configure( "org/compassframework/sample/example/compass.cfg.xml" ); ??
          24. ?????config.addClass(Phrase. class ); ??
          25. ?????compass?=?config.buildCompass(); ??
          26. ?????compass.getSearchEngineIndexManager().deleteIndex(); ??
          27. ?????compass.getSearchEngineIndexManager().createIndex(); ??
          28. ?? ??
          29. ?????createIndex(); ??
          30. ?????search( "mule?AND?crazy" ); ??
          31. ?????search( "Marisol?OR?Ramon" ); ??
          32. ?} ??
          33. ? ??
          34. ? private ? void ?createIndex(){ ??
          35. ??CompassSession?session?=?compass.openSession(); ??
          36. ?????CompassTransaction?tx?=?session.beginTransaction(); ??
          37. ?????Phrase?phrase?=? new ?Phrase(); ??
          38. ?????phrase.setAuthor( "Joe" ); ??
          39. ?????phrase.setText( "I?don't?think?it's?nice?you?laughing.?" ?+? ??
          40. ???????????? "You?see?my?mule?don't?like?people?laughing.?" ?+? ??
          41. ???????????? "He?gets?the?crazy?idea?you're?laughing?at?him.?" ?+? ??
          42. ???????????? "Now?if?you?apologize?like?I?know?you're?going?to,?" ?+? ??
          43. ???????????? "I?might?convince?him?that?you?really?didn't?mean?it..." ); ??
          44. ?????phrase.setId( new ?Long( 1 )); ??
          45. ?????session.save(phrase); ??
          46. ?????tx.commit(); ??
          47. ?????session.close(); ??
          48. ?} ??
          49. ? ??
          50. ? private ? void ?search(String?query){ ??
          51. ?????CompassSession?session?=?compass.openSession(); ??
          52. ?????CompassTransaction?tx?=?session.beginTransaction(); ??
          53. ?????Compass?hits?=?session.find(query); ??
          54. ????? if ?(logger.isDebugEnabled())?{ ??
          55. ????????logger.debug( "search()?-?Found?" ?+?hits.getLength()+ "?hits?for?\"" +query+ "\"" ); ??
          56. ?????} ??
          57. ????? for ?( int ?i?=? 0 ;?i?<?hits.getLength();?i++)?{ ??
          58. ????????print(hits,?i); ??
          59. ?????} ??
          60. ?????hits.close(); ??
          61. ?????tx.commit(); ??
          62. ?????session.close(); ??
          63. ?????compass.close(); ??
          64. ?} ??
          65. ? ??
          66. ? private ? void ?print(Compass?hits,? int ?hitNumber)?{ ??
          67. ?????Phrase?value?=?(Phrase)hits.data(hitNumber); ??
          68. ????? if ?(logger.isDebugEnabled())?{ ??
          69. ?????????logger.debug( "print()?-?Phrase?by?" ?+?value.getAuthor()?+? ":?" ??
          70. ?????????????+?value.getText()); ??
          71. ?????} ??
          72. ?} ??
          73. ??
          74. } ??
          75. ??

          ?

          為了保證能打印出我們的測試,需要加入log4j.properties
          log4j.rootLogger=ERROR, stdout
          log4j.appender.stdout=org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p %c - %m%n
          log4j.logger.org.compassframework.sample.example=DEBUG


          下面是打印出來的測試結果:
          search() - Found 1 hits for "mule AND crazy"
          print() - Phrase by Joe: I don't think it's nice you laughing...
          search() - Found 0 hits for "Marisol OR Ramon"

          posted on 2007-01-29 18:12 leoli 閱讀(510) 評論(0)  編輯  收藏 所屬分類: Frame

          導航

          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          統計

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案(17)

          文章分類(86)

          收藏夾(3)

          flex blog

          good site

          java blog

          my friend

          tools

          抓蝦

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 方山县| 翁源县| 舟曲县| 台北市| 临沧市| 玉山县| 旬邑县| 房产| 兴海县| 交口县| 衡水市| 岚皋县| 富顺县| 菏泽市| 民丰县| 五指山市| 沂水县| 启东市| 凤山县| 阿拉善右旗| 友谊县| 汉源县| 遂昌县| 定安县| 元朗区| 澎湖县| 南汇区| 扬中市| 巨鹿县| 鸡东县| 资溪县| 南城县| 乐昌市| 商水县| 卫辉市| 安泽县| 东丽区| 贵南县| 金川县| 漠河县| 丹凤县|