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
The newest version!
/*
* 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 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;
import java.io.File;
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;
/**
* @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"
));
private Boolean documentsEnabled = false;
private String documentsLocation = null;
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;
}
public Boolean getDocumentsEnabled() {
return documentsEnabled;
}
public void setDocumentsEnabled(Boolean documentsEnabled) {
this.documentsEnabled = documentsEnabled;
}
public String getDocumentsLocation() {
return documentsLocation;
}
public void setDocumentsLocation(String documentsLocation) {
this.documentsLocation = documentsLocation;
}
/* (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);
}
@Override
public void indexDirectory(String dir) throws IOException {
Collection