You are here

public static function Utility::encodeSolrName in Search API Solr 8.2

Same name and namespace in other branches
  1. 8.3 src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::encodeSolrName()
  2. 8 src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::encodeSolrName()
  3. 4.x src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::encodeSolrName()

Encodes field names to avoid characters that are not supported by solr.

Solr doesn't restrict the characters used to build field names. But using non java identifiers within a field name can cause different kind of trouble when running queries. Java identifiers are only consist of letters, digits, '$' and '_'. See https://issues.apache.org/jira/browse/SOLR-3996 and http://docs.oracle.com/cd/E19798-01/821-1841/bnbuk/index.html For full compatibility the '$' has to be avoided, too. And there're more restrictions regarding the field name itself. See https://cwiki.apache.org/confluence/display/solr/Defining+Fields "Field names should consist of alphanumeric or underscore characters only and not start with a digit ... Names with both leading and trailing underscores (e.g. _version_) are reserved." Field names starting with digits or underscores are already avoided by our schema. The same is true for the names of field types. See https://cwiki.apache.org/confluence/display/solr/Field+Type+Definitions+... "It is strongly recommended that names consist of alphanumeric or underscore characters only and not start with a digit. This is not currently strictly enforced."

This function therefore encodes all forbidden characters in their hexadecimal equivalent encapsulated by a leading sequence of '_X' and a termination character '_'. Example: "tm_entity:node/body" becomes "tm_entity_X3a_node_X2f_body".

As a consequence the sequence '_X' itself needs to be encoded if it occurs within a field name. Example: "last_XMas" becomes "last_X5f58_Mas".

Parameters

string $field_name: The field name.

Return value

string The encoded field name.

15 calls to Utility::encodeSolrName()
AbstractSearchApiSolrMultilingualBackend::alterSolrDocuments in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Replaces language unspecific fulltext fields by language specific ones.
AbstractSearchApiSolrMultilingualBackend::getLanguageSpecificSolrFieldNames in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Gets a language-specific mapping from Drupal to Solr field names.
AbstractSearchApiSolrMultilingualBackend::getSchemaLanguageStatistics in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Gets schema language statistics for the multilingual Solr server.
AbstractSearchApiSolrMultilingualBackend::preQuery in src/Plugin/search_api/backend/AbstractSearchApiSolrMultilingualBackend.php
Modify the query before it is sent to solr.
SearchApiSolrBackend::getDocuments in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Retrieves Solr documents from search api index items.

... See full list

File

src/Utility/Utility.php, line 270

Class

Utility
Provides various helper functions for Solr backends.

Namespace

Drupal\search_api_solr\Utility

Code

public static function encodeSolrName($field_name) {
  return preg_replace_callback('/([^\\da-zA-Z_]|_X)/u', function ($matches) {
    return '_X' . bin2hex($matches[1]) . '_';
  }, $field_name);
}