mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
This commit is contained in:
parent
0f2e808006
commit
d7ce1eeebe
@ -2,8 +2,6 @@ package com.mysema.query.lucene.session;
|
||||
|
||||
import com.mysema.query.lucene.LuceneQuery;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* General interface on using Lucene.
|
||||
*
|
||||
@ -17,7 +15,7 @@ public interface LuceneSession {
|
||||
* @return
|
||||
*/
|
||||
LuceneQuery createQuery();
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new index, adds updates to it and publishes the new index to
|
||||
* all readers after the callback finishes.
|
||||
@ -27,8 +25,8 @@ public interface LuceneSession {
|
||||
void updateNew(WriteCallback callback);
|
||||
|
||||
/**
|
||||
* Updates the current index and publishes it to all readers after the callback
|
||||
* finishes.
|
||||
* Updates the current index and publishes it to all readers after the
|
||||
* callback finishes.
|
||||
*
|
||||
* @param callback
|
||||
*/
|
||||
|
||||
@ -10,6 +10,9 @@ import org.apache.lucene.index.IndexWriter;
|
||||
*/
|
||||
public interface WriteCallback {
|
||||
|
||||
/**
|
||||
* @param writer
|
||||
*/
|
||||
void write(IndexWriter writer);
|
||||
|
||||
}
|
||||
|
||||
@ -17,54 +17,25 @@ import org.apache.lucene.store.RAMDirectory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.path.EntityPathBase;
|
||||
import com.mysema.query.types.path.NumberPath;
|
||||
import com.mysema.query.types.path.StringPath;
|
||||
|
||||
public class LuceneSessionImplTest {
|
||||
|
||||
public class QDocument extends EntityPathBase<Document> {
|
||||
|
||||
private static final long serialVersionUID = -4872833626508344081L;
|
||||
|
||||
public QDocument(final String var) {
|
||||
super(Document.class, PathMetadataFactory.forVariable(var));
|
||||
}
|
||||
|
||||
public final NumberPath<Integer> year = createNumber("year", Integer.class);
|
||||
|
||||
public final StringPath title = createString("title");
|
||||
|
||||
public final NumberPath<Double> gross = createNumber("gross", Double.class);
|
||||
}
|
||||
|
||||
private LuceneSession session;
|
||||
|
||||
private Directory directory;
|
||||
|
||||
private StringPath title;
|
||||
|
||||
// private NumberPath<Integer> year;
|
||||
//
|
||||
// private NumberPath<Double> gross;
|
||||
|
||||
private final QDocument document = new QDocument("doc");
|
||||
|
||||
@Before
|
||||
public void before() throws IOException {
|
||||
directory = new RAMDirectory();
|
||||
session = new LuceneSessionImpl(directory);
|
||||
final QDocument entityPath = new QDocument("doc");
|
||||
title = entityPath.title;
|
||||
// year = entityPath.year;
|
||||
// gross = entityPath.gross;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCreate() throws IOException {
|
||||
public void Create() throws IOException {
|
||||
|
||||
session.updateNew(new WriteCallback() {
|
||||
public void write(IndexWriter writer){
|
||||
|
||||
try {
|
||||
writer.addDocument(createDocument(
|
||||
"Jurassic Park",
|
||||
@ -88,40 +59,21 @@ public class LuceneSessionImplTest {
|
||||
}
|
||||
});
|
||||
|
||||
// List<Document> results = session.query(new QueryCallback<List<Document>>() {
|
||||
// public List<Document> query(LuceneQuery query) {
|
||||
//
|
||||
// return query.where(title.eq("Jurassic Park")).list();
|
||||
//
|
||||
// }
|
||||
// });
|
||||
|
||||
List<Document> results = session.createQuery().where(title.eq("Jurassic Park")).list();
|
||||
|
||||
List<Document> results = session.createQuery().where(document.title.eq("Jurassic Park")).list();
|
||||
assertEquals(1, results.size());
|
||||
assertEquals("Jurassic Park", results.get(0).getField("title").stringValue());
|
||||
|
||||
// Long count = session.query(new QueryCallback<Long>() {
|
||||
// public Long query(LuceneQuery query) {
|
||||
// //return query.where(title.ne("AA")).count();
|
||||
//
|
||||
// return query.where(title.startsWith("Nummi")).count();
|
||||
//
|
||||
// }
|
||||
// });
|
||||
|
||||
Long count = session.createQuery().where(title.startsWith("Nummi")).count();
|
||||
|
||||
Long count = session.createQuery().where(document.title.startsWith("Nummi")).count();
|
||||
assertEquals(1, (long) count);
|
||||
}
|
||||
|
||||
private Document createDocument(
|
||||
final String docTitle,
|
||||
final String docAuthor,
|
||||
final String docText,
|
||||
final int docYear,
|
||||
final double docGross) {
|
||||
final Document doc = new Document();
|
||||
String docTitle,
|
||||
String docAuthor,
|
||||
String docText,
|
||||
int docYear,
|
||||
double docGross) {
|
||||
Document doc = new Document();
|
||||
|
||||
doc.add(new Field("title", docTitle, Store.YES, Index.ANALYZED));
|
||||
doc.add(new Field("author", docAuthor, Store.YES, Index.ANALYZED));
|
||||
|
||||
@ -0,0 +1,26 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.mysema.query.lucene.session;
|
||||
|
||||
import org.apache.lucene.document.Document;
|
||||
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.path.EntityPathBase;
|
||||
import com.mysema.query.types.path.NumberPath;
|
||||
import com.mysema.query.types.path.StringPath;
|
||||
|
||||
public class QDocument extends EntityPathBase<Document> {
|
||||
|
||||
private static final long serialVersionUID = -4872833626508344081L;
|
||||
|
||||
public QDocument(final String var) {
|
||||
super(Document.class, PathMetadataFactory.forVariable(var));
|
||||
}
|
||||
|
||||
public final NumberPath<Integer> year = createNumber("year", Integer.class);
|
||||
|
||||
public final StringPath title = createString("title");
|
||||
|
||||
public final NumberPath<Double> gross = createNumber("gross", Double.class);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user