Skip to content

Commit

Permalink
Code cleanup in fulltext addon
Browse files Browse the repository at this point in the history
  • Loading branch information
ragadeeshu committed Sep 12, 2017
1 parent 2c47c0c commit 1c504a2
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 66 deletions.
Expand Up @@ -110,10 +110,8 @@ public PostingsFormat getPostingsFormatForField( String field )


public static IndexWriterConfig population() public static IndexWriterConfig population()
{ {
IndexWriterConfig writerConfig = standard(); Analyzer analyzer = LuceneDataSource.KEYWORD_ANALYZER;
writerConfig.setMaxBufferedDocs( POPULATION_MAX_BUFFERED_DOCS ); return population( analyzer );
writerConfig.setRAMBufferSizeMB( POPULATION_RAM_BUFFER_SIZE_MB );
return writerConfig;
} }


public static IndexWriterConfig population( Analyzer analyzer ) public static IndexWriterConfig population( Analyzer analyzer )
Expand Down
Expand Up @@ -20,11 +20,11 @@
package org.neo4j.kernel.api.impl.fulltext; package org.neo4j.kernel.api.impl.fulltext;


import java.io.IOException; import java.io.IOException;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;


import org.neo4j.graphdb.GraphDatabaseService; import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.impl.logging.LogService; import org.neo4j.kernel.impl.logging.LogService;
Expand All @@ -37,23 +37,27 @@ public class FulltextProvider implements AutoCloseable
{ {
private static FulltextProvider instance; private static FulltextProvider instance;
private final GraphDatabaseService db; private final GraphDatabaseService db;
private Log log; private final Log log;
private final FulltextTransactionEventUpdater fulltextTransactionEventUpdater; private final FulltextTransactionEventUpdater fulltextTransactionEventUpdater;
private final Set<String> nodeProperties;
private final Set<String> relationshipProperties;
private final Set<WritableFulltext> writableNodeIndices;
private final Set<WritableFulltext> writableRelationshipIndices;
private final Map<String,LuceneFulltext> nodeIndices;
private final Map<String,LuceneFulltext> relationshipIndices;
private boolean closed; private boolean closed;
private Set<String> nodeProperties;
private Set<String> relationshipProperties;
private Map<String,LuceneFulltext> nodeIndices;
private Map<String,LuceneFulltext> relationshipIndices;


private FulltextProvider( GraphDatabaseService db, Log log ) private FulltextProvider( GraphDatabaseService db, Log log )
{ {
this.db = db; this.db = db;
this.log = log; this.log = log;
closed = false; closed = false;
fulltextTransactionEventUpdater = new FulltextTransactionEventUpdater( this ); fulltextTransactionEventUpdater = new FulltextTransactionEventUpdater( this, log );
db.registerTransactionEventHandler( fulltextTransactionEventUpdater ); db.registerTransactionEventHandler( fulltextTransactionEventUpdater );
nodeProperties = new HashSet<>(); nodeProperties = new HashSet<>();
relationshipProperties = new HashSet<>(); relationshipProperties = new HashSet<>();
writableNodeIndices = new HashSet<>();
writableRelationshipIndices = new HashSet<>();
nodeIndices = new HashMap<>(); nodeIndices = new HashMap<>();
relationshipIndices = new HashMap<>(); relationshipIndices = new HashMap<>();
} }
Expand Down Expand Up @@ -114,11 +118,13 @@ synchronized void register( LuceneFulltext fulltextIndex ) throws IOException
if ( fulltextIndex.getType() == FulltextIndexType.NODES ) if ( fulltextIndex.getType() == FulltextIndexType.NODES )
{ {
nodeIndices.put( fulltextIndex.getIdentifier(), fulltextIndex ); nodeIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
writableNodeIndices.add( new WritableFulltext( (fulltextIndex) ) );
nodeProperties.addAll( fulltextIndex.getProperties() ); nodeProperties.addAll( fulltextIndex.getProperties() );
} }
else else
{ {
relationshipIndices.put( fulltextIndex.getIdentifier(), fulltextIndex ); relationshipIndices.put( fulltextIndex.getIdentifier(), fulltextIndex );
writableRelationshipIndices.add( new WritableFulltext( (fulltextIndex) ) );
relationshipProperties.addAll( fulltextIndex.getProperties() ); relationshipProperties.addAll( fulltextIndex.getProperties() );
} }
} }
Expand All @@ -135,12 +141,12 @@ String[] getRelationshipProperties()


