Commit 7e4c9d72 authored by alexpott's avatar alexpott

Issue #2544830 by jhedstrom, jhodgdon: Views search filter needs additional...

Issue #2544830 by jhedstrom, jhodgdon: Views search filter needs additional WHERE on search_index table
parent 3bc6dc12
......@@ -93,10 +93,11 @@ public function query($group_by = FALSE) {
$join = Views::pluginManager('join')->createInstance('standard', $definition);
$search_total = $this->query->addRelationship('search_total', $join, $search_index);
// Add the search score field to the query.
$this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', array('function' => 'sum'));
// Add the conditions set up by the search query to the views query.
$search_condition->condition("$search_index.type", $this->searchType);
$search_dataset = $this->query->addTable('node_search_dataset');
$conditions = $this->searchQuery->conditions();
$condition_conditions =& $conditions->conditions();
......@@ -110,6 +111,16 @@ public function query($group_by = FALSE) {
$search_conditions =& $search_condition->conditions();
$search_conditions = array_merge($search_conditions, $condition_conditions);
// Add the keyword conditions, as is done in
// SearchQuery::prepareAndNormalize(), but simplified because we are
// only concerned with relevance ranking so we do not need to normalize.
$or = db_or();
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}
$search_condition->condition($or);
// Add the GROUP BY and HAVING expressions to the query.
$this->query->addWhere(0, $search_condition);
$this->query->addGroupBy("$search_index.sid");
$matches = $this->searchQuery->matches();
......
......@@ -166,11 +166,12 @@ public function query() {
'left_field' => 'word',
);
$join = Views::pluginManager('join')->createInstance('standard', $definition);
$search_total = $this->query->addRelationship('search_total', $join, $search_index);
// Add the search score field to the query.
$this->search_score = $this->query->addField('', "$search_index.score * $search_total.count", 'score', array('function' => 'sum'));
// Add the conditions set up by the search query to the views query.
$search_condition->condition("$search_index.type", $this->searchType);
$search_dataset = $this->query->addTable('node_search_dataset');
$conditions = $this->searchQuery->conditions();
......@@ -185,7 +186,18 @@ public function query() {
$search_conditions =& $search_condition->conditions();
$search_conditions = array_merge($search_conditions, $condition_conditions);
// Add the keyword conditions, as is done in
// SearchQuery::prepareAndNormalize(), but simplified because we are
// only concerned with relevance ranking so we do not need to normalize.
$or = db_or();
foreach ($words as $word) {
$or->condition("$search_index.word", $word);
}
$search_condition->condition($or);
$this->query->addWhere($this->options['group'], $search_condition);
// Add the GROUP BY and HAVING expressions to the query.
$this->query->addGroupBy("$search_index.sid");
$matches = $this->searchQuery->matches();
$placeholder = $this->placeholder();
......
......@@ -101,6 +101,38 @@ public function testSearchIntegration() {
$this->assertNoLink('pizza');
$this->assertNoLink('sandwich');
$this->assertOneLink('cola');
// Test sorting.
$node = [
'title' => "Drupal's search rocks.",
'type' => $type->id(),
];
$this->drupalCreateNode($node);
$node['title'] = "Drupal's search rocks really rocks!";
$this->drupalCreateNode($node);
$this->cronRun();
$this->drupalGet('test-arg/rocks');
$xpath = '//div[@class="views-row"]//a';
/** @var \SimpleXMLElement[] $results */
$results = $this->xpath($xpath);
$this->assertEqual((string) $results[0], "Drupal's search rocks really rocks!");
$this->assertEqual((string) $results[1], "Drupal's search rocks.");
// Test sorting with another set of titles.
$node = [
'title' => "Testing one two two two",
'type' => $type->id(),
];
$this->drupalCreateNode($node);
$node['title'] = "Testing one one one";
$this->drupalCreateNode($node);
$this->cronRun();
$this->drupalGet('test-arg/one');
$xpath = '//div[@class="views-row"]//a';
/** @var \SimpleXMLElement[] $results */
$results = $this->xpath($xpath);
$this->assertEqual((string) $results[0], "Testing one one one");
$this->assertEqual((string) $results[1], "Testing one two two two");
}
/**
......
......@@ -107,21 +107,15 @@ display:
entity_type: node
entity_field: status
sorts:
created:
id: created
table: node_field_data
field: created
order: DESC
score:
id: score
table: node_search_index
field: score
relationship: none
group_type: group
admin_label: ''
exposed: false
expose:
label: ''
granularity: second
plugin_id: date
entity_type: node
entity_field: created
order: DESC
plugin_id: search_score
title: ''
header: { }
footer: { }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment