Changeset 4628


Ignore:
Timestamp:
03/06/14 10:58:13 (11 years ago)
Author:
Twan Goosen
Message:

Implemented getting getDocument(docId) in the solr document service implementation, and added unit tests.
For this, added method createDocumentQuery to document query factory interface (+ javadoc) and implemented.

Location:
vlo/branches/vlo-3.0/vlo-web-app/src
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/SolrDocumentQueryFactory.java

    r4566 r4628  
    1515 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616 */
    17 
    1817package eu.clarin.cmdi.vlo.service;
    1918
     19import eu.clarin.cmdi.vlo.FacetConstants;
    2020import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
    2121import org.apache.solr.client.solrj.SolrQuery;
     
    2727public interface SolrDocumentQueryFactory {
    2828
     29    /**
     30     * Creates a query to retrieve all document that match a query/facet
     31     * selection with an offset and limit
     32     *
     33     * @param selection selection criteria
     34     * @param first search result offset
     35     * @param count limits the number of results
     36     * @return a query set up to retrieve the matching documents, starting with
     37     * the specified starting index and limited to the specified count
     38     */
    2939    SolrQuery createDocumentQuery(QueryFacetsSelection selection, int first, int count);
    30    
     40
     41    /**
     42     * Creates a query to retrieve a single document by id
     43     *
     44     * @param docId identifier of document to retrieve
     45     * @return a query set up to retrieve one row at most with the document that
     46     * has the specified identifier
     47     * @see FacetConstants#FIELD_ID
     48     */
     49    SolrQuery createDocumentQuery(String docId);
     50
    3151}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/AbstractSolrQueryFactory.java

    r4566 r4628  
    3131public abstract class AbstractSolrQueryFactory {
    3232
    33     private static final String SOLR_SEARCH_ALL = "*:*";
     33    protected static final String SOLR_SEARCH_ALL = "*:*";
    3434
    35     protected void addQueryFacetParameters(final SolrQuery query, QueryFacetsSelection queryFacetsSelections) {
     35    protected final void addQueryFacetParameters(final SolrQuery query, QueryFacetsSelection queryFacetsSelections) {
    3636        final String queryString = queryFacetsSelections.getQuery();
    3737        if (queryString == null) {
     
    4848                if (values != null) {
    4949                    for (String value : values) {
    50                         encodedQueries.add(String.format("%s:%s", facetName, ClientUtils.escapeQueryChars(value)));
     50                        encodedQueries.add(createFilterQuery(facetName, value));
    5151                    }
    5252                }
     
    5656    }
    5757
     58    protected final String createFilterQuery(String facetName, String value) {
     59        return String.format("%s:%s", facetName, ClientUtils.escapeQueryChars(value));
     60    }
     61
    5862}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/SolrDocumentQueryFactoryImpl.java

    r4566 r4628  
    1515 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1616 */
    17 
    1817package eu.clarin.cmdi.vlo.service.impl;
    1918
     
    2726 * @author twagoo
    2827 */
    29 public class SolrDocumentQueryFactoryImpl extends AbstractSolrQueryFactory implements SolrDocumentQueryFactory{
     28public class SolrDocumentQueryFactoryImpl extends AbstractSolrQueryFactory implements SolrDocumentQueryFactory {
    3029   
    3130    @Override
     
    3837    }
    3938   
     39    @Override
     40    public SolrQuery createDocumentQuery(String docId) {
     41        final SolrQuery query = getDefaultDocumentQuery();
     42        query.setQuery(SOLR_SEARCH_ALL);
     43        query.addFilterQuery(createFilterQuery(FacetConstants.FIELD_ID, docId));
     44        query.setRows(1);
     45        return query;
     46       
     47    }
     48   
    4049    private SolrQuery getDefaultDocumentQuery() {
    4150        SolrQuery query = new SolrQuery();
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/service/impl/SolrDocumentServiceImpl.java

    r4626 r4628  
    2424import org.apache.solr.client.solrj.SolrQuery;
    2525import org.apache.solr.common.SolrDocument;
     26import org.apache.solr.common.SolrDocumentList;
     27import org.slf4j.Logger;
     28import org.slf4j.LoggerFactory;
    2629
    2730/**
     
    3134public class SolrDocumentServiceImpl implements SolrDocumentService {
    3235
     36    private final static Logger logger = LoggerFactory.getLogger(SolrDocumentServiceImpl.class);
    3337    private final SearchResultsDao searchResultsDao;
    3438    private final SolrDocumentQueryFactory queryFatory;
     
    4145    @Override
    4246    public SolrDocument getDocument(String docId) {
    43         throw new UnsupportedOperationException("Not supported yet.");
     47        final SolrQuery query = queryFatory.createDocumentQuery(docId);
     48        final SolrDocumentList result = searchResultsDao.getDocuments(query);
     49        if (result.size() < 1) {
     50            return null;
     51        } else {
     52            logger.debug("Document with docId {} retrieved:", result);
     53            return result.get(0);
     54        }
    4455    }
    4556
    4657    @Override
    4758    public List<SolrDocument> getDocuments(QueryFacetsSelection selection, int first, int count) {
    48         SolrQuery query = queryFatory.createDocumentQuery(selection,first, count);
     59        final SolrQuery query = queryFatory.createDocumentQuery(selection, first, count);
    4960        return searchResultsDao.getDocuments(query);
    5061    }
     
    5263    @Override
    5364    public long getDocumentCount(QueryFacetsSelection selection) {
    54         SolrQuery query = queryFatory.createDocumentQuery(selection,0,0);
     65        final SolrQuery query = queryFatory.createDocumentQuery(selection, 0, 0);
    5566        return searchResultsDao.getDocuments(query).getNumFound();
    5667    }
Note: See TracChangeset for help on using the changeset viewer.