mirror of
https://github.com/querydsl/querydsl.git
synced 2026-07-03 21:07:49 +08:00
simplified LuceneSession interface
This commit is contained in:
parent
a3eaf15d83
commit
3544eeab5e
@ -52,9 +52,12 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
public LuceneQuery(final Searcher searcher) {
|
||||
this(LuceneSerializer.DEFAULT, searcher);
|
||||
}
|
||||
|
||||
public void close(){
|
||||
// template method
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
private long innerCount(){
|
||||
try {
|
||||
final int maxDoc = searcher.maxDoc();
|
||||
if (maxDoc == 0) {
|
||||
@ -63,12 +66,27 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
return searcher.search(createQuery(), maxDoc).totalHits;
|
||||
} catch (final IOException e) {
|
||||
throw new QueryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long count() {
|
||||
try{
|
||||
return innerCount();
|
||||
}finally{
|
||||
close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public long countDistinct() {
|
||||
return count();
|
||||
try{
|
||||
return innerCount();
|
||||
}finally{
|
||||
close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private Query createQuery() {
|
||||
@ -128,9 +146,17 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
return iterate();
|
||||
}
|
||||
|
||||
private List<Document> innerList(){
|
||||
return new IteratorAdapter<Document>(iterate()).asList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Document> list() {
|
||||
return new IteratorAdapter<Document>(iterate()).asList();
|
||||
try{
|
||||
return innerList();
|
||||
}finally{
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -145,12 +171,17 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
|
||||
@Override
|
||||
public SearchResults<Document> listResults() {
|
||||
final List<Document> documents = list();
|
||||
/*
|
||||
* TODO Get rid of count(). It could be implemented by iterating the
|
||||
* list results in list* from n to m.
|
||||
*/
|
||||
return new SearchResults<Document>(documents, queryMixin.getMetadata().getModifiers(), count());
|
||||
try{
|
||||
List<Document> documents = innerList();
|
||||
/*
|
||||
* TODO Get rid of count(). It could be implemented by iterating the
|
||||
* list results in list* from n to m.
|
||||
*/
|
||||
return new SearchResults<Document>(documents, queryMixin.getMetadata().getModifiers(), innerCount());
|
||||
}finally{
|
||||
close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -176,7 +207,7 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
@Override
|
||||
public Document uniqueResult() {
|
||||
try {
|
||||
final int maxDoc = searcher.maxDoc();
|
||||
int maxDoc = searcher.maxDoc();
|
||||
if (maxDoc == 0) {
|
||||
return null;
|
||||
}
|
||||
@ -188,8 +219,10 @@ public class LuceneQuery implements SimpleQuery<LuceneQuery>,
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
} catch (IOException e) {
|
||||
throw new QueryException(e);
|
||||
} finally {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package com.mysema.query.lucene.session;
|
||||
|
||||
import com.mysema.query.lucene.LuceneQuery;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@ -9,19 +11,13 @@ package com.mysema.query.lucene.session;
|
||||
*/
|
||||
public interface LuceneSession {
|
||||
|
||||
// /**
|
||||
// * @return
|
||||
// */
|
||||
// LuceneQuery createQuery();
|
||||
|
||||
/**
|
||||
* Lucene query callback for querying
|
||||
* Creates a new LuceneQuery
|
||||
*
|
||||
* @param callback
|
||||
* @return
|
||||
*/
|
||||
<T> T query(QueryCallback<T> callback);
|
||||
|
||||
LuceneQuery createQuery();
|
||||
|
||||
/**
|
||||
* Creates a new index, adds updates to it and publishes the new index to
|
||||
* all readers after the callback finishes.
|
||||
|
||||
@ -88,42 +88,45 @@ public class LuceneSessionImpl implements LuceneSession {
|
||||
return searcher.get();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// @Override
|
||||
// public LuceneQuery createQuery() {
|
||||
// try {
|
||||
// IndexSearcher searcher = getSearcher();
|
||||
// searcher.getIndexReader().incRef();
|
||||
// return new LuceneQuery(serializer, searcher);
|
||||
// } catch (IOException e) {
|
||||
// throw new QueryException(e);
|
||||
// }
|
||||
// }
|
||||
|
||||
@Override
|
||||
public <T> T query(QueryCallback<T> callback) {
|
||||
public LuceneQuery createQuery() {
|
||||
try {
|
||||
IndexSearcher is = getSearcher();
|
||||
T results = null;
|
||||
try {
|
||||
// Incrementing the reference count
|
||||
is.getIndexReader().incRef();
|
||||
results = callback.query(new LuceneQuery(serializer, is));
|
||||
|
||||
} finally {
|
||||
// Releasing the reference count
|
||||
// This can be the last to actually close the reader
|
||||
is.getIndexReader().decRef();
|
||||
}
|
||||
|
||||
return results;
|
||||
final IndexSearcher searcher = getSearcher();
|
||||
searcher.getIndexReader().incRef();
|
||||
return new LuceneQuery(serializer, searcher){
|
||||
@Override
|
||||
public void close(){
|
||||
try {
|
||||
searcher.getIndexReader().decRef();
|
||||
} catch (IOException e) {
|
||||
throw new QueryException(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new QueryException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public <T> T query(QueryCallback<T> callback) {
|
||||
// try {
|
||||
// IndexSearcher is = getSearcher();
|
||||
// try {
|
||||
// // Incrementing the reference count
|
||||
// is.getIndexReader().incRef();
|
||||
// return callback.query(new LuceneQuery(serializer, is));
|
||||
// } finally {
|
||||
// // Releasing the reference count
|
||||
// // This can be the last to actually close the reader
|
||||
// is.getIndexReader().decRef();
|
||||
// }
|
||||
// } catch (IOException e) {
|
||||
// throw new QueryException(e);
|
||||
// }
|
||||
//
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void update(WriteCallback callback) {
|
||||
try {
|
||||
|
||||
@ -17,7 +17,6 @@ import org.apache.lucene.store.RAMDirectory;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.query.lucene.LuceneQuery;
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.query.types.path.EntityPathBase;
|
||||
import com.mysema.query.types.path.NumberPath;
|
||||
@ -46,9 +45,9 @@ public class LuceneSessionImplTest {
|
||||
|
||||
private StringPath title;
|
||||
|
||||
private NumberPath<Integer> year;
|
||||
|
||||
private NumberPath<Double> gross;
|
||||
// private NumberPath<Integer> year;
|
||||
//
|
||||
// private NumberPath<Double> gross;
|
||||
|
||||
@Before
|
||||
public void before() throws IOException {
|
||||
@ -56,8 +55,8 @@ public class LuceneSessionImplTest {
|
||||
session = new LuceneSessionImpl(directory);
|
||||
final QDocument entityPath = new QDocument("doc");
|
||||
title = entityPath.title;
|
||||
year = entityPath.year;
|
||||
gross = entityPath.gross;
|
||||
// year = entityPath.year;
|
||||
// gross = entityPath.gross;
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -89,25 +88,29 @@ 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.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();
|
||||
|
||||
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.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();
|
||||
|
||||
assertEquals(1, (long) count);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user