Fixing bug with search exclusion

This commit is contained in:
briancaruso 2012-06-27 17:04:58 +00:00
parent a26e329ed6
commit f997b7073c
5 changed files with 127 additions and 13 deletions

View file

@ -17,6 +17,9 @@ import edu.cornell.mannlib.vitro.webapp.beans.VClass;
*/
public class ExcludeBasedOnType implements SearchIndexExcluder {
private static final String SKIP_MSG = "skipping due to type.";
/** The add, set and remove methods must keep this list sorted. */
List<String> typeURIs;
public ExcludeBasedOnType(String ... typeURIs) {
@ -25,13 +28,31 @@ public class ExcludeBasedOnType implements SearchIndexExcluder {
@Override
public String checkForExclusion(Individual ind) {
if( ind != null ) {
List<VClass> vclasses = ind.getVClasses();
if( vclasses != null && ! Collections.disjoint(vclasses, typeURIs) ){
return("skipping due to type.");
}
}
if( ind == null )
return null;
if( typeURIinExcludeList( ind.getVClass() ))
return SKIP_MSG;
List<VClass> vclasses = ind.getVClasses();
if( vclasses == null)
return null;
for( VClass vclz : vclasses){
if( typeURIinExcludeList( vclz ))
return SKIP_MSG;
}
return null;
}
protected boolean typeURIinExcludeList( VClass vclz){
if( vclz != null && vclz.getURI() != null && !vclz.isAnonymous() ){
int pos = Collections.binarySearch(typeURIs, vclz.getURI());
return pos >= 0;
}else{
return false;
}
}
public void setExcludedTypes(String ... typeURIs){
@ -41,6 +62,7 @@ public class ExcludeBasedOnType implements SearchIndexExcluder {
public void setExcludedTypes(List<String> typeURIs){
synchronized(this){
this.typeURIs = new ArrayList<String>(typeURIs) ;
Collections.sort( this.typeURIs );
}
}
@ -48,6 +70,7 @@ public class ExcludeBasedOnType implements SearchIndexExcluder {
if( typeURI != null && !typeURI.isEmpty()){
synchronized(this){
typeURIs.add(typeURI);
Collections.sort( this.typeURIs );
}
}
}

View file

@ -49,7 +49,7 @@ public class IndividualToSolrDocument {
try{
String excludeMsg = checkExcludes( ind );
if( excludeMsg != DONT_EXCLUDE){
log.debug(excludeMsg);
log.debug(ind.getURI() + " " + excludeMsg);
return null;
}
@ -235,7 +235,7 @@ public class IndividualToSolrDocument {
List<VClass> vclasses = ind.getVClasses(false);
if( vclasses == null || vclasses.isEmpty() ){
throw new SkipIndividualException("Not indexing because individual has no super classes");
throw new SkipIndividualException("Not indexing because individual has no classes");
}
for(VClass clz : vclasses){

View file

@ -41,6 +41,7 @@ public class SyncingExcludeBasedOnType extends ExcludeBasedOnType implements Mod
public SyncingExcludeBasedOnType( Model model){
this.setExcludedTypes( buildProhibitedClassesList(searchIndexURI, model) );
log.info("types excluded from search: " + typeURIs);
}
private List<String> buildProhibitedClassesList( String URI, Model model){
@ -83,11 +84,13 @@ public class SyncingExcludeBasedOnType extends ExcludeBasedOnType implements Mod
if( s.getObject() != null && s.getObject().canAs(Resource.class)){
String classURI = ((Resource)s.getObject().as(Resource.class)).getURI();
this.addTypeToExclude(classURI);
log.debug("prohibited classes: " + this.typeURIs);
}
}
}catch(Exception ex){
log.error("could not add statement",ex);
}
}
@Override
@ -97,6 +100,7 @@ public class SyncingExcludeBasedOnType extends ExcludeBasedOnType implements Mod
if( s.getObject() != null && s.getObject().canAs(Resource.class)){
String classURI = ((Resource)s.getObject().as(Resource.class)).getURI();
this.removeTypeToExclude(classURI);
log.debug("prohibited classes: " + this.typeURIs);
}
}
}catch(Exception ex){

View file

@ -19,7 +19,7 @@ import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
public class ProhibitedFromSearchTest {
String SEARCH_CONFIG_URI = "http://example.com/TestSearchConfig";
String SEARCH_CONFIG_URI = DisplayVocabulary.SEARCH_INDEX_URI;
String TEST_CLASS = "http://vivoweb.org/ontology/test/bogus#Class5";
String n3 =

View file

@ -0,0 +1,87 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.search.solr.documentBuilding;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import edu.cornell.mannlib.vitro.webapp.beans.IndividualImpl;
import edu.cornell.mannlib.vitro.webapp.beans.VClass;
public class ExcludeBasedOnTypeTest {
@Test
public void testCheckForExclusion() {
ExcludeBasedOnType ebot = new ExcludeBasedOnType();
ebot.addTypeToExclude("http://xmlns.com/foaf/0.1/Person");
IndividualImpl ind = new IndividualImpl();
ind.setURI("http://example.com/n2343");
VClass personClass = new VClass("http://xmlns.com/foaf/0.1/Person");
ind.setVClass(personClass);
String excludeResult = ebot.checkForExclusion(ind);
assertNotNull( excludeResult );
}
@Test
public void testCheckForExclusion2() {
ExcludeBasedOnType ebot = new ExcludeBasedOnType();
ebot.addTypeToExclude("http://xmlns.com/foaf/0.1/KillerRobot");
IndividualImpl ind = new IndividualImpl();
ind.setURI("http://example.com/n2343");
VClass personClass = new VClass("http://xmlns.com/foaf/0.1/Agent");
ind.setVClass(personClass);
List<VClass> vClassList = new ArrayList<VClass>();
vClassList.add( new VClass("http://example.com/Robot"));
vClassList.add( new VClass("http://example.com/KillerRobot"));
vClassList.add( new VClass("http://example.com/Droid"));
ind.setVClasses(vClassList, true);
String excludeResult = ebot.checkForExclusion(ind);
assertNotNull( excludeResult );
}
@Test
public void testCheckForNonExclusion() {
ExcludeBasedOnType ebot = new ExcludeBasedOnType();
ebot.addTypeToExclude("http://xmlns.com/foaf/0.1/Person");
IndividualImpl ind = new IndividualImpl();
ind.setURI("http://example.com/n2343");
VClass personClass = new VClass("http://xmlns.com/foaf/0.1/Robot");
ind.setVClass(personClass);
String excludeResult = ebot.checkForExclusion(ind);
assertNull( excludeResult );
}
@Test
public void testCheckForNonExclusion2() {
ExcludeBasedOnType ebot = new ExcludeBasedOnType();
ebot.addTypeToExclude("http://xmlns.com/foaf/0.1/Person");
IndividualImpl ind = new IndividualImpl();
ind.setURI("http://example.com/n2343");
VClass personClass = new VClass("http://xmlns.com/foaf/0.1/Agent");
ind.setVClass(personClass);
List<VClass> vClassList = new ArrayList<VClass>();
vClassList.add( new VClass("http://example.com/Robot"));
vClassList.add( new VClass("http://example.com/KillerRobot"));
vClassList.add( new VClass("http://example.com/Droid"));
ind.setVClasses(vClassList, true);
String excludeResult = ebot.checkForExclusion(ind);
assertNull( excludeResult );
}
}