You are here

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

Same name and namespace in other branches
  1. 8.3 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.

2 calls to Utility::encodeSolrName()
SearchApiSolrBackend::getSolrFieldNames in src/Plugin/search_api/backend/SearchApiSolrBackend.php
Creates a list of all indexed field names mapped to their Solr field names.
UtilitiesTest::testFieldNameEncoder in tests/src/Kernel/UtilitiesTest.php
Tests encoding and decoding of Solr field names.

File

src/Utility/Utility.php, line 230

Class

Utility
Utility functions specific to solr.

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