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="target/test-index" />
</connection>
<transaction >
<processors>
<readCommitted transLog="ram://" />
</processors>
</transaction>
<cache>
<firstLevel type="org.compass.core.cache.first.NullFirstLevelCache" />
</cache>
<searchEngine useCompoundFile="true">
<optimizer schedule="false" />
</searchEngine>
</compass>
</compass-core-config>
=====================================================================
package org.jixiuf.compass.pojo;
import java.util.Date;
import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.SearchableReference;
import org.compass.annotations.Store;
@Searchable
public class Author {
String id;
String name;
Book book;
Date date;
@SearchableId(store=Store.YES,index=Index.UN_TOKENIZED,name="id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@SearchableProperty(index = Index.TOKENIZED, store = Store.YES)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@SearchableReference
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
@SearchableProperty(store=Store.YES,index=Index.NOT_ANALYZED,format="yyyy-MM-dd HH:mm:ss")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package org.jixiuf.compass.pojo;
import java.util.Date;
import java.util.List;
import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.SearchableReference;
import org.compass.annotations.Store;
@Searchable
public class Author2 {
String id;
String name;
List<Book> books;
Date date;
@SearchableId(store=Store.YES,index=Index.UN_TOKENIZED,name="id")
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@SearchableProperty(index = Index.TOKENIZED, store = Store.YES)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@SearchableReference
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
@SearchableProperty(store=Store.YES,index=Index.NOT_ANALYZED,format="yyyy-MM-dd HH:mm:ss")
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package org.jixiuf.compass.pojo;
import org.compass.annotations.Index;
import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;
import org.compass.annotations.SearchableReference;
import org.compass.annotations.Store;
@Searchable
public class Book {
String id;
String name;
Author2 author;
@SearchableReference
public Author2 getAuthor() {
return author;
}
public void setAuthor(Author2 author) {
this.author = author;
}
@SearchableId(name="id",store=Store.YES,index=Index.UN_TOKENIZED)
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@SearchableProperty(index = Index.TOKENIZED, store = Store.YES)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
===============================================================
package org.jixiuf.compass;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.compass.core.CompassException;
import org.compass.core.CompassHighlighter;
import org.compass.core.CompassHits;
import org.compass.core.CompassQuery;
import org.compass.core.CompassQueryBuilder;
import org.compass.core.CompassSession;
import org.compass.core.CompassTransaction;
import org.compass.core.CompassQueryBuilder.CompassBooleanQueryBuilder;
import org.jixiuf.compass.pojo.Author;
import org.jixiuf.compass.pojo.Author2;
import org.jixiuf.compass.pojo.Book;
import org.jixiuf.compass.util.CompassUtil;
import org.junit.Test;
public class Test1 {
@Test
public void testSave() {
CompassSession session = CompassUtil.getCompassSession();
CompassTransaction tac = session.beginTransaction();
try {
Author author = new Author();
Book book = new Book();
book.setId("你好");
book.setName("bookName");
author.setId("你好");
author.setName("中文作者名纪秀峰");
author.setBook(book);
author.setDate(new Date());
session.save(book);
session.save(author);
tac.commit();
session.commit();
} catch (CompassException ce) {
System.out.println("error==============");
session.rollback();
}
}
@Test
public void testSaveList() {
CompassSession session = CompassUtil.getCompassSession();
CompassTransaction tac = session.beginTransaction();
try {
Author2 author = new Author2();
author.setId("你好");
author.setName("中文作者名纪秀峰");
List<Book> books = new ArrayList<Book>();
Book book = new Book();
book.setId("你好");
book.setName("bookName");
book.setAuthor(author);
books.add(book);
session.save(book);
book= new Book();
book.setId("wq");
book.setName("mameaaaaaaa");
books.add(book);
author.setBooks(books);
author.setDate(new Date());
session.save(book);
session.save(author);
tac.commit();
session.commit();
} catch (CompassException ce) {
System.out.println("error==============");
session.rollback();
}
}
@Test
public void testCascade() {
CompassSession session = CompassUtil.getCompassSession();
CompassTransaction tac = session.beginTransaction();
try {
Author2 author = new Author2();
author.setId("你好");
author.setName("中文作者名纪秀峰");
Book book = new Book();
book.setId("你好");
book.setName("bookName");
book.setAuthor(author);
session.save(book);
author.setDate(new Date());
session.save(book);
session.save(author);
tac.commit();
session.commit();
} catch (CompassException ce) {
System.out.println("error==============");
session.rollback();
}
}
@Test
public void testFind() {
CompassSession session = CompassUtil.getCompassSession();
CompassQuery queryAuthor = session.queryBuilder().alias("Book");
CompassQuery id = session.queryBuilder().term("id", "你好");
CompassHits hits = session.queryBuilder().bool().addMust(queryAuthor).addShould(id)
.toQuery().hits();
Book a = (Book) hits.data(0);
System.out.println("author.name:" + a.getName());
System.out.println("author.id:" + a.getId());
// System.out.println(a.getBook().getId());
System.out.println(a.getAuthor().getId());
session.commit();
}
@Test
public void testFindBookByauthor() {
CompassSession session = CompassUtil.getCompassSession();
CompassQuery queryAuthor = session.queryBuilder().alias("Book");
CompassQuery id = session.queryBuilder().term("book.author.id", "你好");
CompassHits hits = session.queryBuilder().bool().addMust(queryAuthor).addShould(id)
.toQuery().hits();
Book a = (Book) hits.data(0);
System.out.println("author.name:" + a.getName());
System.out.println("author.id:" + a.getId());
// System.out.println(a.getBook().getId());
System.out.println(a.getAuthor().getId());
session.commit();
}
@Test
public void testFindList() {
CompassSession session = CompassUtil.getCompassSession();
CompassQuery queryAuthor = session.queryBuilder().alias("Author2");
CompassQuery id = session.queryBuilder().term("id", "你好");
CompassHits hits = session.queryBuilder().bool().addMust(queryAuthor).addShould(id)
.toQuery().hits();
Author2 a = (Author2) hits.data(0);
System.out.println("author.name:" + a.getName());
System.out.println("author.id:" + a.getId());
List<Book> books = a.getBooks();
System.out.println(books.size());
for(Book b :books) {
System.out.println(b.getId());
}
// System.out.println(a.getBook().getId());
session.commit();
}
@Test
public void testFindDate() {
CompassSession session = CompassUtil.getCompassSession();
CompassQuery queryAuthor = session.queryBuilder().alias("Author");
CompassQuery id = session.queryBuilder().term("date", "2010-04-23 23:35:22");
CompassHits hits = session.queryBuilder().bool().addMust(queryAuthor).addMust(id)
.toQuery().hits();
Author a = (Author) hits.data(0);
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(a.getDate()));
System.out.println("author.name:" + a.getName());
System.out.println("author.id:" + a.getId());
// System.out.println(a.getBook().getId());
session.commit();
}
@Test
public void testDelete() {
CompassSession session = CompassUtil.getCompassSession();
Book a = new Book();
a.setId("你好");
session.delete(a);
session.commit();
}
@Test
public void testReIndex() {
testDelete();
testSave();
}
@Test
// 高亮显示
public void testHighLight() {
CompassSession session = CompassUtil.getCompassSession();
CompassHits hits = session.find("纪秀峰");
Author a = (Author) hits.data(0);
CompassHighlighter hll = hits.highlighter(0);
// hll.setHighlighter("jixiuf");
String ht = hll.fragment("a", "纪safsdfa");
System.out.println(ht);
System.out.println(hll.fragment("name"));
session.commit();
}
@Test
// 相对于 testQueryUs 过滤了Book ,只搜book 类型
public void testQueryBookByUseBoolean() {
CompassSession session = CompassUtil.getCompassSession();
CompassQueryBuilder qb = session.queryBuilder();
CompassBooleanQueryBuilder boo = qb.bool();
CompassQuery bookQuery = qb.alias(Book.class.getSimpleName());// 只查alias
CompassQuery us = qb.queryString("我们").toQuery();// 只查有"我们"关键字的
CompassHits hits = boo.addMust(bookQuery).addMust(us).toQuery().hits();
for (int i = 0; i < hits.length(); i++) {
Book b = (Book) hits.data(i);
System.out.println("book.id=" + b.getId() + " book.name"
+ b.getName());
}
session.commit();
}
@Test
public void testQueryUs() {
CompassSession session = CompassUtil.getCompassSession();
CompassQueryBuilder qb = session.queryBuilder();
CompassHits hits = qb.queryString("我们").toQuery().hits();// 只查有"我们"关键字的
for (int i = 0; i < hits.length(); i++) {
Object o = hits.data(i);
if (o instanceof Book) {
System.out.println("= a book ==============");
Book b = (Book) o;
System.out.println("book.id=" + b.getId() + " book.name"
+ b.getName());
} else if (o instanceof Author) {
Author a = (Author) o;
System.out.println("=a author==============");
System.out.println(a.getName());
System.out.println(a.getId());
System.out.println(a.getBook());
}
}
session.commit();
}
}
===============
package org.jixiuf.compass.util;
import org.compass.annotations.config.CompassAnnotationsConfiguration;
import org.compass.core.Compass;
import org.compass.core.CompassSession;
import org.compass.core.config.CompassConfiguration;
public class CompassUtil {
public static Compass getCompass() {
// CompassConfiguration conf = new
// CompassAnnotationsConfiguration().addScan("org/jixiuf/compass/pojo").configure();
CompassConfiguration conf = new CompassAnnotationsConfiguration()
.addScan("org/jixiuf/compass/pojo");
// CompassConfiguration conf= new CompassAnnotationsConfiguration()
// 注意这里的配置可以从compass reference 中查看到,其中的default 字为默认的highlighter
// ,也可以是你自定认一个
// compass.engine.highlighter.jixiufffffffffffffff.formatter.simple.pre
// CompassHighlighterInstance.setHighlighter("jixiufffffffffffffff");
conf.setSetting(
"compass.engine.highlighter.default.formatter.simple.pre",
"<span style=' color:red'>");
conf.setSetting(
"compass.engine.highlighter.default.formatter.simple.post",
"</span>");
conf.setSetting(
"compass.engine.highlighter.jixiuf.formatter.simple.pre",
"<span name='jixiuf' ,style=' color:red'>");
conf.setSetting(
"compass.engine.highlighter.jixiuf.formatter.simple.post",
"</span>");
conf.configure();
Compass compass = conf.buildCompass();
return compass;
}
public static CompassSession getCompassSession() {
return getCompass().openSession();
}
}