1<?php
2
3namespace Elastica\Query;
4
5/**
6 * Class SimpleQueryString.
7 *
8 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
9 */
10class SimpleQueryString extends AbstractQuery
11{
12    public const OPERATOR_AND = 'and';
13    public const OPERATOR_OR = 'or';
14
15    public function __construct(string $query, array $fields = [])
16    {
17        $this->setQuery($query);
18        if (0 < \count($fields)) {
19            $this->setFields($fields);
20        }
21    }
22
23    /**
24     * Set the querystring for this query.
25     *
26     * @param string $query see ES documentation for querystring syntax
27     *
28     * @return $this
29     */
30    public function setQuery(string $query): self
31    {
32        return $this->setParam('query', $query);
33    }
34
35    /**
36     * @param string[] $fields the fields on which to perform this query. Defaults to index.query.default_field.
37     *
38     * @return $this
39     */
40    public function setFields(array $fields): self
41    {
42        return $this->setParam('fields', $fields);
43    }
44
45    /**
46     * Set the default operator to use if no explicit operator is defined in the query string.
47     *
48     * @param string $operator see OPERATOR_* constants for options
49     *
50     * @return $this
51     */
52    public function setDefaultOperator(string $operator = self::OPERATOR_OR): self
53    {
54        return $this->setParam('default_operator', $operator);
55    }
56
57    /**
58     * Set the analyzer used to analyze each term of the query.
59     *
60     * @return $this
61     */
62    public function setAnalyzer(string $analyzer): self
63    {
64        return $this->setParam('analyzer', $analyzer);
65    }
66
67    /**
68     * Set minimum_should_match option.
69     *
70     * @param int|string $minimumShouldMatch
71     *
72     * @return $this
73     */
74    public function setMinimumShouldMatch($minimumShouldMatch): self
75    {
76        return $this->setParam('minimum_should_match', $minimumShouldMatch);
77    }
78}
79