org.apache.jetspeed.search.solr.SolrSearchEngineImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jetspeed-search Show documentation
Show all versions of jetspeed-search Show documentation
Jetspeed-2 Search Component
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jetspeed.search.solr;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.lang.StringUtils;
import org.apache.jetspeed.search.BaseParsedObject;
import org.apache.jetspeed.search.HandlerFactory;
import org.apache.jetspeed.search.ObjectHandler;
import org.apache.jetspeed.search.ParsedObject;
import org.apache.jetspeed.search.SearchEngine;
import org.apache.jetspeed.search.SearchResults;
import org.apache.jetspeed.search.SearchResultsImpl;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.SolrInputField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @version $Id: SolrSearchEngineImpl.java 1086464 2011-03-29 02:08:39Z woonsan $
*/
public class SolrSearchEngineImpl implements SearchEngine
{
private final static Logger log = LoggerFactory.getLogger(SolrSearchEngineImpl.class);
private SolrServer server;
private boolean optimizeAfterUpdate = true;
private HandlerFactory handlerFactory;
private Set searchableMetadataFieldNames =
new HashSet(Arrays.asList(
"ID",
"url",
"portlet",
"portlet_application",
"subject",
"creator",
"publisher",
"title",
"fieldname.title",
"contributor",
"description",
"fieldname.description"
));
public SolrSearchEngineImpl(SolrServer server, boolean optimzeAfterUpdate, HandlerFactory handlerFactory)
{
this.server = server;
this.optimizeAfterUpdate = optimzeAfterUpdate;
this.handlerFactory = handlerFactory;
}
public Set getSearchableMetadataFieldNames()
{
return searchableMetadataFieldNames;
}
public void setSearchableMetadataFieldNames(Set searchableMetadataFieldNames)
{
this.searchableMetadataFieldNames = searchableMetadataFieldNames;
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#add(java.lang.Object)
*/
public boolean add(Object o)
{
Collection c = new ArrayList(1);
c.add(o);
return add(c);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#add(java.util.Collection)
*/
public boolean add(Collection objects)
{
return removeIfExistsAndAdd(objects);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#remove(java.lang.Object)
*/
public boolean remove(Object o)
{
Collection c = new ArrayList(1);
c.add(o);
return remove(c);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#remove(java.util.Collection)
*/
public synchronized boolean remove(Collection objects)
{
int deleteCount = 0;
try
{
Iterator it = objects.iterator();
while (it.hasNext())
{
Object o = it.next();
// Look up appropriate handler
ObjectHandler handler = handlerFactory.getHandler(o);
// Parse the object
ParsedObject parsedObject = handler.parseObject(o);
if (parsedObject.getKey() != null)
{
String queryString = new StringBuilder(40).append(ParsedObject.FIELDNAME_KEY).append(':').append(ClientUtils.escapeQueryChars(parsedObject.getKey())).toString();
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
QueryResponse qrsp = server.query(query);
int count = qrsp.getResults().size();
if (count > 0)
{
// Remove the document from search index
UpdateResponse rsp = server.deleteByQuery(queryString);
if (rsp.getStatus() < 300)
{
deleteCount += count;
}
}
}
}
if (deleteCount > 0)
{
server.commit();
if (optimizeAfterUpdate) {
server.optimize();
}
}
}
catch (Exception e)
{
log.error("Exception during removing documents in the search index.", e);
}
return deleteCount > 0;
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#update(java.lang.Object)
*/
public boolean update(Object o)
{
Collection c = new ArrayList(1);
c.add(o);
return update(c);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEnging#update(java.util.Collection)
*/
public boolean update(Collection objects)
{
return removeIfExistsAndAdd(objects);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEngine#search(java.lang.String)
*/
public SearchResults search(String queryString)
{
return search(queryString, ParsedObject.FIELDNAME_SYNTHETIC);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEngine#search(java.lang.String, java.lang.String)
*/
public SearchResults search(String queryString, String defaultFieldName)
{
return search(queryString, defaultFieldName, 0);
}
/* (non-Javadoc)
* @see org.apache.jetspeed.search.SearchEngine#search(java.lang.String, java.lang.String, int)
*/
public SearchResults search(String queryString, String defaultFieldName, int topHitsCount)
{
SearchResults results = null;
try
{
SolrQuery query = new SolrQuery();
query.setQuery(queryString);
QueryResponse rsp = server.query(query);
SolrDocumentList docList = rsp.getResults();
List resultList = new ArrayList();
for (SolrDocument doc : docList)
{
ParsedObject result = new BaseParsedObject();
addFieldsToParsedObject(doc, result);
Object type = doc.getFirstValue(ParsedObject.FIELDNAME_TYPE);
if(type != null)
{
result.setType(type.toString());
}
Object key = doc.getFirstValue(ParsedObject.FIELDNAME_KEY);
if(key != null)
{
result.setKey(key.toString());
}
Object description = doc.getFirstValue(ParsedObject.FIELDNAME_DESCRIPTION);
if(description != null)
{
result.setDescription(description.toString());
}
Object title = doc.getFirstValue(ParsedObject.FIELDNAME_TITLE);
if(title != null)
{
result.setTitle(title.toString());
}
Object content = doc.getFirstValue(ParsedObject.FIELDNAME_CONTENT);
if(content != null)
{
result.setContent(content.toString());
}
Object language = doc.getFirstValue(ParsedObject.FIELDNAME_LANGUAGE);
if (language != null)
{
result.setLanguage(language.toString());
}
Object classname = doc.getFirstValue(ParsedObject.FIELDNAME_CLASSNAME);
if (classname != null)
{
result.setClassName(classname.toString());
}
Object url = doc.getFirstValue(ParsedObject.FIELDNAME_URL);
if (url != null)
{
result.setURL(new URL(url.toString()));
}
Collection