Set<WritableFulltext> writableNodeIndices() Set<WritableFulltext> writableNodeIndices()
{ {
return nodeIndices.values().stream().map( WritableFulltext::new ).collect( Collectors.toSet() ); return Collections.unmodifiableSet( writableNodeIndices );
} }


Set<WritableFulltext> writableRelationshipIndices() Set<WritableFulltext> writableRelationshipIndices()
{ {
return relationshipIndices.values().stream().map( WritableFulltext::new ).collect( Collectors.toSet() ); return Collections.unmodifiableSet( writableRelationshipIndices );
} }


/** /**
Expand Down
Expand Up @@ -33,15 +33,18 @@
import org.neo4j.graphdb.event.PropertyEntry; import org.neo4j.graphdb.event.PropertyEntry;
import org.neo4j.graphdb.event.TransactionData; import org.neo4j.graphdb.event.TransactionData;
import org.neo4j.graphdb.event.TransactionEventHandler; import org.neo4j.graphdb.event.TransactionEventHandler;
import org.neo4j.logging.Log;


class FulltextTransactionEventUpdater implements TransactionEventHandler<Object> class FulltextTransactionEventUpdater implements TransactionEventHandler<Object>
{ {


private FulltextProvider fulltextProvider; private final FulltextProvider fulltextProvider;
private final Log log;


FulltextTransactionEventUpdater( FulltextProvider fulltextProvider ) FulltextTransactionEventUpdater( FulltextProvider fulltextProvider, Log log )
{ {
this.fulltextProvider = fulltextProvider; this.fulltextProvider = fulltextProvider;
this.log = log;
} }


@Override @Override
Expand Down Expand Up @@ -105,7 +108,7 @@ public void afterCommit( TransactionData data, Object state )
} }
catch ( IOException e ) catch ( IOException e )
{ {
throw new RuntimeException( "Unable to update fulltext index", e ); log.error( "Unable to update fulltext index", e );
} }
} }


Expand All @@ -119,7 +122,7 @@ private <E extends Entity> void updatePropertyData( Map<Long,Map<String,Object>>
long entityId = stateEntry.getKey(); long entityId = stateEntry.getKey();
Map<String,Object> allProperties = Map<String,Object> allProperties =
stateEntry.getValue().entrySet().stream().filter( entry -> indexedProperties.contains( entry.getKey() ) ).collect( stateEntry.getValue().entrySet().stream().filter( entry -> indexedProperties.contains( entry.getKey() ) ).collect(
Collectors.toMap( entry -> entry.getKey(), entry -> entry.getValue() ) ); Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) );
if ( !allProperties.isEmpty() ) if ( !allProperties.isEmpty() )
{ {
Document document = LuceneFulltextDocumentStructure.documentRepresentingProperties( entityId, allProperties ); Document document = LuceneFulltextDocumentStructure.documentRepresentingProperties( entityId, allProperties );
Expand Down Expand Up @@ -154,7 +157,7 @@ private void refreshIndex( WritableFulltext index )
} }
catch ( IOException e ) catch ( IOException e )
{ {
e.printStackTrace(); log.error( "Failed to refresh fulltext after updates", e );
} }
} }


Expand Down
Expand Up @@ -25,6 +25,7 @@
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;


import org.neo4j.kernel.api.impl.index.AbstractLuceneIndex; import org.neo4j.kernel.api.impl.index.AbstractLuceneIndex;
Expand Down Expand Up @@ -75,9 +76,7 @@ public boolean equals( Object o )
@Override @Override
public int hashCode() public int hashCode()
{ {
int result = identifier.hashCode(); return Objects.hash( identifier, type );
result = 31 * result + type.hashCode();
return result;
} }


PartitionedIndexWriter getIndexWriter( WritableFulltext writableIndex ) throws IOException PartitionedIndexWriter getIndexWriter( WritableFulltext writableIndex ) throws IOException
Expand Down
Expand Up @@ -92,6 +92,6 @@ public void close()


private PrimitiveLongIterator partitionedOperation( Function<ReadOnlyFulltext,PrimitiveLongIterator> readerFunction ) private PrimitiveLongIterator partitionedOperation( Function<ReadOnlyFulltext,PrimitiveLongIterator> readerFunction )
{ {
return PrimitiveLongCollections.concat( indexReaders.parallelStream().map( readerFunction::apply ).collect( Collectors.toList() ) ); return PrimitiveLongCollections.concat( indexReaders.parallelStream().map( readerFunction ).collect( Collectors.toList() ) );
} }
} }
Expand Up @@ -23,14 +23,13 @@
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser; import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query; import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort; import org.apache.lucene.search.Sort;


import java.io.IOException; import java.io.IOException;


import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator; import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.kernel.api.impl.index.collector.DocValuesCollector; import org.neo4j.kernel.api.impl.index.collector.DocValuesCollector;
import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher; import org.neo4j.kernel.api.impl.index.partition.PartitionSearcher;
Expand Down Expand Up @@ -59,70 +58,48 @@ class SimpleFulltextReader implements ReadOnlyFulltext
@Override @Override
public PrimitiveLongIterator query( String... terms ) public PrimitiveLongIterator query( String... terms )
{ {
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser( properties, analyzer ); String concatenatedQuery = String.join( " ", terms );
multiFieldQueryParser.setDefaultOperator( QueryParser.Operator.OR ); return innerQuery( concatenatedQuery );
Query query;
try
{
query = multiFieldQueryParser.parse( String.join( " ", terms ) );
}
catch ( ParseException e )
{
query = parseFallbackBooleanQuery( multiFieldQueryParser, terms );
}
return query( query );
} }


private Query parseFallbackBooleanQuery( MultiFieldQueryParser multiFieldQueryParser, String[] tokens ) @Override
public PrimitiveLongIterator fuzzyQuery( String... terms )
{ {
Query query; String concatenatedQuery = String.join( "~ ", terms ) + "~";
BooleanQuery.Builder builder = new BooleanQuery.Builder(); return innerQuery( concatenatedQuery );
for ( String s : tokens )
{
for ( String property : properties )
{
Query booleanQuery = multiFieldQueryParser.createBooleanQuery( property, s );
if ( booleanQuery != null )
{
builder.add( booleanQuery, BooleanClause.Occur.SHOULD );
}
}
}
query = builder.build();
return query;
} }


@Override @Override
public PrimitiveLongIterator fuzzyQuery( String... terms ) public void close()
{ {
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser( properties, analyzer );
multiFieldQueryParser.setDefaultOperator( QueryParser.Operator.OR );
Query query;
try try
{ {
query = multiFieldQueryParser.parse( String.join( "~ ", terms ) + "~" ); partitionSearcher.close();
} }
catch ( ParseException e ) catch ( IOException e )
{ {
query = parseFallbackBooleanQuery( multiFieldQueryParser, terms ); throw new IndexReaderCloseException( e );
} }
return query( query );
} }


@Override private PrimitiveLongIterator innerQuery( String concatenatedQuery )
public void close()
{ {
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser( properties, analyzer );
multiFieldQueryParser.setDefaultOperator( QueryParser.Operator.OR );
Query query;
try try
{ {
partitionSearcher.close(); query = multiFieldQueryParser.parse( concatenatedQuery );
} }
catch ( IOException e ) catch ( ParseException e )
{ {
throw new IndexReaderCloseException( e ); assert false;
return PrimitiveLongCollections.emptyIterator();
} }
return indexQuery( query );
} }


private PrimitiveLongIterator query( Query query ) private PrimitiveLongIterator indexQuery( Query query )
{ {
try try
{ {
Expand Down

0 comments on commit 1c504a2

Please sign in to comment.