MatchPhrase.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace Elastica\Query;
  3. /**
  4. * Match Phrase query.
  5. *
  6. * @author Jacques Moati <jacques@moati.net>
  7. * @author Tobias Schultze <http://tobion.de>
  8. *
  9. * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase.html
  10. */
  11. class MatchPhrase extends AbstractQuery
  12. {
  13. /**
  14. * @param string $field
  15. * @param mixed $values
  16. */
  17. public function __construct($field = null, $values = null)
  18. {
  19. if (null !== $field && null !== $values) {
  20. $this->setParam($field, $values);
  21. }
  22. }
  23. /**
  24. * Sets a param for the message array.
  25. *
  26. * @param string $field
  27. * @param mixed $values
  28. *
  29. * @return $this
  30. */
  31. public function setField($field, $values)
  32. {
  33. return $this->setParam($field, $values);
  34. }
  35. /**
  36. * Sets a param for the given field.
  37. *
  38. * @param string $field
  39. * @param string $key
  40. * @param string $value
  41. *
  42. * @return $this
  43. */
  44. public function setFieldParam($field, $key, $value)
  45. {
  46. if (!isset($this->_params[$field])) {
  47. $this->_params[$field] = [];
  48. }
  49. $this->_params[$field][$key] = $value;
  50. return $this;
  51. }
  52. /**
  53. * Sets the query string.
  54. *
  55. * @param string $field
  56. * @param string $query
  57. *
  58. * @return $this
  59. */
  60. public function setFieldQuery($field, $query)
  61. {
  62. return $this->setFieldParam($field, 'query', $query);
  63. }
  64. /**
  65. * Set field analyzer.
  66. *
  67. * @param string $field
  68. * @param string $analyzer
  69. *
  70. * @return $this
  71. */
  72. public function setFieldAnalyzer($field, $analyzer)
  73. {
  74. return $this->setFieldParam($field, 'analyzer', $analyzer);
  75. }
  76. /**
  77. * Set field boost value.
  78. *
  79. * If not set, defaults to 1.0.
  80. *
  81. * @param string $field
  82. * @param float $boost
  83. *
  84. * @return $this
  85. */
  86. public function setFieldBoost($field, $boost = 1.0)
  87. {
  88. return $this->setFieldParam($field, 'boost', (float) $boost);
  89. }
  90. }