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

No Support for setting analyzer on fuzzy like this query and params cannot be set #611

Merged
merged 3 commits into from
May 10, 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
2 changes: 2 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CHANGES
2014-04-28
- Added setAnalyzer method to Query\FuzzyLikeThis Class and fixed issue with params not being merged #611

2014-04-28
- Typo fixes #600, #602
Expand Down
30 changes: 30 additions & 0 deletions lib/Elastica/Query/FuzzyLikeThis.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class FuzzyLikeThis extends AbstractQuery
*/
protected $_boost = 1.0;

/**
* Analyzer
*
* @var sting Analyzer
*/
protected $_analyzer;


/**
* Adds field to flt query
*
Expand Down Expand Up @@ -154,6 +162,20 @@ public function setMaxQueryTerms($value)
return $this;
}

/**
* Set analyzer
*
* @param string $text Analyzer text
* @return \Elastica\Query\FuzzyLikeThis
*/
public function setAnalyzer($text)
{
$text = trim($text);
$this->_analyzer = $text;

return $this;
}

/**
* Converts fuzzy like this query to array
*
Expand All @@ -174,12 +196,20 @@ public function toArray()
$args['like_text'] = $this->_likeText;
}

if (!empty($this->_analyzer)) {
$args['analyzer'] = $this->_analyzer;
}


$args['min_similarity'] = ($this->_minSimilarity > 0) ? $this->_minSimilarity : 0;

$args['prefix_length'] = $this->_prefixLength;
$args['ignore_tf'] = $this->_ignoreTF;
$args['max_query_terms'] = $this->_maxQueryTerms;

$data = parent::toArray();
$args = array_merge($args, $data['fuzzy_like_this']);

return array('fuzzy_like_this' => $args);
}
}
98 changes: 98 additions & 0 deletions test/lib/Elastica/Test/Query/FuzzyLikeThisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,102 @@ public function testSetBoost()

$this->assertEquals($boost, $data['fuzzy_like_this']['boost']);
}

public function testAddAnalyzerViasetParam()
{
$analyzer = 'snowball';

$query = new FuzzyLikeThis();
$query->setParam('analyzer', $analyzer);

$data = $query->toArray();
$this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']);
}

public function testSetAnalyzer()
{
$analyzer = 'snowball';

$query = new FuzzyLikeThis();
$query->setAnalyzer($analyzer);

$data = $query->toArray();
$this->assertEquals($analyzer, $data['fuzzy_like_this']['analyzer']);
}

public function testAnalyzerNotPresentInArrayToMaintainDefaultOfField()
{
$query = new FuzzyLikeThis();

$data = $query->toArray();
$this->assertArrayNotHasKey('analyzer', $data);
}

public function testArgArrayFieldsOverwrittenBySetParams()
{
$query = new FuzzyLikeThis();
$query->setMaxQueryTerms(100);
$query->setParam('max_query_terms', 200);

$data = $query->toArray();
$this->assertEquals(200, $data['fuzzy_like_this']['max_query_terms']);
}

public function testSearchSetAnalyzer()
{
$client = $this->_getClient();
$index = new Index($client, 'test');
$index->create(array('analysis' => array(
'analyzer' => array(
'searchAnalyzer' => array(
'type' => 'custom',
'tokenizer' => 'standard',
'filter' => array('myStopWords')
)
),
'filter' => array(
'myStopWords' => array(
'type' => 'stop',
'stopwords' => array('The')
)
)
)), true);

$index->getSettings()->setNumberOfReplicas(0);
//$index->getSettings()->setNumberOfShards(1);

$type = new Type($index, 'helloworldfuzzy');
$mapping = new Mapping($type , array(
'email' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'),
'content' => array('store' => 'yes', 'type' => 'string', 'index' => 'analyzed'),
));

$mapping->setSource(array('enabled' => false));
$type->setMapping($mapping);

$doc = new Document(1000, array('email' => '[email protected]', 'content' => 'The Fuzzy Test!'));
$type->addDocument($doc);

$doc = new Document(1001, array('email' => '[email protected]', 'content' => 'Elastica Fuzzy Test'));
$type->addDocument($doc);

// Refresh index
$index->refresh();

$fltQuery = new FuzzyLikeThis();
$fltQuery->addFields(array("email","content"));
$fltQuery->setLikeText("The");

$fltQuery->setMinSimilarity(0.1);
$fltQuery->setMaxQueryTerms(3);

// Test before analyzer applied, should return 1 result
$resultSet = $type->search($fltQuery);
$this->assertEquals(1, $resultSet->count());

$fltQuery->setParam('analyzer', 'searchAnalyzer');

$resultSet = $type->search($fltQuery);
$this->assertEquals(0, $resultSet->count());
}
}