Skip to content

Commit

Permalink
Extract the common ground between the Fulltext analyzer and updater t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
chrisvest committed Sep 20, 2017
1 parent c646c13 commit d4ca73e
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 137 deletions.
Expand Up @@ -21,48 +21,21 @@

import org.apache.lucene.analysis.en.EnglishAnalyzer;
import org.apache.lucene.analysis.sv.SwedishAnalyzer;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.time.Clock;

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.NullLog;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FulltextIndexType.NODES;

public class FulltextAnalyzerTest
public class FulltextAnalyzerTest extends LuceneFulltextTestSupport
{
private static final Label LABEL = Label.label( "label" );
private static final NullLog LOG = NullLog.getInstance();

@Rule
public DatabaseRule dbRule = new EmbeddedDatabaseRule().startLazily();

private AvailabilityGuard availabilityGuard = new AvailabilityGuard( Clock.systemDefaultZone(), LOG );

@Test
public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
{
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
JobScheduler scheduler = dbRule.resolveDependency( JobScheduler.class );
FileSystemAbstraction fs = dbRule.resolveDependency( FileSystemAbstraction.class );
File storeDir = dbRule.getStoreDir();
FulltextFactory fulltextFactory = new FulltextFactory( fs, storeDir, new EnglishAnalyzer() );

try ( FulltextProvider provider = new FulltextProvider( db, LOG, availabilityGuard, scheduler ) )
try ( FulltextProvider provider = createProvider() )
{
fulltextFactory.createFulltextIndex( "bloomNodes", NODES, singletonList( "prop" ), provider );
provider.init();
Expand All @@ -71,8 +44,8 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
long secondID;
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( LABEL );
Node node2 = db.createNode( LABEL );
Node node = db.createNode();
Node node2 = db.createNode();
firstID = node.getId();
secondID = node2.getId();
node.setProperty( "prop", "Hello and hello again, in the end." );
Expand All @@ -83,27 +56,21 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", NODES ) )
{

assertFalse( reader.query( "and" ).hasNext() );
assertFalse( reader.query( "in" ).hasNext() );
assertFalse( reader.query( "the" ).hasNext() );
assertEquals( secondID, reader.query( "en" ).next() );
assertEquals( secondID, reader.query( "och" ).next() );
assertEquals( secondID, reader.query( "ett" ).next() );
assertExactQueryFindsNothing( reader, "and" );
assertExactQueryFindsNothing( reader, "in" );
assertExactQueryFindsNothing( reader, "the" );
assertExactQueryFindsIds( reader, "en", secondID );
assertExactQueryFindsIds( reader, "och", secondID );
assertExactQueryFindsIds( reader, "ett", secondID );
}
}
}

@Test
public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
{
GraphDatabaseAPI db = dbRule.getGraphDatabaseAPI();
JobScheduler scheduler = dbRule.resolveDependency( JobScheduler.class );
FileSystemAbstraction fs = dbRule.resolveDependency( FileSystemAbstraction.class );
File storeDir = dbRule.getStoreDir();
FulltextFactory fulltextFactory = new FulltextFactory( fs, storeDir, new SwedishAnalyzer() );

try ( FulltextProvider provider = new FulltextProvider( db, LOG, availabilityGuard, scheduler ); )
try ( FulltextProvider provider = createProvider(); )
{
fulltextFactory.createFulltextIndex( "bloomNodes", NODES, singletonList( "prop" ), provider );
provider.init();
Expand All @@ -112,8 +79,8 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
long secondID;
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( LABEL );
Node node2 = db.createNode( LABEL );
Node node = db.createNode();
Node node2 = db.createNode();
firstID = node.getId();
secondID = node2.getId();
node.setProperty( "prop", "Hello and hello again, in the end." );
Expand All @@ -124,12 +91,12 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception

try ( ReadOnlyFulltext reader = provider.getReader( "bloomNodes", NODES ) )
{
assertEquals( firstID, reader.query( "and" ).next() );
assertEquals( firstID, reader.query( "in" ).next() );
assertEquals( firstID, reader.query( "the" ).next() );
assertFalse( reader.query( "en" ).hasNext() );
assertFalse( reader.query( "och" ).hasNext() );
assertFalse( reader.query( "ett" ).hasNext() );
assertExactQueryFindsIds( reader, "and", firstID );
assertExactQueryFindsIds( reader, "in", firstID );
assertExactQueryFindsIds( reader, "the", firstID );
assertExactQueryFindsNothing( reader, "en" );
assertExactQueryFindsNothing( reader, "och" );
assertExactQueryFindsNothing( reader, "ett" );
}
}
}
Expand Down
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2002-2017 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.api.impl.fulltext;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.junit.Before;
import org.junit.Rule;

import java.io.File;
import java.time.Clock;

import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongSet;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.Log;
import org.neo4j.logging.NullLog;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;

import static org.junit.Assert.assertTrue;

public class LuceneFulltextTestSupport
{
protected static final StandardAnalyzer ANALYZER = new StandardAnalyzer();
protected static final Log LOG = NullLog.getInstance();

@Rule
public DatabaseRule dbRule = new EmbeddedDatabaseRule().startLazily();

protected static final RelationshipType RELTYPE = RelationshipType.withName( "type" );

protected AvailabilityGuard availabilityGuard = new AvailabilityGuard( Clock.systemDefaultZone(), LOG );
protected GraphDatabaseAPI db;
protected FulltextFactory fulltextFactory;
protected JobScheduler scheduler;
protected FileSystemAbstraction fs;
protected File storeDir;

@Before
public void setUp() throws Exception
{
db = dbRule.getGraphDatabaseAPI();
scheduler = dbRule.resolveDependency( JobScheduler.class );
fs = dbRule.resolveDependency( FileSystemAbstraction.class );
storeDir = dbRule.getStoreDir();
fulltextFactory = new FulltextFactory( fs, storeDir, ANALYZER );
}

protected FulltextProvider createProvider()
{
return new FulltextProvider( db, LOG, availabilityGuard, scheduler );
}

protected void assertExactQueryFindsNothing( ReadOnlyFulltext reader, String query )
{
assertExactQueryFindsIds( reader, query );
}

protected void assertExactQueryFindsIds( ReadOnlyFulltext reader, String[] query, long... ids )
{
PrimitiveLongIterator result = reader.query( query );
assertQueryResultsMatch( result, ids );
}

protected void assertExactQueryFindsIds( ReadOnlyFulltext reader, String query, long... ids )
{
assertExactQueryFindsIds( reader, new String[]{query}, ids );
}

protected void assertFuzzyQueryFindsIds( ReadOnlyFulltext reader, String query, long... ids )
{
PrimitiveLongIterator result = reader.fuzzyQuery( query );
assertQueryResultsMatch( result, ids );
}

protected void assertQueryResultsMatch( PrimitiveLongIterator result, long[] ids )
{
PrimitiveLongSet set = PrimitiveLongCollections.setOf( ids );
while ( result.hasNext() )
{
assertTrue( set.remove( result.next() ) );
}
assertTrue( set.isEmpty() );
}

protected void setNodeProp( long nodeId, String value )
{
try ( Transaction tx = db.beginTx() )
{
Node node = db.getNodeById( nodeId );
node.setProperty( "prop", value );
tx.success();
}
}
}
Expand Up @@ -19,61 +19,24 @@
*/
package org.neo4j.kernel.api.impl.fulltext;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.time.Clock;
import java.util.Arrays;

import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.collection.primitive.PrimitiveLongSet;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.AvailabilityGuard;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.logging.Log;
import org.neo4j.logging.NullLog;
import org.neo4j.scheduler.JobScheduler;
import org.neo4j.test.rule.DatabaseRule;
import org.neo4j.test.rule.EmbeddedDatabaseRule;

import static java.util.Collections.singletonList;
import static org.junit.Assert.assertTrue;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FulltextIndexType.NODES;
import static org.neo4j.kernel.api.impl.fulltext.FulltextProvider.FulltextIndexType.RELATIONSHIPS;

public class LuceneFulltextUpdaterTest
public class LuceneFulltextUpdaterTest extends LuceneFulltextTestSupport
{
public static final StandardAnalyzer ANALYZER = new StandardAnalyzer();
private static final Log LOG = NullLog.getInstance();

@Rule
public DatabaseRule dbRule = new EmbeddedDatabaseRule().startLazily();

private static final RelationshipType RELTYPE = RelationshipType.withName( "type" );

private AvailabilityGuard availabilityGuard = new AvailabilityGuard( Clock.systemDefaultZone(), LOG );
private GraphDatabaseAPI db;
private FulltextFactory fulltextFactory;
private JobScheduler scheduler;

@Before
public void setUp() throws Exception
{
db = dbRule.getGraphDatabaseAPI();
scheduler = dbRule.resolveDependency( JobScheduler.class );
FileSystemAbstraction fs = dbRule.resolveDependency( FileSystemAbstraction.class );
File storeDir = dbRule.getStoreDir();
fulltextFactory = new FulltextFactory( fs, storeDir, ANALYZER );
}

@Test
public void shouldFindNodeWithString() throws Exception
{
Expand Down Expand Up @@ -699,51 +662,4 @@ public void shouldReturnMatchesThatContainLuceneSyntaxCharacters() throws Except
}
}
}

