You are here

protected function SearchApiSolrTest::checkQueryParsers in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::checkQueryParsers()

Tests the conversion of Search API queries into Solr queries.

1 call to SearchApiSolrTest::checkQueryParsers()
SearchApiSolrTest::checkBackendSpecificFeatures in tests/src/Kernel/SearchApiSolrTest.php
Checks backend specific features.

File

tests/src/Kernel/SearchApiSolrTest.php, line 328

Class

SearchApiSolrTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

protected function checkQueryParsers() {
  $parse_mode_manager = \Drupal::service('plugin.manager.search_api.parse_mode');
  $parse_mode_terms = $parse_mode_manager
    ->createInstance('terms');
  $parse_mode_phrase = $parse_mode_manager
    ->createInstance('phrase');
  $parse_mode_sloppy_terms = $parse_mode_manager
    ->createInstance('sloppy_terms');
  $parse_mode_sloppy_phrase = $parse_mode_manager
    ->createInstance('sloppy_phrase');
  $parse_mode_fuzzy_terms = $parse_mode_manager
    ->createInstance('fuzzy_terms');
  $parse_mode_direct = $parse_mode_manager
    ->createInstance('direct');
  $parse_mode_edismax = $parse_mode_manager
    ->createInstance('edismax');
  $query = $this
    ->buildSearch('foo "apple pie" bar');
  $query
    ->setParseMode($parse_mode_phrase);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'phrase');
  $this
    ->assertEquals('(+"foo \\"apple pie\\" bar")', $flat);
  $query
    ->setParseMode($parse_mode_sloppy_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'sloppy_terms', [
    'slop' => 1234,
  ]);
  $this
    ->assertEquals('(+"foo" +"apple pie"~1234 +"bar")', $flat);
  $query
    ->setParseMode($parse_mode_sloppy_phrase);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'sloppy_phrase', [
    'slop' => 5678,
  ]);
  $this
    ->assertEquals('(+"foo \\"apple pie\\" bar"~5678)', $flat);
  $query
    ->setParseMode($parse_mode_fuzzy_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'fuzzy_terms', [
    'fuzzy' => 1,
  ]);
  $this
    ->assertEquals('(+foo~1 +"apple pie" +bar~1)', $flat);
  $query
    ->setParseMode($parse_mode_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'terms');
  $this
    ->assertEquals('(+"foo" +"apple pie" +"bar")', $flat);
  $query
    ->setParseMode($parse_mode_edismax);
  $exception = FALSE;
  try {
    $flat = SolrUtility::flattenKeys($query
      ->getKeys(), [], 'edismax');
  } catch (SearchApiSolrException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception);
  $query
    ->setParseMode($parse_mode_direct);
  $exception = FALSE;
  try {
    $flat = SolrUtility::flattenKeys($query
      ->getKeys(), [], 'direct');
  } catch (SearchApiSolrException $e) {
    $exception = TRUE;
  }
  $this
    ->assertTrue($exception);
  $query
    ->setParseMode($parse_mode_phrase);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field',
  ], 'phrase');
  $this
    ->assertEquals('solr_field:(+"foo \\"apple pie\\" bar")', $flat);
  $query
    ->setParseMode($parse_mode_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field',
  ], 'terms');
  $this
    ->assertEquals('((+(solr_field:"foo") +(solr_field:"apple pie") +(solr_field:"bar")) solr_field:(+"foo" +"apple pie" +"bar"))', $flat);
  $query
    ->setParseMode($parse_mode_edismax);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field',
  ], 'edismax');
  $this
    ->assertEquals('({!edismax qf=\'solr_field\'}+"foo" +"apple pie" +"bar")', $flat);
  $query
    ->setParseMode($parse_mode_phrase);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field_1',
    'solr_field_2',
  ], 'phrase');
  $this
    ->assertEquals('(solr_field_1:(+"foo \\"apple pie\\" bar") solr_field_2:(+"foo \\"apple pie\\" bar"))', $flat);
  $query
    ->setParseMode($parse_mode_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field_1',
    'solr_field_2',
  ], 'terms');
  $this
    ->assertEquals('((+(solr_field_1:"foo" solr_field_2:"foo") +(solr_field_1:"apple pie" solr_field_2:"apple pie") +(solr_field_1:"bar" solr_field_2:"bar")) solr_field_1:(+"foo" +"apple pie" +"bar") solr_field_2:(+"foo" +"apple pie" +"bar"))', $flat);
  $query
    ->setParseMode($parse_mode_edismax);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [
    'solr_field_1',
    'solr_field_2',
  ], 'edismax');
  $this
    ->assertEquals('({!edismax qf=\'solr_field_1 solr_field_2\'}+"foo" +"apple pie" +"bar")', $flat);
  $query
    ->setParseMode($parse_mode_terms);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'keys');
  $this
    ->assertEquals('+"foo" +"apple pie" +"bar"', $flat);
  $query = $this
    ->buildSearch('foo apple pie bar');
  $query
    ->setParseMode($parse_mode_sloppy_phrase);
  $flat = SolrUtility::flattenKeys($query
    ->getKeys(), [], 'sloppy_phrase', [
    'slop' => 5,
  ]);
  $this
    ->assertEquals('(+"foo apple pie bar"~5)', $flat);
}