summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/test/lib/Elastica/Test/Query/SimpleQueryStringTest.php
blob: eff9b8a1abed1c408538d4ce1c66db4d08346fa7 (plain)
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
<?php

namespace Elastica\Test\Query;


use Elastica\Document;
use Elastica\Index;
use Elastica\Query\SimpleQueryString;
use Elastica\Test\Base;

class SimpleQueryStringTest extends Base
{
    /**
     * @var Index
     */
    protected $_index;

    protected function setUp()
    {
        parent::setUp();
        $this->_index = $this->_createIndex("simple_query_string_test");
        $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'))
        );
        $this->_index->getType("guitars")->addDocuments($docs);
        $this->_index->refresh();
    }

    protected function tearDown()
    {
        parent::tearDown();
        $this->_index->delete();
    }

    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());
    }

    public function testQuery()
    {
        $query = new SimpleQueryString("gibson +sg +-faded", array("make", "model"));
        $results = $this->_index->search($query);

        $this->assertEquals(2, $results->getTotalHits());

        $query->setFields(array("model"));
        $results = $this->_index->search($query);

        // We should not get any hits, since the "make" field was not included in the query.
        $this->assertEquals(0, $results->getTotalHits());
    }
}