private FulltextProvider createProvider()
{
return new FulltextProvider( db, LOG, availabilityGuard, scheduler );
}

private void assertExactQueryFindsNothing( ReadOnlyFulltext reader, String query )
{
assertExactQueryFindsIds( reader, query );
}

private void assertExactQueryFindsIds( ReadOnlyFulltext reader, String[] query, long... ids )
{
PrimitiveLongIterator result = reader.query( query );
assertQueryResultsMatch( result, ids );
}

private void assertExactQueryFindsIds( ReadOnlyFulltext reader, String query, long... ids )
{
assertExactQueryFindsIds( reader, new String[]{query}, ids );
}

private void assertFuzzyQueryFindsIds( ReadOnlyFulltext reader, String query, long... ids )
{
PrimitiveLongIterator result = reader.fuzzyQuery( query );
assertQueryResultsMatch( result, ids );
}

private void assertQueryResultsMatch( PrimitiveLongIterator result, long[] ids )
{
PrimitiveLongSet set = PrimitiveLongCollections.setOf( ids );
while ( result.hasNext() )
{
assertTrue( set.remove( result.next() ) );
}
assertTrue( set.isEmpty() );
}

private void setNodeProp( long nodeId, String value )
{
try ( Transaction tx = db.beginTx() )
{
Node node = db.getNodeById( nodeId );
node.setProperty( "prop", value );
tx.success();
}
}
}

0 comments on commit d4ca73e

Please sign in to comment.