1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
<?php
namespace Elastica\Test\Query;
use Elastica\Document;
use Elastica\Query\SimpleQueryString;
use Elastica\Test\Base;
class SimpleQueryStringTest extends Base
{
/**
* @group unit
*/
public function testToArray()
{
$string = 'this is a test';
$fields = array('field1', 'field2');
$query = new SimpleQueryString($string, $fields);
$query->setDefaultOperator(SimpleQueryString::OPERATOR_OR);
$query->setAnalyzer('whitespace');
$expected = array(
'simple_query_string' => array(
'query' => $string,
'fields' => $fields,
'analyzer' => 'whitespace',
'default_operator' => SimpleQueryString::OPERATOR_OR,
),
);
$this->assertEquals($expected, $query->toArray());
}
/**
* @group functional
*/
public function testQuery()
{
$index = $this->_createIndex();
$docs = array(
new Document(1, array('make' => 'Gibson', 'model' => 'Les Paul')),
new Document(2, array('make' => 'Gibson', 'model' => 'SG Standard')),
new Document(3, array('make' => 'Gibson', 'model' => 'SG Supreme')),
new Document(4, array('make' => 'Gibson', 'model' => 'SG Faded')),
new Document(5, array('make' => 'Fender', 'model' => 'Stratocaster')),
);
$index->getType('guitars')->addDocuments($docs);
$index->refresh();
$query = new SimpleQueryString('gibson +sg +-faded', array('make', 'model'));
$results = $index->search($query);
$this->assertEquals(2, $results->getTotalHits());
$query->setFields(array('model'));
$results = $index->search($query);
// We should not get any hits, since the "make" field was not included in the query.
$this->assertEquals(0, $results->getTotalHits());
}
/**
* @group unit
*/
public function testSetMinimumShouldMatch()
{
$expected = array(
'simple_query_string' => array(
'query' => 'DONT PANIC',
'minimum_should_match' => '75%',
),
);
$query = new SimpleQueryString($expected['simple_query_string']['query']);
$query->setMinimumShouldMatch($expected['simple_query_string']['minimum_should_match']);
$this->assertEquals($expected, $query->toArray());
$this->assertInstanceOf('Elastica\Query\SimpleQueryString', $query->setMinimumShouldMatch('75%'));
}
/**
* @group functional
*/
public function testSetMinimumShouldMatchWorks()
{
$index = $this->_createIndex();
$type = $index->getType('foobars');
$type->addDocuments(array(
new Document(1, array('body' => 'foo')),
new Document(2, array('body' => 'bar')),
new Document(3, array('body' => 'foo bar')),
new Document(4, array('body' => 'foo baz bar')),
));
$index->refresh();
$query = new SimpleQueryString('foo bar');
$query->setMinimumShouldMatch(2);
$results = $type->search($query);
$this->assertCount(2, $results);
$this->assertEquals(3, $results[0]->getId());
$this->assertEquals(4, $results[1]->getId());
}
}
|