Skip to content
djimenez edited this page Jan 26, 2013 · 3 revisions

What Example Code is Available?

See the Example Usage wiki page for some PHP code as well as a link to an IBM Developer Works article.

How Can I Use Additional Parameters (like fq, facet, etc)?

The Apache_Solr_Service::search() has its first three arguments mapped to q, start, and rows repsectively. These are the three most common parameters for any query. However, there are a number of additional parameters available to control the behavior of Solr request handlers. Any of these addtional parameters can be passed along in the search method's 4th argument. This argument takes an array of key / value pairs where the key is the additional parameter name (e.g. 'fq') and the value is a single scalar value (will be converted to a string for the query) or an array of scalar values - use arrays when a parameter has multiple values (e.g. you are sending multiple 'facet.field' values).

<?php

$solr = new Apache_Solr_Service();

$query = "your query";
$start = 0;
$rows = 10;

$additionalParameters = array(
    'fq' => 'a filtering query',
    'facet' => 'true',
    // notice I use an array for a muti-valued parameter
    'facet.field' => array(
       'field_1',
       'field_2'
    )
);

$results = $solr->search($query, $start, $rows, $additionalParameters);

The call above would be mapped to the URL: http://localhost:8180/solr/select?q=your+query&start=0&rows=10&fq=a+filtering+query&facet=true&facet.field=field_1&facet.field=field_2

How Do I Use A Non-Default Request Handler?

All named request handlers can be accessed by using the 'qt' additional query parameter in your search call (see the section on additional parameters above). For example, to use the DisMax request handler configured in the distributed Solr configuration you would pass a value of 'dismax' for the the 'qt' parameter.

How do I Add Document / Field Boosts?

There are two levels of boost available in every document. Document level boosts apply to the whole document when searching. Field level boosts apply only to a particular document field.

The Apache_Solr_Document class contains methods for setting a document level boost as well as individual field boosts. You'll need to use the document class for adding documents to solr that you want boosts on. The methods used below should all be documented in their related documentation blocks in source as well.

<?php

$document = new Apache_Solr_Document();

// set a boost for the document
$document->setBoost(2);

// get the current document boost
$document->getBoost();

// add a field with a boost as the last parameter
$document->addField("field", "value", 2);

// or set a field boost separately
$document->field = "value";
$document->setFieldBoost("field", 2);

// get a current field's boost
$boost = $document->getFieldBoost("field");

// or get all field boosts - array indexed by field name
$boosts = $document->getFieldBoosts();

// document boosting can be removed by setting to false
$document->setBoost(false);

// field boosting can also be removed by setting to false
$document->setFieldBoost("field", false);

What is Solr?

Solr is an open source enterprise search server based on the Lucene Java search library, with XML/HTTP and JSON APIs, hit highlighting, faceted search, caching, replication, a web administration interface and many more features. It runs in a Java servlet container such as Tomcat.

For more information about Solr, please see the Solr Web Page.

Why the 3-Clause BSD license?

This client was originally submitted in Solr's issue tracker: https://issues.apache.org/jira/browse/SOLR-341 - as such, it was under the Apache 2.0 license so that it was compatible to be included in the Solr distribution. However, the project ultimately remained third party. Since then, we've been asked several times to allow the client to use other licenses so it could be included in other projects (e.g. a Drupal search plugin). We think that the 3-Clause BSD license is the most compatible with the largest number of other OSS licenses.