Skip to content

Commit

Permalink
Various polish for fulltext addon
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Sep 12, 2017
1 parent 8dce836 commit 45fee03
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 52 deletions.
Expand Up @@ -73,10 +73,10 @@ private IndexWriterConfigs()
public static IndexWriterConfig standard()
{
Analyzer analyzer = LuceneDataSource.KEYWORD_ANALYZER;
return getIndexWriterConfigWithAnalyzer( analyzer );
return standard( analyzer );
}

private static IndexWriterConfig getIndexWriterConfigWithAnalyzer( Analyzer analyzer )
public static IndexWriterConfig standard( Analyzer analyzer )
{
IndexWriterConfig writerConfig = new IndexWriterConfig( analyzer );

Expand Down Expand Up @@ -118,7 +118,7 @@ public static IndexWriterConfig population()

public static IndexWriterConfig population( Analyzer analyzer )
{
IndexWriterConfig writerConfig = getIndexWriterConfigWithAnalyzer( analyzer );
IndexWriterConfig writerConfig = standard( analyzer );
writerConfig.setMaxBufferedDocs( POPULATION_MAX_BUFFERED_DOCS );
writerConfig.setRAMBufferSizeMB( POPULATION_RAM_BUFFER_SIZE_MB );
return writerConfig;
Expand Down
Expand Up @@ -24,24 +24,23 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;

import org.neo4j.function.Factory;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.kernel.api.impl.index.IndexWriterConfigs;
import org.neo4j.kernel.api.impl.index.builder.LuceneIndexStorageBuilder;
import org.neo4j.kernel.api.impl.index.partition.WritableIndexPartitionFactory;

import static org.neo4j.kernel.api.impl.index.LuceneKernelExtensions.directoryFactory;

/**
* Used for creating {@link LuceneFulltext} and registering those to a {@link FulltextProvider}.
*/
public class FulltextFactory
{
public static final String INDEX_DIR = "fulltext";
private final FileSystemAbstraction fileSystem;
private final WritableIndexPartitionFactory partitionFactory;
private final File storeDir;
private final File indexDir;
private final Analyzer analyzer;

/**
Expand All @@ -55,9 +54,9 @@ public FulltextFactory( FileSystemAbstraction fileSystem, File storeDir, Analyze
{
this.analyzer = analyzer;
this.fileSystem = fileSystem;
Factory<IndexWriterConfig> population = () -> IndexWriterConfigs.population( analyzer );
partitionFactory = new WritableIndexPartitionFactory( population );
this.storeDir = storeDir;
Factory<IndexWriterConfig> indexWriterConfigFactory = () -> IndexWriterConfigs.standard( analyzer );
partitionFactory = new WritableIndexPartitionFactory( indexWriterConfigFactory );
indexDir = new File( storeDir, INDEX_DIR );
}

/**
Expand All @@ -68,12 +67,11 @@ public FulltextFactory( FileSystemAbstraction fileSystem, File storeDir, Analyze
* @param provider The provider to register with
* @throws IOException
*/
public void createFulltextIndex( String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type, String[] properties, FulltextProvider provider )
public void createFulltextIndex( String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type, List<String> properties, FulltextProvider provider )
throws IOException
{
LuceneIndexStorageBuilder storageBuilder = LuceneIndexStorageBuilder.create();
storageBuilder.withFileSystem( fileSystem ).withIndexIdentifier( identifier ).withDirectoryFactory(
directoryFactory( false, this.fileSystem ) ).withIndexRootFolder( Paths.get( this.storeDir.getAbsolutePath(), "fulltext" ).toFile() );
storageBuilder.withFileSystem( fileSystem ).withIndexIdentifier( identifier ).withIndexRootFolder( indexDir );
provider.register( new LuceneFulltext( storageBuilder.build(), partitionFactory, properties, analyzer, identifier, type ) );
}
}
Expand Up @@ -57,7 +57,7 @@ private FulltextProvider( GraphDatabaseService db )
/**
* Fetch the current instance of the provider. If there is none, create one associated with the given database.
* @param db Database used for eventual creation of the provider.
* @return
* @return The instance for the given database.
*/
public static synchronized FulltextProvider instance( GraphDatabaseService db )
{
Expand Down
Expand Up @@ -22,7 +22,6 @@
import org.apache.lucene.analysis.Analyzer;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
Expand All @@ -42,21 +41,16 @@

class LuceneFulltext extends AbstractLuceneIndex
{

private static final String KEY_STATUS = "status";
private static final String ONLINE = "online";
private static final Map<String,String> ONLINE_COMMIT_USER_DATA = singletonMap( KEY_STATUS, ONLINE );
private final Analyzer analyzer;
private final String identifier;
private final FulltextProvider.FULLTEXT_INDEX_TYPE type;
private final TaskCoordinator taskCoordinator = new TaskCoordinator( 10, TimeUnit.MILLISECONDS );
private final Set<String> properties;

LuceneFulltext( PartitionedIndexStorage indexStorage, IndexPartitionFactory partitionFactory, String[] properties, Analyzer analyzer,
LuceneFulltext( PartitionedIndexStorage indexStorage, IndexPartitionFactory partitionFactory, List<String> properties, Analyzer analyzer,
String identifier, FulltextProvider.FULLTEXT_INDEX_TYPE type )
{
super( indexStorage, partitionFactory );
this.properties = Collections.unmodifiableSet( new HashSet<>( Arrays.asList( properties ) ) );
this.properties = Collections.unmodifiableSet( new HashSet<>( properties ) );
this.analyzer = analyzer;
this.identifier = identifier;
this.type = type;
Expand Down
Expand Up @@ -23,6 +23,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.io.fs.FileSystemAbstraction;
Expand Down Expand Up @@ -53,17 +54,8 @@ class BloomKernelExtension extends LifecycleAdapter
@Override
public void init() throws IOException, ProcedureException
{
String[] properties = config.get( LoadableBloomFulltextConfig.bloom_indexed_properties ).toArray( new String[0] );
Analyzer analyzer;
try
{
Class configuredAnalayzer = Class.forName( config.get( LoadableBloomFulltextConfig.bloom_analyzer ) );
analyzer = (Analyzer) configuredAnalayzer.newInstance();
}
catch ( Exception e )
{
throw new RuntimeException( "Could not create the configured analyzer", e );
}
List<String> properties = config.get( LoadableBloomFulltextConfig.bloom_indexed_properties );
Analyzer analyzer = getAnalyzer();

FulltextProvider provider = FulltextProvider.instance( db );
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemAbstraction, storeDir, analyzer );
Expand All @@ -76,6 +68,21 @@ public void init() throws IOException, ProcedureException
procedures.register( new BloomProcedure( FulltextProvider.FULLTEXT_INDEX_TYPE.RELATIONSHIPS, bloomRelationships, provider ) );
}

private Analyzer getAnalyzer()
{
Analyzer analyzer;
try
{
Class configuredAnalayzer = Class.forName( config.get( LoadableBloomFulltextConfig.bloom_analyzer ) );
analyzer = (Analyzer) configuredAnalayzer.newInstance();
}
catch ( Exception e )
{
throw new RuntimeException( "Could not create the configured analyzer", e );
}
return analyzer;
}

@Override
public void shutdown() throws Exception
{
Expand Down
Expand Up @@ -44,7 +44,7 @@ public class BloomProcedure extends CallableProcedure.BasicProcedure
private static final String[] PROCEDURE_NAMESPACE = {"db", "fulltext"};
private final String identifier;
private final FulltextProvider provider;
private FulltextProvider.FULLTEXT_INDEX_TYPE type;
private final FulltextProvider.FULLTEXT_INDEX_TYPE type;

/**
* Creates a procedure for querying the bloom fulltext addon.
Expand Down
Expand Up @@ -25,6 +25,8 @@
import org.junit.Rule;
import org.junit.Test;

import java.util.Arrays;

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
Expand Down Expand Up @@ -56,7 +58,7 @@ public void shouldBeAbleToSpecifyEnglishAnalyzer() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new EnglishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -92,7 +94,7 @@ public void shouldBeAbleToSpecifySwedishAnalyzer() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), new SwedishAnalyzer() );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "bloomNodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down
Expand Up @@ -24,6 +24,8 @@
import org.junit.Rule;
import org.junit.Test;

import java.util.Arrays;

import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
Expand Down Expand Up @@ -61,7 +63,7 @@ public void shouldFindNodeWithString() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -96,7 +98,7 @@ public void shouldFindNodeWithNumber() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -131,7 +133,7 @@ public void shouldFindNodeWithBoolean() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -166,7 +168,7 @@ public void shouldFindNodeWithArrays() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -209,7 +211,7 @@ public void shouldRepresentPropertyChanges() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -258,7 +260,7 @@ public void shouldNotFindRemovedNodes() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -301,7 +303,7 @@ public void shouldNotFindRemovedProperties() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop", "prop2"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop", "prop2" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -364,7 +366,7 @@ public void shouldOnlyIndexIndexedProperties() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
try ( Transaction tx = db.beginTx() )
Expand Down Expand Up @@ -398,7 +400,7 @@ public void shouldSearchAcrossMultipleProperties() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop", "prop2"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop", "prop2" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -438,7 +440,7 @@ public void shouldOrderResultsBasedOnRelevance() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"first", "last"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "first", "last" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -485,8 +487,8 @@ public void shouldDifferentiateNodesAndRelationships() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "relationships", FULLTEXT_INDEX_TYPE.RELATIONSHIPS, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );
fulltextFactory.createFulltextIndex( "relationships", FULLTEXT_INDEX_TYPE.RELATIONSHIPS, Arrays.asList( "prop" ), provider );

long firstNodeID;
long secondNodeID;
Expand Down Expand Up @@ -545,7 +547,7 @@ public void fuzzyQueryShouldBeFuzzy() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -584,7 +586,7 @@ public void fuzzyQueryShouldReturnExactMatchesFirst() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );

long firstID;
long secondID;
Expand Down Expand Up @@ -628,8 +630,8 @@ public void shouldNotReturnNonMatches() throws Exception
FulltextFactory fulltextFactory = new FulltextFactory( fileSystemRule, testDirectory.graphDbDir(), ANALYZER );
try ( FulltextProvider provider = FulltextProvider.instance( db ) )
{
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "relationships", FULLTEXT_INDEX_TYPE.RELATIONSHIPS, new String[]{"prop"}, provider );
fulltextFactory.createFulltextIndex( "nodes", FULLTEXT_INDEX_TYPE.NODES, Arrays.asList( "prop" ), provider );
fulltextFactory.createFulltextIndex( "relationships", FULLTEXT_INDEX_TYPE.RELATIONSHIPS, Arrays.asList( "prop" ), provider );

try ( Transaction tx = db.beginTx() )
{
Expand Down

0 comments on commit 45fee03

Please sign in to comment.