Skip to content
Snippets Groups Projects
Unverified Commit 6c6cf06a authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

add field masking span query

parent c6427d59
No related branches found
No related tags found
No related merge requests found
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Query\Span;
use ONGR\ElasticsearchDSL\ParametersTrait;
/**
* Elasticsearch span within query.
*
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-field-masking-query.html
*/
class FieldMaskingSpanQuery implements SpanQueryInterface
{
use ParametersTrait;
/**
* @var SpanQueryInterface
*/
private $query;
/**
* @var string
*/
private $field;
/**
* @param string $field
* @param SpanQueryInterface $query
*/
public function __construct($field, SpanQueryInterface $query)
{
$this->setQuery($query);
$this->setField($field);
}
/**
* @return mixed
*/
public function getQuery()
{
return $this->query;
}
/**
* @param mixed $query
* @return self
*/
public function setQuery($query)
{
$this->query = $query;
return $this;
}
/**
* @return string
*/
public function getField()
{
return $this->field;
}
/**
* @param string $field
* @return FieldMaskingSpanQuery
*/
public function setField($field)
{
$this->field = $field;
return $this;
}
/**
* {@inheritdoc}
*/
public function toArray()
{
$output = [
'query' => $this->getQuery()->toArray(),
'field' => $this->getField(),
];
$output = $this->processArray($output);
return [$this->getType() => $output];
}
/**
* {@inheritdoc}
*/
public function getType()
{
return 'field_masking_span';
}
}
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchDSL\Tests\Unit\Query\Span;
use ONGR\ElasticsearchDSL\Query\Span\FieldMaskingSpanQuery;
use ONGR\ElasticsearchDSL\Query\Span\SpanNearQuery;
use ONGR\ElasticsearchDSL\Query\Span\SpanTermQuery;
/**
* Unit test for FieldMaskingSpanQuery.
*/
class FieldMaskingSpanQueryTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests for toArray().
*/
public function testToArray()
{
$spanTermQuery = new SpanTermQuery('text', 'quick brown');
$spanTermQueryForMask = new SpanTermQuery('text.stems', 'fox');
$fieldMaskingSpanQuery = new FieldMaskingSpanQuery('text', $spanTermQueryForMask);
$spanNearQuery = new SpanNearQuery();
$spanNearQuery->addQuery($spanTermQuery);
$spanNearQuery->addQuery($fieldMaskingSpanQuery);
$spanNearQuery->setSlop(5);
$spanNearQuery->addParameter('in_order', false);
$result = [
'span_near' => [
'clauses' => [
[
'span_term' => [ 'text' => 'quick brown']
],
[
'field_masking_span' => [
'query' => [ 'span_term' => [ 'text.stems' => 'fox' ] ],
'field' => 'text',
]
]
],
'slop' => 5,
'in_order' => false,
],
];
$this->assertEquals($result, $spanNearQuery->toArray());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment