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    const OPERATOR_AND = 'and';
13    const OPERATOR_OR = 'or';
14
15    /**
16     * @param string $query
17     * @param array  $fields
18     */
19    public function __construct(string $query, array $fields = [])
20    {
21        $this->setQuery($query);
22        if (0 < \count($fields)) {
23            $this->setFields($fields);
24        }
25    }
26
27    /**
28     * Set the querystring for this query.
29     *
30     * @param string $query see ES documentation for querystring syntax
31     *
32     * @return $this
33     */
34    public function setQuery(string $query): self
35    {
36        return $this->setParam('query', $query);
37    }
38
39    /**
40     * @param string[] $fields the fields on which to perform this query. Defaults to index.query.default_field.
41     *
42     * @return $this
43     */
44    public function setFields(array $fields): self
45    {
46        return $this->setParam('fields', $fields);
47    }
48
49    /**
50     * Set the default operator to use if no explicit operator is defined in the query string.
51     *
52     * @param string $operator see OPERATOR_* constants for options
53     *
54     * @return $this
55     */
56    public function setDefaultOperator(string $operator = self::OPERATOR_OR): self
57    {
58        return $this->setParam('default_operator', $operator);
59    }
60
61    /**
62     * Set the analyzer used to analyze each term of the query.
63     *
64     * @param string $analyzer
65     *
66     * @return $this
67     */
68    public function setAnalyzer(string $analyzer): self
69    {
70        return $this->setParam('analyzer', $analyzer);
71    }
72
73    /**
74     * Set minimum_should_match option.
75     *
76     * @param int|string $minimumShouldMatch
77     *
78     * @return $this
79     */
80    public function setMinimumShouldMatch($minimumShouldMatch): self
81    {
82        return $this->setParam('minimum_should_match', $minimumShouldMatch);
83    }
84}
85