Skip to content

Commit

Permalink
HSEARCH-785 - Expose topDocs to integration layer, and make HSQueryIm…
Browse files Browse the repository at this point in the history
…pl Serializable
  • Loading branch information
Sanne committed Jun 30, 2011
1 parent b6b4540 commit da859e9
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.lucene.document.FieldSelector;
import org.apache.lucene.document.FieldSelectorResult;
import org.apache.lucene.document.MapFieldSelector;
import org.apache.lucene.search.TopDocs;

import org.hibernate.search.ProjectionConstants;
import org.hibernate.search.engine.DocumentBuilder;
Expand Down Expand Up @@ -308,4 +309,9 @@ private String forceClassNameExtraction(int scoreDocIndex) throws IOException {
return doc.get( DocumentBuilder.CLASS_FIELDNAME );
}

@Override
public TopDocs getTopDocs() {
return queryHits.getTopDocs();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package org.hibernate.search.query.engine.impl;

import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -72,14 +73,14 @@
* @author Emmanuel Bernard <emmanuel@hibernate.org>
* @author Hardy Ferentschik <hardy@hibernate.org>
*/
public class HSQueryImpl implements HSQuery {
public class HSQueryImpl implements HSQuery, Serializable {

private static final FullTextFilterImplementor[] EMPTY_FULL_TEXT_FILTER_IMPLEMENTOR = new FullTextFilterImplementor[0];

private final SearchFactoryImplementor searchFactoryImplementor;
private transient SearchFactoryImplementor searchFactoryImplementor;
private Query luceneQuery;
private List<Class<?>> targetedEntities;
private TimeoutManagerImpl timeoutManager;
private transient TimeoutManagerImpl timeoutManager;
private Set<Class<?>> indexedTargetedEntities;
private boolean allowFieldSelectionInProjection = true;
/**
Expand All @@ -92,13 +93,13 @@ public class HSQueryImpl implements HSQuery {
private String[] projectedFields;
private int firstResult;
private Integer maxResults;
private Set<Class<?>> classesAndSubclasses;
private transient Set<Class<?>> classesAndSubclasses;
//optimization: if we can avoid the filter clause (we can most of the time) do it as it has a significant perf impact
private boolean needClassFilterClause;
private Set<String> idFieldNames;
private TimeoutExceptionFactory timeoutExceptionFactory = QueryTimeoutException.DEFAULT_TIMEOUT_EXCEPTION_FACTORY;
private transient TimeoutExceptionFactory timeoutExceptionFactory = QueryTimeoutException.DEFAULT_TIMEOUT_EXCEPTION_FACTORY;
private boolean useFieldCacheOnClassTypes = false;
private FacetManagerImpl facetManager;
private transient FacetManagerImpl facetManager;

/**
* The number of results for this query. This field gets populated once {@link #queryResultSize}, {@link #queryEntityInfos}
Expand All @@ -109,6 +110,10 @@ public class HSQueryImpl implements HSQuery {
public HSQueryImpl(SearchFactoryImplementor searchFactoryImplementor) {
this.searchFactoryImplementor = searchFactoryImplementor;
}

public void afterDeserialise(SearchFactoryImplementor searchFactoryImplementor) {
this.searchFactoryImplementor = searchFactoryImplementor;
}

public HSQuery luceneQuery(Query query) {
clearCachedResults();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.io.IOException;

import org.apache.lucene.search.TopDocs;

/**
* DocumentExtractor is a traverser over the full-text results (EntityInfo)
*
Expand All @@ -43,4 +45,6 @@ public interface DocumentExtractor {
int getMaxIndex();

void close();

TopDocs getTopDocs();
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,10 @@ public interface HSQuery extends ProjectionConstants {
* @deprecated should be at most SearchFactoryIntegrator, preferably removed altogether
*/
SearchFactoryImplementor getSearchFactoryImplementor();

/**
* @param searchFactory
*/
void afterDeserialise(SearchFactoryImplementor searchFactory);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* JBoss, Home of Professional Open Source
* Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

package org.hibernate.search.test.engine;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.hibernate.search.engine.SearchFactoryImplementor;
import org.hibernate.search.query.engine.spi.DocumentExtractor;
import org.hibernate.search.query.engine.spi.HSQuery;
import org.hibernate.search.test.AlternateDocument;
import org.hibernate.search.test.Document;
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.test.SerializationTestHelper;
import org.junit.Test;

/**
* This test is meant to verify that HSQuery implementation is able to
* be serialized, deserialized and then still perform the query.
* a
* @author Sanne Grinovero <sanne@hibernate.org> (C) 2011 Red Hat Inc.
*/
public class QuerySerializationTest extends SearchTestCase {

@Test
public void testQueryObjectIsSerializable() throws IOException, ClassNotFoundException {
Session s = getSessions().openSession();
s.getTransaction().begin();
Document document =
new Document( "Hibernate OGM in Action", "Cloud mapping with Hibernate", "blah blah cloud blah cloud" );
s.persist( document );
s.getTransaction().commit();
s.close();

TermQuery query = new TermQuery( new Term( "Abstract", "hibernate" ) );

FullTextSession fullTextSession = Search.getFullTextSession( s );
SearchFactoryImplementor searchFactory = (SearchFactoryImplementor) fullTextSession.getSearchFactory();

//this is *not* the standard way to create a Query:
HSQuery hsQuery = searchFactory.createHSQuery().luceneQuery( query ).targetedEntities( new ArrayList() );
int size = extractResultSize(hsQuery);

assertEquals( "Should have found a match", 1, size );

HSQuery hsQueryDuplicate = (HSQuery) SerializationTestHelper.duplicateBySerialization(hsQuery);
hsQueryDuplicate.afterDeserialise(searchFactory);
int sizeOfDuplicate = extractResultSize(hsQueryDuplicate);

assertEquals( "Should have found a match", 1, sizeOfDuplicate );
}

private int extractResultSize(HSQuery hsQuery) {
DocumentExtractor documentExtractor = hsQuery.queryDocumentExtractor();
TopDocs topDocs = documentExtractor.getTopDocs();
documentExtractor.close();
return topDocs.totalHits;
}

protected Class<?>[] getAnnotatedClasses() {
return new Class[] {
Document.class,
AlternateDocument.class
};
}

}

0 comments on commit da859e9

Please sign in to comment.