Fix facets on non string fields & fix raw with "in" query

This commit is contained in:
Mathieu Darse
2015-07-23 18:41:10 +02:00
parent a31442368b
commit a59c42980c
3 changed files with 52 additions and 3 deletions

View File

@@ -34,12 +34,12 @@ class RawNode extends Node
foreach ($fields as $field) { foreach ($fields as $field) {
$index_fields[] = $field->getIndexField(true); $index_fields[] = $field->getIndexField(true);
} }
$query = []; $query = null;
if (count($index_fields) > 1) { if (count($index_fields) > 1) {
$query['multi_match']['query'] = $this->text; $query['multi_match']['query'] = $this->text;
$query['multi_match']['fields'] = $index_fields; $query['multi_match']['fields'] = $index_fields;
$query['multi_match']['analyzer'] = 'keyword'; $query['multi_match']['analyzer'] = 'keyword';
} else { } elseif (count($index_fields) === 1) {
$index_field = reset($index_fields); $index_field = reset($index_fields);
$query['term'][$index_field] = $this->text; $query['term'][$index_field] = $this->text;
} }

View File

@@ -101,7 +101,7 @@ class Field
'%scaption.%s%s', '%scaption.%s%s',
$this->is_private ? 'private_' : '', $this->is_private ? 'private_' : '',
$this->name, $this->name,
$raw ? '.raw' : '' $raw && $this->type === Mapping::TYPE_STRING ? '.raw' : ''
); );
} }

View File

@@ -20,4 +20,53 @@ class RawNodeTest extends \PHPUnit_Framework_TestCase
$node = new RawNode('foo'); $node = new RawNode('foo');
$this->assertEquals('<raw:"foo">', (string) $node); $this->assertEquals('<raw:"foo">', (string) $node);
} }
public function testQueryBuildOnSingleField()
{
$field = $this->prophesize(Field::class);
$field->getIndexField(true)->willReturn('foo.raw');
$query_context = $this->prophesize(QueryContext::class);
$query_context->getUnrestrictedFields()->willReturn([$field->reveal()]);
$query_context->getPrivateFields()->willReturn([]);
$node = new RawNode('bar');
$query = $node->buildQuery($query_context->reveal());
$expected = '{
"term": {
"foo.raw": "bar"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
public function testQueryBuildOnMultipleFields()
{
$field_a = $this->prophesize(Field::class);
$field_a->getIndexField(true)->willReturn('foo.raw');
$field_b = $this->prophesize(Field::class);
$field_b->getIndexField(true)->willReturn('bar.raw');
$query_context = $this->prophesize(QueryContext::class);
$query_context->getUnrestrictedFields()->willReturn([
$field_a->reveal(),
$field_b->reveal()
]);
$query_context->getPrivateFields()->willReturn([]);
$node = new RawNode('baz');
$query = $node->buildQuery($query_context->reveal());
$expected = '{
"multi_match": {
"query": "baz",
"fields": ["foo.raw", "bar.raw"],
"analyzer": "keyword"
}
}';
$this->assertEquals(json_decode($expected, true), $query);
}
} }