You are here

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

Same name and namespace in other branches
  1. 8 src/Utility/Utility.php \Drupal\search_api_solr\Utility\Utility::encodeSolrName()
  2. 8.2 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.

11 calls to Utility::encodeSolrName()
SearchApiSolrBackend::formatSolrFieldNames in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Returns a language-specific mapping from Drupal to Solr field names.
SearchApiSolrBackend::getDocuments in src/Plugin/search_api/backend/SearchApiSolrBackend.php
SolrFieldType::getCollatedField in src/Entity/SolrFieldType.php
Returns the collated field definition.
SolrFieldType::getDynamicFields in src/Entity/SolrFieldType.php
Gets a list of dynamic Solr fields that will use this Solr Field Type.
StreamingExpressionBuilder::__construct in src/Utility/StreamingExpressionBuilder.php
StreamingExpressionBuilder constructor.

... See full list

File

src/Utility/Utility.php, line 272

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);
}