Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * PODD is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.search.impl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.QueryResponse; import org.springframework.util.StringUtils; import podd.search.IndexSearcher; import podd.search.SearchCriteria; import podd.search.SearchResponse; /** * API for searching the index * @author Philip Wu * */ public class IndexSearcherImpl implements IndexSearcher { protected static final Logger LOGGER = LoggerFactory.getLogger(IndexSearcherImpl.class); /** * The connection configuration to the solr server */ protected SolrServer solrServer; public SolrServer getSolrServer() { return solrServer; } public void setSolrServer(SolrServer solrServer) { this.solrServer = solrServer; } @Override public SearchResponse search(SearchCriteria criteria) { SolrQuery solrQuery = createQuery(criteria); // Only perform a search if there is a valid query if (StringUtils.hasText(solrQuery.getQuery())) { try { QueryResponse queryResponse = solrServer.query(solrQuery); return new SearchResponseImpl(queryResponse, criteria.getPageSize(), solrQuery.getHighlightFragsize()); } catch (SolrServerException e) { LOGGER.error("Found exception", e); } } return null; } /** * Helper method to create SolrQuery with common configuration * @return */ protected SolrQuery createQuery(SearchCriteria criteria) { Integer start = (criteria.getStart() != null) ? criteria.getStart().intValue() : null; SolrQuery solrQuery = createBasicSolrQuery(); //solrQuery.setParam("hl.requireFieldMatch", "true"); solrQuery.setQuery(criteria.getQuery()); solrQuery.setStart(start); solrQuery.setRows(criteria.getPageSize()); solrQuery.setFilterQueries(criteria.getQueryFilter()); return solrQuery; } /** * Creates a new SolrQuery with some predefined parameters * @return */ private SolrQuery createBasicSolrQuery() { SolrQuery solrQuery = new SolrQuery(); solrQuery.setHighlight(true).setHighlightSnippets(1); //set other params as needed solrQuery.setParam("hl.fl", "*"); return solrQuery; } }