001/* 002 * Copyright 2014 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.cms.search.solr; 017 018import java.util.Collections; 019import java.util.Map; 020 021import org.apache.solr.client.solrj.response.QueryResponse; 022import org.apache.solr.common.SolrDocumentList; 023import org.slf4j.LoggerFactory; 024 025import org.ametys.cms.search.SearchResult; 026import org.ametys.cms.search.SearchResults; 027import org.ametys.cms.search.SearchResultsIterable; 028import org.ametys.plugins.repository.AmetysObject; 029import org.ametys.plugins.repository.AmetysObjectIterable; 030import org.ametys.plugins.repository.AmetysObjectIterator; 031import org.ametys.plugins.repository.AmetysObjectResolver; 032 033/** 034 * Solr implementation of a {@link SearchResults}. 035 * @param <A> the actual type of {@link AmetysObject}s. 036 */ 037public class SolrSearchResults<A extends AmetysObject> implements SearchResults<A> 038{ 039 040 /** The solr query response */ 041 protected QueryResponse _response; 042 043 /** The Solr documents of the search response */ 044 protected SolrDocumentList _docList; 045 046 /** The AmetysObject resolver. */ 047 protected AmetysObjectResolver _resolver; 048 049 /** The facet results. */ 050 protected Map<String, Map<String, Integer>> _facetResults; 051 052 /** 053 * Build a SolrSearchResults object. 054 * @param response the solr search response. 055 * @param resolver The Ametys object resolver. 056 * @param facetResults The facet results. 057 */ 058 public SolrSearchResults(QueryResponse response, AmetysObjectResolver resolver, Map<String, Map<String, Integer>> facetResults) 059 { 060 _response = response; 061 _docList = response.getResults(); 062 _resolver = resolver; 063 _facetResults = facetResults; 064 } 065 066 @Override 067 public SearchResultsIterable<SearchResult<A>> getResults() 068 { 069 return new SolrResponseIterable<>(_docList, _resolver); 070 } 071 072 @Override 073 public AmetysObjectIterable<A> getObjects() 074 { 075 return new AmetysObjectIterable<A>() 076 { 077 @Override 078 public long getSize() 079 { 080 return _docList.size(); 081 } 082 083 @Override 084 public AmetysObjectIterator<A> iterator() 085 { 086 return new SolrResponseAmetysObjectIterator<>(_docList.iterator(), _docList.size(), _resolver, LoggerFactory.getLogger(SolrResponseAmetysObjectIterator.class)); 087 } 088 089 @Override 090 public void close() 091 { 092 // Nothing to do. 093 } 094 }; 095 } 096 097 @Override 098 public Iterable<String> getObjectIds() 099 { 100 return new SolrResponseIdIterable(_docList); 101 } 102 103 @Override 104 public Map<String, Map<String, Integer>> getFacetResults() 105 { 106 return Collections.unmodifiableMap(_facetResults); 107 } 108 109 @Override 110 public long getTotalCount() 111 { 112 return _docList.getNumFound(); 113 } 114 115 @Override 116 public float getMaxScore() 117 { 118 return _docList.getMaxScore(); 119 } 120}