Example usage for org.apache.solr.update DocumentBuilder toDocument

List of usage examples for org.apache.solr.update DocumentBuilder toDocument

Introduction

In this page you can find the example usage for org.apache.solr.update DocumentBuilder toDocument.

Prototype

public static Document toDocument(SolrInputDocument doc, IndexSchema schema) 

Source Link

Usage

From source file:net.yacy.search.index.SingleDocumentMatcher.java

License:Open Source License

/**
 * Check a given Solr document against a Solr query, without requesting a Solr
 * index, but using instead in-memory Lucene utility. This lets checking if a
 * single document matches some criterias, before adding it to a Solr index.
 * /*  w  w  w.  j  a v a 2s  .  c o  m*/
 * @param solrDoc
 *            the Solr document to check
 * @param query
 *            a standard Solr query string
 * @param core
 *            the Solr index core holding the Solr schema of the document
 * @return true when the document matches the given Solr query
 * @throws SyntaxError
 *             when the query String syntax is not valid
 * @throws SolrException when a query required element is missing, or when a problem occurred when accessing the target core
 * @throws IllegalArgumentException
 *             when a parameter is null.
 * @see <a href=
 *      "http://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html">The
 *      Solr Standard Query Parser</a>
 */
public static boolean matches(final SolrInputDocument solrDoc, final String query, final SolrCore core)
        throws SyntaxError, IllegalArgumentException {
    if (solrDoc == null || query == null || core == null) {
        throw new IllegalArgumentException("All parameters must be non null");
    }
    final IndexSchema schema = core.getLatestSchema();
    if (schema == null) {
        throw new IllegalArgumentException("All parameters must be non null");
    }

    final org.apache.lucene.document.Document luceneDoc = DocumentBuilder.toDocument(solrDoc, schema);

    final Analyzer indexAnalyzer = schema.getIndexAnalyzer();

    /*
     * Using the Lucene RAMDirectory could be an alternative, but it is slower with
     * a larger memory footprint
     */
    final MemoryIndex index = MemoryIndex.fromDocument(luceneDoc, indexAnalyzer);

    final Query luceneQuery = toLuceneQuery(query, core);

    final float score = index.search(luceneQuery);

    return score > 0.0f;
}