I'm attempting to add a custom field to Solr from a Drupal enviorenment.
Atm in schema.xml I have
<field name="custom_files" type="text" indexed="true" stored="true" termVectors="true"/>
While in drupal custom module in hook_apachesolr_index_documents_alter() is
foreach($documents as &$document){
$document->addField('custom_files', 'some long string');
In solr query and schema browser 'custom_files' field is present and can be read, however, on general search it returns nothing. The only way to make something to return based on 'custom_files' field is to search directly in field.
How could I meake solr search 'custom_files' field on general search?
Note: I tried also to make my field using dynamic field definition but ended with same result.
You don't mention which version of Drupal (D7 I assume?) or which module you're using (apachesolr or search_api_solr) , but the gist is that you need to add it to the fl
parameter (fl
= field list) so that the contents of the field are returned in the search results. You've indexed the data, but you also have to tell the query to return that data. With the apachesolr module, you would use the hook_apachesolr_query_prepare()
hook to add that parameter.
function mymodule_apachesolr_query_prepare() {
$query->addParam('fl', 'custom_files');
)
On a side note, why are you using a custom field in schema.xml? Solr has dynamic fields which allow you to create a custom field on the fly without having to add anything to the schema definition. These text fields are defined in the D7 apachesolr schema:
<dynamicField name="ts_*" type="text" indexed="true" stored="true" multiValued="false" termVectors="true"/>
<dynamicField name="tm_*" type="text" indexed="true" stored="true" multiValued="true" termVectors="true"/>
The s
and m
stand for single
and multiple
, so you would use ts_
if the field would only store a single value per document, and tm_
if the field would have multiple values per document.
So in your case you could do this in your indexing hook:
$document->addField('ts_custom_files', 'some long string');
and then
$query->addParam('fl', 'ts_custom_files');
in your query_prepare
hook. And all this without adding anything to your schema.