Mastering Solr Query Building: Expert Tips and Strategies Skip to main content

Custom Solr Query

Saurabh Dhariwal

Saurabh Dhariwal

Building Custom Solr Query

Solr search has great performance, is suitable even for use on large commercial websites and supports facets and multi-index searches. What if we want to build your own query or get all indexed nodes programmatically?

Yes, building the custom query for Solr content will solve the problem.

Benefits of custom Solr Query

  1. Reduces load time.
  2. Custom filters.

1. Initialize solr api query using your index name

// To get index field value
$public_index = “Name of your Index”
$index = search_api_index_load($public_index);
$query = new SearchApiQuery($index);

2. Building filter, you can add multiple filters in your query

// Build a filter.
$node_filter = $query->createFilter('AND');
$node_filter->condition('type', 'content_type', '=');
$node_filter->condition('status', NODE_PUBLISHED, '=');

3. Adding filters to query

// Added filter in query.
$filter->filter($node_filter);
$query->filter($filter);

4. Fetch results of custom query

// Fetch results.
$results = $query->execute()['results'];
$arr_nodes = array_keys(return $arr_node$results);

Hope this helped you to get most out of the system, feel free to share your reviews and Pick the best answer for your requirements or you can contact us for any Web Development Services.