How to programmatically query a CCK field in Solr Search API index application

The following codes will query the "title" field in Solr Search API index application:

  
$server = search_api_server_load('machine_name_of_your_solr_server');
$index = search_api_index_load('machine_name_of_your_index_application');
$query = new SearchApiQuery($index);
$query->keys($term);
$query->fields(array('title'));
$solr = new SearchApiSolrService($server);
$result = $solr->search($query);
  

The $result should contain the query result in array form. Note: the "machine_name_of_your_solr_server" can be found by going to the "View" tab of your Search API server and its URL should contain this (Eg. if the URL is http://www.yoursite.com/admin/config/search/search_api/server/solr_server, the machine name of your Search API server is solr_server.) and the "machine_name_of_your_index_application" can be found by going to the "View" tab of your index application and its URL should contain this (Eg. if the URL is http://www.yoursite.com/admin/config/search/search_api/index/myapp_index, the machine name of your index application is myapp_index.)

To query a custom field with machine name "field_family_name", we will use this "field_family_name:content_raw" as parameter of our $query->fields():

  
$server = search_api_server_load('machine_name_of_your_solr_server');
$index = search_api_index_load('machine_name_of_your_index_application');
$query = new SearchApiQuery($index);
$query->keys($term);
$query->fields(array('field_family_name:content_raw'));
$solr = new SearchApiSolrService($server);
$result = $solr->search($query);
  

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.