Compass2.0.2自带例子解析:
下面的代码来自compass自带的例子 代码如下:
Author.java
1. package com.tutorial;
2.
3. import java.util.ArrayList;
4. import java.util.Date;
5. import java.util.List;
6.
7. public class Author implements Identifiable {
8.
9. private Long id;
10. private Name name;
11. private Date birthdate;
12. private String[] keywords;
13. private List<Book> books = new ArrayList<Book>();
14.
15. public Author() {
16. }
17.
18. public Date getBirthdate() {
19. return birthdate;
20. }
21.
22. public void setBirthdate(Date birthdate) {
23. this.birthdate = birthdate;
24. }
25.
26. public List<Book> getBooks() {
27. return books;
28. }
29.
30. public void setBooks(List<Book> books) {
31. this.books = books;
32. }
33.
34. public void addBook(Book book) {
35. books.add(book);
36. }
37.
38. public Long getId() {
39. return id;
40. }
41.
42. public void setId(Long id) {
43. this.id = id;
44. }
45.
46. public Name getName() {
47. return name;
48. }
49.
50. public void setName(Name name) {
51. this.name = name;
52. }
53.
54. public String[] getKeywords() {
55. return keywords;
56. }
57.
58. public void setKeywords(String[] keywords) {
59. this.keywords = keywords;
60. }
61.
62. public String toString() {
63. return name.toString();
64. }
65. }
Book.java
1. package com.tutorial;
2.
3. import java.util.Date;
4.
5. public class Book implements Identifiable {
6.
7. private Long id;
8. private String title;
9. private Date publishDate;
10. private String[] keywords;
11. private String summary;
12.
13. public Book() {
14. }
15.
16. public Book(String title, Date publishDate, String summary, String[] keywords) {
17. this.title = title;
18. this.publishDate = publishDate;
19. this.summary = summary;
20. this.keywords = keywords;
21. }
22.
23. public String[] getKeywords() {
24. return keywords;
25. }
26.
27. public void setKeywords(String[] keywords) {
28. this.keywords = keywords;
29. }
30.
31. public Date getPublishDate() {
32. return publishDate;
33. }
34.
35. public void setPublishDate(Date publishDate) {
36. this.publishDate = publishDate;
37. }
38.
39. public String getSummary() {
40. return summary;
41. }
42.
43. public void setSummary(String summary) {
44. this.summary = summary;
45. }
46.
47. public String getTitle() {
48. return title;
49. }
50.
51. public void setTitle(String title) {
52. this.title = title;
53. }
54.
55. public Long getId() {
56. return id;
57. }
58.
59. public void setId(Long id) {
60. this.id = id;
61. }
62.
63. public String toString() {
64. return title;
65. }
66. }
Identifiable.java
1. package com.tutorial;
2.
3. public interface Identifiable {
4.
5. Long getId();
6. }
Name.java
1. package com.tutorial;
2.
3. public class Name {
4.
5. private String title;
6. private String firstName;
7. private String lastName;
8.
9. public Name() {
10. }
11.
12. public Name(String title, String firstName, String lastName) {
13. this.title = title;
14. this.firstName = firstName;
15. this.lastName = lastName;
16. }
17.
18. public String getFirstName() {
19. return firstName;
20. }
21.
22. public void setFirstName(String firstName) {
23. this.firstName = firstName;
24. }
25.
26. public String getLastName() {
27. return lastName;
28. }
29.
30. public void setLastName(String lastName) {
31. this.lastName = lastName;
32. }
33.
34. public String getTitle() {
35. return title;
36. }
37.
38. public void setTitle(String title) {
39. this.title = title;
40. }
41.
42. public boolean equals(Object other) {
43.
44. if (this == other) {
45. return true;
46. }
47.
48. if (!(other instanceof Name)) {
49. return false;
50. }
51.
52. Name otherName = (Name) other;
53. if (title != null) {
54. if (!title.equals(otherName.getTitle())) {
55. return false;
56. }
57. }
58. if (firstName != null) {
59. if (!firstName.equals(otherName.getFirstName())) {
60. return false;
61. }
62. }
63. if (lastName != null) {
64. if (!lastName.equals(otherName.getLastName())) {
65. return false;
66. }
67. }
68. return true;
69. }
70.
71. public int hashCode() {
72. int hash = 1;
73. hash = hash * 31 + title == null ? 0 : title.hashCode();
74. hash = hash * 31 + firstName == null ? 0 : firstName.hashCode();
75. hash = hash * 31 + lastName == null ? 0 : lastName.hashCode();
76. return hash;
77. }
78.
79. public String toString() {
80. return title + ” ” + ” ” + firstName + ” ” + lastName;
81. }
82. }
Library.java
1. package com.tutorial;
2.
3. public final class Library {
4.
5. public static final class Group {
6. public static final String Id = “library”;
7. public static final String DispayName = “Library Meta Data”;
8. public static final String Uri = “http://compass/toturial“;
9. }
10.
11. public static final class Alias {
12.
13. public static final class Book {
14. public static final String Id = “book”;
15. public static final String Name = “book”;
16. public static final String DisplayName = “Book”;
17. public static final String Uri = “http://compass/toturial/alias/book“;
18. public static final String GroupId = “library”;
19. }
20.
21. public static final class Name {
22. public static final String Id = “name”;
23. public static final String Name = “name”;
24. public static final String DisplayName = “Name”;
25. public static final String Uri = “http://compass/toturial/alias/name“;
26. public static final String GroupId = “library”;
27. }
28.
29. public static final class Author {
30. public static final String Id = “author”;
31. public static final String Name = “author”;
32. public static final String DisplayName = “Author”;
33. public static final String Uri = “http://compass/toturial/alias/author“;
34. public static final String GroupId = “library”;
35. }
36.
37. }
38.
39. public static final class MetaData {
40.
41. public static final class Summary {
42. public static final String Id = “summary”;
43. public static final String Name = “summary”;
44. public static final String DisplayName = “Summary”;
45. public static final String Uri = “http://compass/toturial/summary“;
46. public static final String GroupId = “library”;
47. }
48.
49. public static final class Keyword {
50. public static final String Id = “keyword”;
51. public static final String Name = “keyword”;
52. public static final String DisplayName = “Keyword”;
53. public static final String Uri = “http://compass/toturial/keyword“;
54. public static final String GroupId = “library”;
55. }
56.
57. public static final class PublishDate {
58. public static final String Id = “publishDate”;
59. public static final String Name = “publish”;
60. public static final String DisplayName = “Publish Date”;
61. public static final String Uri = “http://compass/toturial/publishDate“;
62. public static final String GroupId = “library”;
63. }
64.
65. public static final class Title {
66. public static final String Id = “title”;
67. public static final String Name = “title”;
68. public static final String DisplayName = “Title”;
69. public static final String Uri = “http://compass/toturial/title“;
70. public static final String GroupId = “library”;
71. }
72.
73. public static final class Type {
74. public static final String Id = “type”;
75. public static final String Name = “type”;
76. public static final String DisplayName = “Type”;
77. public static final String Uri = “http://compass/toturial/type“;
78. public static final String GroupId = “library”;
79. }
80.
81. public static final class TitleName {
82. public static final String Id = “titleName”;
83. public static final String Name = “titleName”;
84. public static final String DisplayName = “Title Name”;
85. public static final String Uri = “http://compass/toturial/titleName“;
86. public static final String GroupId = “library”;
87. }
88.
89. public static final class Isbn {
90. public static final String Id = “isbn”;
91. public static final String Name = “name”;
92. public static final String DisplayName = “ISBN”;
93. public static final String Uri = “http://compass/toturial/name“;
94. public static final String GroupId = “library”;
95. }
96.
97. public static final class Name {
98. public static final String Id = “name”;
99. public static final String Name = “name”;
100. public static final String DisplayName = “Name”;
101. public static final String Uri = “http://compass/toturial/name“;
102. public static final String GroupId = “library”;
103. }
104.
105. public static final class Birthdate {
106. public static final String Id = “birthdate”;
107. public static final String Name = “birthdate”;
108. public static final String DisplayName = “Birthdate”;
109. public static final String Uri = “http://compass/toturial/birthdate“;
110. public static final String GroupId = “library”;
111. public static final String Format = “yyyy/MM/dd”;
112. }
113.
114. public static final class FirstName {
115. public static final String Id = “firstName”;
116. public static final String Name = “firstName”;
117. public static final String DisplayName = “First Name”;
118. public static final String Uri = “http://compass/toturial/firstName“;
119. public static final String GroupId = “library”;
120. }
121.
122. public static final class LastName {
123. public static final String Id = “lastName”;
124. public static final String Name = “lastName”;
125. public static final String DisplayName = “Last Name”;
126. public static final String Uri = “http://compass/sample/library/lastName“;
127. public static final String GroupId = “library”;
128. }
129. }
130. }
Author.cpm.xml
1. <?xml version=”1.0″?>
2. <!DOCTYPE compass-core-mapping PUBLIC
3. “-//Compass/Compass Core Mapping DTD 2.0//EN”
4. “http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd“>
5.
6. <compass-core-mapping package=”com.tutorial”>
7.
8. <class name=”Author” alias=”${library.author}”>
9.
10. <id name=”id” />
11.
12. <constant>
13. <meta-data>${library.type}</meta-data>
14. <meta-data-value>${library.type.mdPerson}</meta-data-value>
15. <meta-data-value>${library.type.mdAuthor}</meta-data-value>
16. </constant>
17.
18. <property name=”keywords”>
19. <meta-data boost=”2″>${library.keyword}</meta-data>
20. </property>
21.
22. <component name=”name” ref-alias=”${library.name.md}” />
23.
24. <property name=”birthdate”>
25. <meta-data>${library.birthdate}</meta-data>
26. </property>
27.
28. <reference name=”books” ref-alias=”${library.book}” />
29.
30. </class>
31.
32. <class name=”Name” alias=”${library.name}” root=”false”>
33. <property name=”title”>
34. <meta-data>${library.titleName}</meta-data>
35. </property>
36. <property name=”firstName”>
37. <meta-data>${library.firstName}</meta-data>
38. <meta-data>${library.name}</meta-data>
39. </property>
40. <property name=”lastName”>
41. <meta-data>${library.lastName}</meta-data>
42. <meta-data>${library.name}</meta-data>
43. </property>
44. </class>
45.
46. </compass-core-mapping>
Book.cpm.xml
1. <?xml version=”1.0″?>
2. <!DOCTYPE compass-core-mapping PUBLIC
3. “-//Compass/Compass Core Mapping DTD 2.0//EN”
4. “http://www.compass-project.org/dtd/compass-core-mapping-2.0.dtd“>
5.
6. <compass-core-mapping package=”com.tutorial”>
7.
8. <class name=”Book” alias=”${library.book}”>
9.
10. <id name=”id” />
11.
12. <property name=”keywords”>
13. <meta-data boost=”2″>${library.keyword}</meta-data>
14. </property>
15.
16. <property name=”title”>
17. <meta-data>${library.title}</meta-data>
18. </property>
19.
20. <property name=”publishDate”>
21. <meta-data>${library.publishDate}</meta-data>
22. </property>
23.
24. <property name=”summary”>
25. <meta-data>${library.summary}</meta-data>
26. </property>
27.
28. </class>
29.
30. </compass-core-mapping>
library.cmd.xml
1. <?xml version=”1.0″?>
2. <!DOCTYPE compass-core-meta-data PUBLIC
3. “-//Compass/Compass Core Meta Data DTD 2.0//EN”
4. “http://www.compass-project.org/dtd/compass-core-meta-data-2.0.dtd“>
5.
6. <compass-core-meta-data>
7.
8. <meta-data-group id=”library” displayName=”Library Meta Data”>
9.
10. <description>Library Meta Data</description>
11. <uri>http://compass/toturial</uri>
12.
13. <alias id=”author” displayName=”Author”>
14. <description>Author alias</description>
15. <uri>http://compass/toturial/alias/author</uri>
16. <name>author</name>
17. </alias>
18.
19. <alias id=”name” displayName=”Name”>
20. <description>Name alias</description>
21. <uri>http://compass/toturial/alias/name</uri>
22. <name>name</name>
23. </alias>
24.
25. <alias id=”book” displayName=”Book”>
26. <description>Book alias</description>
27. <uri>http://compass/toturial/alias/book</uri>
28. <name>book</name>
29. </alias>
30.
31. <meta-data id=”type” displayName=”Type”>
32. <description>Type of an entity in the system</description>
33. <uri>http://compass/toturial/type</uri>
34. <name>type</name>
35. <value id=”mdPerson”>person</value>
36. <value id=”mdAuthor”>author</value>
37. </meta-data>
38.
39. <meta-data id=”keyword” displayName=”Keyword”>
40. <description>Keyword associated with an entity</description>
41. <uri>http://compass/toturial/keyword</uri>
42. <name>keyword</name>
43. </meta-data>
44.
45. <meta-data id=”name” displayName=”Name”>
46. <description>The name of a person (firstName or lastName) without the title</description>
47. <uri>http://compass/toturial/name</uri>
48. <name>name</name>
49. </meta-data>
50.
51. <meta-data id=”firstName” displayName=”First Name”>
52. <description>The first name of a person</description>
53. <uri>http://compass/toturial/firstName</uri>
54. <name>firstName</name>
55. </meta-data>
56.
57. <meta-data id=”lastName” displayName=”Last Name”>
58. <description>The last name of a person</description>
59. <uri>http://compass/toturial/lastName</uri>
60. <name>lastName</name>
61. </meta-data>
62.
63. <meta-data id=”titleName” displayName=”Title Name”>
64. <description>The title of a person</description>
65. <uri>http://compass/toturial/titleName</uri>
66. <name>titleName</name>
67. </meta-data>
68.
69. <meta-data id=”birthdate” displayName=”Birthdate”>
70. <description>The birthdate of a person</description>
71. <uri>http://compass/toturial/birthdate</uri>
72. <name format=”yyyy/MM/dd”>birthdate</name>
73. </meta-data>
74.
75. <meta-data id=”isbn” displayName=”ISBN”>
76. <description>ISBN of the book</description>
77. <uri>http://compass/toturial/name</uri>
78. <name>name</name>
79. </meta-data>
80.
81. <meta-data id=”title” displayName=”Title”>
82. <description>The title of a book or an article</description>
83. <uri>http://compass/toturial/title</uri>
84. <name>title</name>
85. </meta-data>
86.
87. <meta-data id=”publishDate” displayName=”Publish Date”>
88. <description>The publish date of a book or an article</description>
89. <uri>http://compass/toturial/publishDate</uri>
90. <name>publish</name>
91. </meta-data>
92.
93. <meta-data id=”summary” displayName=”Summary”>
94. <description>The summary of a book or an article</description>
95. <uri>http://compass/toturial/summary</uri>
96. <name>summary</name>
97. </meta-data>
98.
99. </meta-data-group>
100.
101. </compass-core-meta-data>
compass.cfg.xml
1. <!DOCTYPE compass-core-configuration PUBLIC
2. “-//Compass/Compass Core Configuration DTD 2.0//EN”
3. “http://www.compass-project.org/dtd/compass-core-configuration-2.0.dtd“>
4.
5. <compass-core-configuration>
6.
7. <compass>
8.
9. <setting name=”compass.engine.connection”>target/index</setting>
10.
11. <meta-data resource=”com/tutorial/library.cmd.xml” />
12.
13. </compass>
14.
15. </compass-core-configuration>
测试代码:
LibraryTests.java
1. package com.demo;
2.
3. import java.text.SimpleDateFormat;
4. import java.util.Calendar;
5.
6. import org.compass.core.Compass;
7. import org.compass.core.CompassCallbackWithoutResult;
8. import org.compass.core.CompassException;
9. import org.compass.core.CompassHits;
10. import org.compass.core.CompassQueryBuilder;
11. import org.compass.core.CompassSession;
12. import org.compass.core.CompassTemplate;
13. import org.compass.core.CompassTransaction;
14. import org.compass.core.Resource;
15. import org.compass.core.config.CompassConfiguration;
16.
17. import com.tutorial.Author;
18. import com.tutorial.Book;
19. import com.tutorial.Identifiable;
20. import com.tutorial.Library;
21. import com.tutorial.Name;
22.
23. public class LibraryTests {
24.
25. public static void main(String[] args){
26.
27. CompassConfiguration config = new CompassConfiguration();
28. config.configure(“/com/tutorial/compass.cfg.xml”);
29. config.addClass(Author.class).addClass(Book.class);
30. Compass compass = config.buildCompass();
31. compass.getSearchEngineIndexManager().deleteIndex();
32. compass.getSearchEngineIndexManager().createIndex();
33.
34. try {
35. setUpData(compass);
36. } catch (Exception e) {
37. e.printStackTrace();
38. }
39.
40. CompassSession session = compass.openSession();
41. CompassTransaction tx = null;
42. try {
43. tx = session.beginTransaction();
44. Author author = (Author) session.load(Author.class, 1);
45.
46. SimpleDateFormat sdf = new SimpleDateFormat(Library.MetaData.Birthdate.Format);
47. System.out.println(sdf.format(author.getBirthdate()));
48. System.out.println(author.getName().getFirstName());
49. System.out.println(author.getBooks().size());
50.
51. //delete it
52. //session.delete(author);
53.
54. tx.commit();
55. } catch (Exception e) {
56. if (tx != null) {
57. tx.rollback();
58. }
59. } finally {
60. session.close();
61. }
62.
63. CompassTemplate compassTemplate = new CompassTemplate(compass);
64. compassTemplate.execute(new CompassCallbackWithoutResult() {
65. protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
66. String query = “fiercely”;
67. CompassHits hits = session.find(query);
68.
69. System.out.println(“Found [" + hits.getLength() + "] hits for [" + query + "] query”);
70. System.out.println(“======================================================”);
71. for (int i = 0; i < hits.getLength(); i++) {
72. Object value = hits.data(i);
73. Resource resource = hits.resource(i);
74. System.out.println(“ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]“);
75. System.out.println(“:::: ” + value);
76. System.out.println(“”);
77. }
78.
79. hits.close();
80. }
81. });
82.
83. compassTemplate = new CompassTemplate(compass);
84. compassTemplate.execute(new CompassCallbackWithoutResult() {
85. protected void doInCompassWithoutResult(CompassSession session) throws CompassException {
86.
87. CompassQueryBuilder queryBuilder = session.queryBuilder();
88. CompassHits hits = queryBuilder.bool().addMust(queryBuilder.term(“book.id”, 1))
89. //.addMustNot(queryBuilder.term(“author.books.id”, 1))
90. .toQuery().hits();
91.
92. System.out.println(“Found [" + hits.getLength() + "] hits for [" + "Jack" + "] query”);
93. System.out.println(“======================================================”);
94. for (int i = 0; i < hits.getLength(); i++) {
95. Object value = hits.data(i);
96. Resource resource = hits.resource(i);
97. System.out.println(“ALIAS [" + resource.getAlias() + "] ID [" + ((Identifiable) value).getId() + "] SCORE [" + hits.score(i) + "]“);
98. System.out.println(“:::: ” + value);
99. System.out.println(“”);
100. }
101.
102. hits.close();
103. }
104. });
105.
106. }
107.
108. public static void setUpData(Compass compass) throws Exception {
109.
110. CompassSession session = compass.openSession();
111. CompassTransaction tx = session.beginTransaction();
112.
113. Author jackLondon = new Author();
114. jackLondon.setId(new Long(1));
115. jackLondon.setName(new Name(“Mr”, “Jack”, “London”));
116. Calendar c = Calendar.getInstance();
117. c.set(1876, 0, 12);
118. jackLondon.setBirthdate(c.getTime());
119. jackLondon.setKeywords(new String[] { “american author” });
120.
121. Book whiteFang = new Book();
122. whiteFang.setId(new Long(1));
123. whiteFang.setTitle(“White Fang”);
124. c.set(1906, 0, 1);
125. whiteFang.setPublishDate(c.getTime());
126. whiteFang.setSummary(“The remarkable story of a fiercely independent creature of the wild”);
127. whiteFang.setKeywords(new String[] { “jack london”, “call of the wild” });
128. jackLondon.addBook(whiteFang);
129. session.save(whiteFang);
130.
131. Book callOfTheWild = new Book();
132. callOfTheWild.setId(new Long(2));
133. callOfTheWild.setTitle(“The Call of the Wild”);
134. c.set(1903, 0, 1);
135. callOfTheWild.setPublishDate(c.getTime());
136. callOfTheWild.setSummary(“The Call of the Wild is a tale about unbreakable spirit”);
137. callOfTheWild.setKeywords(new String[] { “jack london”, “buck”, “white fang” });
138. jackLondon.addBook(callOfTheWild);
139. session.save(callOfTheWild);
140.
141. session.save(jackLondon);
142.
143. Author jamesClavell = new Author();
144. jamesClavell.setId(new Long(2));
145. jamesClavell.setName(new Name(“Mr”, “James”, “Clavell”));
146. c.set(1924, 9, 10);
147. jamesClavell.setBirthdate(c.getTime());
148. jamesClavell.setKeywords(new String[] { “far east”, “shogun”, “japan”, “hong kong” });
149.
150. Book shogun = new Book();
151. shogun.setId(new Long(3));
152. shogun.setTitle(“Shogun”);
153. c.set(1975, 0, 1);
154. shogun.setPublishDate(c.getTime());
155. shogun.setSummary(“A story of a hero who is not a person but a place and a time,”
156. + ” medieval Japan on the threshold of becoming a sea power”);
157. shogun.setKeywords(new String[] { “james clavell”, “Blackthorne”, “Toranaga”, “japan” });
158. jamesClavell.addBook(shogun);
159. session.save(shogun);
160.
161. Book taipan = new Book();
162. taipan.setId(new Long(4));
163. taipan.setTitle(“Taipan”);
164. c.set(1966, 0, 1);
165. taipan.setPublishDate(c.getTime());
166. taipan.setSummary(“Tai-Pan is chinese for /”supreme leader/”. This is the man with real power ”
167. + “to his hands. And such a Tai-Pan is Dirk Struan who is obsessed by his plan to make Hong Kong ”
168. + “the /”jewel in the crown of her British Majesty/”. In 1841 he achieves his goal but he has many ”
169. + “enemies who try to destroy his plans. Will they succeed?”);
170. taipan.setKeywords(new String[] { “james clavell”, “Dirk Struan”, “joss”, “hong kong” });
171. jamesClavell.addBook(taipan);
172.
173. session.save(taipan);
174.
175. session.save(jamesClavell);
176.
177. tx.commit();
178. session.close();
179. }
180. }