Skip to content

Commit 334e6c7

Browse files
committed
Field level boosting is added.
1 parent 302b0c5 commit 334e6c7

File tree

5 files changed

+62
-21
lines changed

5 files changed

+62
-21
lines changed

src/Nqxcode/LuceneSearch/Model/Config.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,22 @@ public function classUidPair(Model $model)
144144
*/
145145
public function fields(Model $model)
146146
{
147+
$fields = [];
147148
$c = $this->config($model);
148-
return $c['fields'];
149+
150+
foreach ($c['fields'] as $key => $value) {
151+
$boost = 1;
152+
$field = $value;
153+
154+
if (is_array($value)) {
155+
$boost = array_get($value, "boost");
156+
$field = $key;
157+
}
158+
159+
$fields[$field] = ['boost' => $boost];
160+
}
161+
162+
return $fields;
149163
}
150164

151165
/**
@@ -181,6 +195,17 @@ function ($i) use ($field) {
181195
}
182196
}
183197

198+
$attributes = array_map(function ($value) {
199+
$boost = 1;
200+
201+
if (is_array($value)) {
202+
$boost = array_get($value, "boost");
203+
$value = array_get($value, "value");
204+
}
205+
206+
return ['boost' => $boost, 'value' => $value];
207+
}, $attributes);
208+
184209
return $attributes;
185210
}
186211

src/Nqxcode/LuceneSearch/Search.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,25 @@ public function update(Model $model)
118118
$fields = $this->config->fields($model);
119119

120120
// Add fields to document to be indexed (but not stored).
121-
foreach ($fields as $field) {
122-
$doc->addField(Field::unStored(trim($field), strip_tags(trim($model->{trim($field)}))));
121+
foreach ($fields as $fieldName => $options) {
122+
$fieldValue = $model->{trim($fieldName)};
123+
124+
$field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)));
125+
$field->boost = array_get($options, 'boost');
126+
127+
$doc->addField($field);
123128
}
124129

125130
$optionalAttributes = $this->config->optionalAttributes($model);
126131

127132
// Add optional attributes to document to be indexed (but not stored).
128-
foreach ($optionalAttributes as $fieldName => $fieldValue) {
129-
$doc->addField(Field::unStored(trim($fieldName), strip_tags(trim($fieldValue))));
133+
foreach ($optionalAttributes as $fieldName => $options) {
134+
$fieldValue = array_get($options, "value");
135+
136+
$field = Field::unStored(trim($fieldName), strip_tags(trim($fieldValue)));
137+
$field->boost = array_get($options, "boost");
138+
139+
$doc->addField($field);
130140
}
131141

132142
// Add document to index.

tests/functional/BaseTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ protected function configure()
2424
[
2525
'tests\models\Product' => [
2626
'fields' => [
27-
'name',
28-
'description'
27+
'name' => ['boost' => 1],
28+
'description' => ['boost' => 0.2],
2929
],
3030
'optional_attributes' => true
3131
]

tests/unit/ConfigTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public function testModelWithIncorrectClassUid()
7878
public function testFields()
7979
{
8080
$fields = $this->config->fields($this->productRepoMock);
81-
$this->assertEquals(['name', 'description'], $fields);
81+
$this->assertEquals(['name' => ['boost' => 1], 'description' => ['boost' => 1]], $fields);
8282

8383
$fields = $this->config->fields($this->dummyRepoMock);
84-
$this->assertEquals(['first_field', 'second_field'], $fields);
84+
$this->assertEquals(['first_field' => ['boost' => 1], 'second_field' => ['boost' => 1]], $fields);
8585
}
8686

8787
public function testClassUid()

tests/unit/SearchTest.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public function setUp()
3838
->andReturn(['class_uid', '12345']);
3939
$this->config->shouldReceive('fields')
4040
->with($this->model)
41-
->andReturn(['name']);
41+
->andReturn(['name' => ['boost' => 1]]);
4242

4343
$this->config->shouldReceive('optionalAttributes')
44-
->andReturn(['optional_attribute1' => 'optional value']);
44+
->andReturn(['optional_attribute1' => ['boost' => 1, 'value' => 'optional value']]);
4545

4646
}
4747

@@ -52,8 +52,14 @@ public function testUpdate()
5252
$doc = new Document();
5353
$doc->addField(Field::keyword('private_key', 1));
5454
$doc->addField(Field::Keyword('class_uid', '12345'));
55-
$doc->addField(Field::unStored('name', 'test name'));
56-
$doc->addField(Field::unStored('optional_attribute1', 'optional value'));
55+
56+
$field = Field::unStored('name', 'test name');
57+
$field->boost = 1;
58+
$doc->addField($field);
59+
60+
$field = Field::unStored('optional_attribute1', 'optional value');
61+
$field->boost = 1;
62+
$doc->addField($field);
5763

5864
$this->assertEquals($doc, $arg);
5965
return true;
@@ -67,10 +73,10 @@ public function testUpdate()
6773
$this->assertEquals($term, $arg);
6874
return true;
6975
}))->andReturnUsing(function () {
70-
$hitMock = m::mock();
71-
$hitMock->id = 10;
72-
return [$hitMock];
73-
})->once();
76+
$hitMock = m::mock();
77+
$hitMock->id = 10;
78+
return [$hitMock];
79+
})->once();
7480

7581
$luceneIndex->shouldReceive('delete')->with(10)->once();
7682

@@ -91,10 +97,10 @@ public function testDelete()
9197
$this->assertEquals($term, $arg);
9298
return true;
9399
}))->andReturnUsing(function () {
94-
$hitMock = m::mock();
95-
$hitMock->id = 10;
96-
return [$hitMock];
97-
});
100+
$hitMock = m::mock();
101+
$hitMock->id = 10;
102+
return [$hitMock];
103+
});
98104

99105
$luceneIndex->shouldReceive('delete')->with(10)->once();
100106

0 commit comments

Comments
 (0)