From f997b7073c23dc30c5ff7cb43bd44e21ebde4e1b Mon Sep 17 00:00:00 2001 From: briancaruso Date: Wed, 27 Jun 2012 17:04:58 +0000 Subject: [PATCH] Fixing bug with search exclusion --- .../documentBuilding/ExcludeBasedOnType.java | 39 +++++++-- .../IndividualToSolrDocument.java | 4 +- .../SyncingExcludeBasedOnType.java | 8 +- .../beans/ProhibitedFromSearchTest.java | 2 +- .../ExcludeBasedOnTypeTest.java | 87 +++++++++++++++++++ 5 files changed, 127 insertions(+), 13 deletions(-) create mode 100644 webapp/test/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnTypeTest.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnType.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnType.java index 3d9515143..b2b6094bb 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnType.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnType.java @@ -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 typeURIs; public ExcludeBasedOnType(String ... typeURIs) { @@ -25,22 +28,41 @@ public class ExcludeBasedOnType implements SearchIndexExcluder { @Override public String checkForExclusion(Individual ind) { - if( ind != null ) { - List 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 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){ setExcludedTypes(Arrays.asList(typeURIs)); } public void setExcludedTypes(List typeURIs){ synchronized(this){ - this.typeURIs = new ArrayList(typeURIs); + this.typeURIs = new ArrayList(typeURIs) ; + Collections.sort( this.typeURIs ); } } @@ -48,13 +70,14 @@ public class ExcludeBasedOnType implements SearchIndexExcluder { if( typeURI != null && !typeURI.isEmpty()){ synchronized(this){ typeURIs.add(typeURI); + Collections.sort( this.typeURIs ); } } } protected void removeTypeToExclude(String typeURI){ synchronized(this){ - typeURIs.remove(typeURI); + typeURIs.remove(typeURI); } } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/IndividualToSolrDocument.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/IndividualToSolrDocument.java index 3317506c8..65fc13f4c 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/IndividualToSolrDocument.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/IndividualToSolrDocument.java @@ -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 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){ diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/SyncingExcludeBasedOnType.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/SyncingExcludeBasedOnType.java index cccc6fe6e..3dc3f5dde 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/SyncingExcludeBasedOnType.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/SyncingExcludeBasedOnType.java @@ -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 buildProhibitedClassesList( String URI, Model model){ @@ -68,7 +69,7 @@ public class SyncingExcludeBasedOnType extends ExcludeBasedOnType implements Mod }catch(Throwable t){ log.error(t,t); }finally{ qExec.close(); } - }finally{ model.leaveCriticalSection(); } + }finally{ model.leaveCriticalSection(); } return newProhibitedClasses; } @@ -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 @@ -96,7 +99,8 @@ public class SyncingExcludeBasedOnType extends ExcludeBasedOnType implements Mod if( isExcludeClassPredicate( s ) && isAboutSearchIndex(s)){ if( s.getObject() != null && s.getObject().canAs(Resource.class)){ String classURI = ((Resource)s.getObject().as(Resource.class)).getURI(); - this.removeTypeToExclude(classURI); + this.removeTypeToExclude(classURI); + log.debug("prohibited classes: " + this.typeURIs); } } }catch(Exception ex){ diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearchTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearchTest.java index 1cd9f5c9e..5dd72408d 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearchTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/beans/ProhibitedFromSearchTest.java @@ -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 = diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnTypeTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnTypeTest.java new file mode 100644 index 000000000..710cc01b7 --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/search/solr/documentBuilding/ExcludeBasedOnTypeTest.java @@ -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 vClassList = new ArrayList(); + 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 vClassList = new ArrayList(); + 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 ); + } +}