The Apache Lucene project develops open-source search software
// -- test/compass/App.java --
package test.compass;
import org.apache.commons.lang.time.DateUtils;
import org.compass.core.Compass;
import org.compass.core.CompassHits;
import org.compass.core.CompassSession;
import org.compass.core.config.CompassConfiguration;
public class App {
static final String[] DATE_PATTERN = { "yyyy-MM-dd" };
public static void main(String[] args) throws Exception {
CompassConfiguration conf = new CompassConfiguration().configure();
Compass compass = conf.buildCompass();
CompassSession session = compass.openSession();
try {
Author a = new Author();
a.setId(1L);
a.setName("Solnone");
a.setBirthday(DateUtils.parseDate("1900-01-01", DATE_PATTERN));
session.save(a);
CompassHits hits = session.find("Solnone");
for (int i = 0; i < hits.getLength(); i++) {
System.out.println(hits.data(i));
}
} finally {
session.close();
compass.close();
}
}
}
/*
test.compass.Author@7f5580[id=1,name=Solnone,birthday=Mon Jan 01 00:00:00 CST 1900]
*/
// -- test/compass/Author.java --
package test.compass;
import java.util.Date;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableConstant;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableMetaData;
import org.compass.annotations.SearchableProperty;
@Searchable
@SearchableConstant(name = "type", values = { "person", "author" })
public class Author {
private Long id;
private String name;
private Date birthday;
@SearchableId
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@SearchableProperty(name = "name")
@SearchableMetaData(name = "authorName")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@SearchableProperty(format = "yyyy-MM-dd")
public Date getBirthday() {
return this.birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
// -- compass.cfg.xml --
<compass-core-config xmlns="http://www.compass-project.org/schema/core-config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.compass-project.org/schema/core-config
http://www.compass-project.org/schema/compass-core-config-2.2.xsd">
<compass name="default">
<connection>
<file path="test-index" />
</connection>
<mappings>
<class name="test.compass.Author" />
</mappings>
</compass>
</compass-core-config>
沒有留言:
張貼留言