Completed
Push — master ( 5bd694...1eeab7 )
by Maxence
01:43
created

ElasticSearchService::onRemovingIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
/**
3
 * Files_FullTextSearch - Index the content of your files
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2018
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Files_FullTextSearch\Service;
28
29
30
use OCA\FullTextSearch\IFullTextSearchPlatform;
31
use OCA\FullTextSearch\Model\SearchRequest;
32
33
class ElasticSearchService {
34
35
	/** @var MiscService */
36
	private $miscService;
37
38
39
	/**
40
	 * ElasticSearchService constructor.
41
	 *
42
	 * @param MiscService $miscService
43
	 *
44
	 * @internal param IProviderFactory $factory
45
	 */
46
	public function __construct(MiscService $miscService) {
47
		$this->miscService = $miscService;
48
	}
49
50
51
	/**
52
	 * @param IFullTextSearchPlatform $platform
53
	 */
54
	public function onInitializingIndex(IFullTextSearchPlatform $platform) {
55
		if ($platform->getId() !== 'elastic_search') {
56
			return;
57
		}
58
	}
59
60
61
	/**
62
	 * @param IFullTextSearchPlatform $platform
63
	 */
64
	public function onResettingIndex(IFullTextSearchPlatform $platform) {
65
		if ($platform->getId() !== 'elastic_search') {
66
			return;
67
		}
68
	}
69
70
71
	/**
72
	 * @param IFullTextSearchPlatform $platform
73
	 * @param array $arr
74
	 */
75
	public function onIndexingDocument(IFullTextSearchPlatform $platform, &$arr) {
0 ignored issues
show
Unused Code introduced by
The parameter $arr is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
76
		if ($platform->getId() !== 'elastic_search') {
77
			return;
78
		}
79
	}
80
81
82
	/**
83
	 * @param IFullTextSearchPlatform $platform
84
	 * @param SearchRequest $request
85
	 * @param array $arr
86
	 */
87
	public function onSearchingQuery(IFullTextSearchPlatform $platform, SearchRequest $request, &$arr) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 101 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
88
		if ($platform->getId() !== 'elastic_search') {
89
			return;
90
		}
91
92
		$this->searchQueryShareNames($request, $arr);
93
		$this->searchQueryShareOptions($request, $arr);
94
	}
95
96
97
	/**
98
	 * @param SearchRequest $request
99
	 * @param array $arr
100
	 */
101
	private function searchQueryShareNames(SearchRequest $request, &$arr) {
102
		$query = [];
103
		$words = explode(' ', $request->getSearch());
104
		foreach ($words as $word) {
105
			array_push(
106
				$query, ['wildcard' => ['share_names.' . $request->getAuthor() => '*' . $word . '*']]
107
			);
108
		}
109
110
		array_push($arr['params']['body']['query']['bool']['must']['bool']['should'], $query);
111
	}
112
113
114
	/**
115
	 * @param SearchRequest $request
116
	 * @param array $arr
117
	 */
118
	private function searchQueryShareOptions(SearchRequest $request, &$arr) {
119
		$this->searchQueryShareOptionsExtension($request, $arr);
120
	}
121
122
123
	private function searchQueryShareOptionsExtension(SearchRequest $request, &$arr) {
124
		$extension = $request->getOption('files_extension');
125
		if ($extension === '') {
126
			return;
127
		}
128
129
		$query = [
130
			['wildcard' => ['share_names.' . $request->getAuthor() => '*' . $extension]],
131
			['wildcard' => ['title' => '*' . $extension]]
132
		];
133
134
		$arr['params']['body']['query']['bool']['filter'][]['bool']['should'] = $query;
135
	}
136
137
138
}
139