Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[new maintainer] Multi-match query does not support match query options #569

Merged
merged 8 commits into from
Mar 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGES

2014-03-13
- Added missing query options for MultiMatch (operator, minimum_should_match, zero_terms_query, cutoff_frequency, type, fuzziness, prefix_length, max_expansions, analyzer)
- Added missing query options for Match (zero_terms_query, cutoff_frequency)

2013-09-03
- Support isset() calls on Result objects

Expand Down
30 changes: 30 additions & 0 deletions lib/Elastica/Query/Match.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
* @category Xodoa
* @package Elastica
* @author F21
* @author WONG Wing Lun <[email protected]>
* @link http://www.elasticsearch.org/guide/reference/query-dsl/match-query.html
*/
class Match extends AbstractQuery
{
const ZERO_TERM_NONE = 'none';
const ZERO_TERM_ALL = 'all';

/**
* Sets a param for the message array
*
Expand Down Expand Up @@ -164,4 +168,30 @@ public function setFieldMaxExpansions($field, $maxExpansions)
{
return $this->setFieldParam($field, 'max_expansions', (int) $maxExpansions);
}

/**
* Set zero terms query
*
* If not set, default to 'none'
*
* @param string $field
* @param string $zeroTermQuery
* @return \Elastica\Query\Match
*/
public function setFieldZeroTermsQuery($field, $zeroTermQuery = 'none')
{
return $this->setFieldParam($field, 'zero_terms_query', $zeroTermQuery);
}

/**
* Set cutoff frequency
*
* @param string $field
* @param float $cutoffFrequency
* @return \Elastica\Query\Match
*/
public function setFieldCutoffFrequency($field, $cutoffFrequency)
{
return $this->setFieldParam($field, 'cutoff_frequency', $cutoffFrequency);
}
}
117 changes: 117 additions & 0 deletions lib/Elastica/Query/MultiMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,23 @@
* @category Xodoa
* @package Elastica
* @author Rodolfo Adhenawer Campagnoli Moraes <[email protected]>
* @author Wong Wing Lun <[email protected]>
* @author Tristan Maindron <[email protected]>
* @link http://www.elasticsearch.org/guide/reference/query-dsl/multi-match-query.html
*/
class MultiMatch extends AbstractQuery
{
const TYPE_BEST_FIELDS = 'best_fields';
const TYPE_MOST_FIELDS = 'most_fields';
const TYPE_CROSS_FIELDS = 'cross_fields';
const TYPE_PHRASE = 'phrase';
const TYPE_PHRASE_PREFIX = 'phrase_prefix';

const OPERATOR_OR = 'or';
const OPERATOR_AND = 'and';

const ZERO_TERM_NONE = 'none';
const ZERO_TERM_ALL = 'all';

/**
* Sets the query
Expand Down Expand Up @@ -60,4 +73,108 @@ public function setTieBreaker($tieBreaker = 0.0)
{
return $this->setParam('tie_breaker', $tieBreaker);
}

/**
* Sets operator for Match Query
*
* If not set, defaults to 'or'
*
* @param string $operator
* @return \Elastica\Query\MultiMatch Current object
*/
public function setOperator($operator = 'or')
{
return $this->setParam('operator', $operator);
}

/**
* Set field minimum should match for Match Query
*
* @param int $minimumShouldMatch
* @return \Elastica\Query\Match
*/
public function setMinimumShouldMatch($minimumShouldMatch)
{
return $this->setParam('minimum_should_match', (int) $minimumShouldMatch);
}

/**
* Set zero terms query for Match Query
*
* If not set, default to 'none'
*
* @param string $zeroTermQuery
* @return \Elastica\Query\Match
*/
public function setZeroTermsQuery($zeroTermQuery = 'none')
{
return $this->setParam('zero_terms_query', $zeroTermQuery);
}

/**
* Set cutoff frequency for Match Query
*
* @param float $cutoffFrequency
* @return \Elastica\Query\Match
*/
public function setCutoffFrequency($cutoffFrequency)
{
return $this->setParam('cutoff_frequency', $cutoffFrequency);
}

/**
* Set type
*
* @param string $field
* @param string $type
* @return \Elastica\Query\Match
*/
public function setType($type)
{
return $this->setParam('type', $type);
}

/**
* Set fuzziness
*
* @param float $fuzziness
* @return \Elastica\Query\Match
*/
public function setFuzziness($fuzziness)
{
return $this->setParam('fuzziness', (float) $fuzziness);
}

/**
* Set prefix length
*
* @param int $prefixLength
* @return \Elastica\Query\Match
*/
public function setPrefixLength($prefixLength)
{
return $this->setParam('prefix_length', (int) $prefixLength);
}

/**
* Set max expansions
*
* @param int $maxExpansions
* @return \Elastica\Query\Match
*/
public function setMaxExpansions($maxExpansions)
{
return $this->setParam('max_expansions', (int) $maxExpansions);
}

/**
* Set analyzer
*
* @param string $analyzer
* @return \Elastica\Query\Match
*/
public function setAnalyzer($analyzer)
{
return $this->setParam('analyzer', $analyzer);
}
}
21 changes: 21 additions & 0 deletions test/lib/Elastica/Test/Query/MatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ public function testMatch()
$this->assertEquals(4, $resultSet->count());
}

public function testMatchZeroTerm()
{
$client = $this->_getClient();
$index = $client->getIndex('test');
$index->create(array(), true);
$type = $index->getType('test');
$doc = new Document(1, array('name' => 'Basel-Stadt'));
$type->addDocument($doc);
$doc = new Document(2, array('name' => 'New York'));
$type->addDocument($doc);
$index->refresh();

$query = new Match();
$query->setFieldQuery('name', '');
$query->setFieldZeroTermsQuery('name', Match::ZERO_TERM_ALL);

$resultSet = $index->search($query);

$this->assertEquals(2, $resultSet->count());
}

public function testMatchPhrase()
{
$client = $this->_getClient();
Expand Down
Loading