Solr Query Builder

A simple PHP 5.3 library that will help to make Solr Queries with an object oriented, dynamic approach. Primarily wrote for using with solr-php-client, but can be used with any solr client that works with simple query-string. Can be integrated with any PHP 5.3 Framework easily.


  • Simple queries
  • Automatic escaping
  • Grouping terms
  • Term modifiers
    • Fuzzy Search
    • Required/Restricted
    • Boost factors
    • Proximity Search
  • Range Queries (Date/Numeric)
  • (Any number of) Subqueries

Usages Example

Two simple classes made everything here. Field and QueryString. __toString() of both classes just return the raw string query that you can use for searching solr. Subqueries are actually another full featured QueryString object. So, Any QueryString object can be used as subquery to another QueryString.

Here is a small snippet of example -

use Solr\QueryBuilder\Querystring;
use Solr\QueryBuilder\Field;

echo "--------- Solr Query Test ----------- \n";

$Query = new Querystring();
$Query->addField(new Field('name', 'Anis uddin Ahmad'));
$Query->addField(new Field('phone', '+880 173 0053053'), 'PHONE-an-optional-key-for-removing');

echo 'Simple query: '. $Query ;
// OUTPUT-> Simple query: name:"Anis uddin Ahmad" AND phone:"+880 173 0053053"

// Removing fields

echo "--------- Solr SubQuery Test ----------- \n";

$subQuery = new  Querystring();
$subQuery->addField(new Field(null, 'value for default field'));
$subQuery->addField(new Field('item_type', 'anything'));


echo 'Subquery Output: '. $subQuery;
// OUTPUT-> Subquery Output: "value for default field" OR item_type:anything

// Add $subQuery in $Query
$Query->addSubQuery($subQuery, 'optional-key');

echo 'After adding sub-query: '. $Query;
// OUTPUT-> After adding sub-query: name:"Anis uddin Ahmad" AND ("value for default field" OR item_type:anything)

For more examples, check examples/index.php or test files.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.