Reputation: 1333
I need to test if certain documents match a query before actually indexing them. How would you do this? One of the possibilities I'm thinking of is running a plain lucene index on memory (ramdisk?) and follow a index -> test query -> delete loop for every new document I have before sending it to the actual Solr server.
Can anyone think of a better solution for this problem?
Thanks a lot.
Update:
Looks like this could be a good starting point: http://www.lucenetutorial.com/lucene-in-5-minutes.html
Upvotes: 0
Views: 420
Reputation: 3096
Since Solr allows transactions / commits you can actually index them and before you do commit do state a delete query which removes all non matching documents.
/**
* @author Omnaest
*/
public class SolrSimpleIndexingTest
{
protected SolrServer solrServer = newSolrServerInstance();
@Test
public void testSolr() throws IOException,
SolrServerException
{
{
SolrInputDocument solrInputDocument = new SolrInputDocument();
{
solrInputDocument.addField( "id", "0" );
solrInputDocument.addField( "text", "test1" );
}
this.solrServer.add( solrInputDocument );
}
{
SolrInputDocument solrInputDocument = new SolrInputDocument();
{
solrInputDocument.addField( "id", "1" );
solrInputDocument.addField( "text", "test2" );
}
this.solrServer.add( solrInputDocument );
}
this.solrServer.deleteByQuery( "text:([* TO *] -test2)" );
this.solrServer.commit();
/*
* Now your index does only contain the document with id=1 !!
*/
QueryResponse queryResponse = this.solrServer.query( new SolrQuery().setQuery( "*:*" ) );
SolrDocumentList solrDocumentList = queryResponse.getResults();
assertEquals( 1, solrDocumentList.size() );
assertEquals( "1", solrDocumentList.get( 0 ).getFieldValue( "id" ) );
}
/**
* @return
*/
private static CommonsHttpSolrServer newSolrServerInstance()
{
try
{
return new CommonsHttpSolrServer( "http://localhost:8983/solr" );
}
catch ( MalformedURLException e )
{
e.printStackTrace();
fail();
}
return null;
}
}
Upvotes: 2