Update elasticsearch and SphinxSearch classes

This commit is contained in:
DariusIII
2022-05-01 21:41:03 +02:00
parent c92e2b1a87
commit 14c5c221bf
3 changed files with 50 additions and 94 deletions
+41 -87
View File
@@ -18,6 +18,7 @@ class ElasticSearchSiteSearch
public function indexSearch(array|string $phrases, int $limit): mixed
{
$keywords = $this->sanitize($phrases);
try {
$search = [
'scroll' => '30s',
@@ -26,7 +27,7 @@ class ElasticSearchSiteSearch
'query' => [
'query_string' => [
'query' => $keywords,
'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name'],
'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name', 'categories_id'],
'analyze_wildcard' => true,
'default_operator' => 'and',
],
@@ -43,27 +44,7 @@ class ElasticSearchSiteSearch
],
];
$results = \Elasticsearch::search($search);
$searchResult = [];
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
foreach ($results['hits']['hits'] as $result) {
$searchResult[] = $result['_source']['id'];
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $results['_scroll_id'];
// Execute a Scroll request and repeat
$results = \Elasticsearch::scroll([
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id
'scroll' => '30s', // and the same timeout window
]
);
}
return $searchResult;
return $this->search($search);
} catch (BadRequest400Exception $request400Exception) {
return [];
}
@@ -102,27 +83,7 @@ class ElasticSearchSiteSearch
],
];
$results = \Elasticsearch::search($search);
$searchResult = [];
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
foreach ($results['hits']['hits'] as $result) {
$searchResult[] = $result['_source']['id'];
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $results['_scroll_id'];
// Execute a Scroll request and repeat
$results = \Elasticsearch::scroll([
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id
'scroll' => '30s', // and the same timeout window
]
);
}
return $searchResult;
return $this->search($search);
} catch (BadRequest400Exception $request400Exception) {
return [];
}
@@ -163,27 +124,7 @@ class ElasticSearchSiteSearch
],
];
$results = \Elasticsearch::search($search);
$searchResult = [];
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
foreach ($results['hits']['hits'] as $result) {
$searchResult[] = $result['_source']['id'];
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $results['_scroll_id'];
// Execute a Scroll request and repeat
$results = \Elasticsearch::scroll([
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id
'scroll' => '30s', // and the same timeout window
]
);
}
return $searchResult;
return $this->search($search);
} catch (BadRequest400Exception $request400Exception) {
return [];
}
@@ -212,29 +153,7 @@ class ElasticSearchSiteSearch
],
];
$results = \Elasticsearch::search($search);
$ids = [];
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
foreach ($results['hits']['hits'] as $result) {
$ids[] = $result['_source']['id'];
}
if (empty($ids)) {
return collect();
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $results['_scroll_id'];
// Execute a Scroll request and repeat
$results = \Elasticsearch::scroll([
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id
'scroll' => '30s', // and the same timeout window
]
);
}
return $ids;
return $this->search($search);
} catch (BadRequest400Exception $request400Exception) {
return [];
}
@@ -408,4 +327,39 @@ class ElasticSearchSiteSearch
return implode(' ', $keywords);
}
/**
* @param array $search
* @param bool $fullResults
* @return array|\Illuminate\Support\Collection
*/
protected function search(array $search, bool $fullResults = false): array|\Illuminate\Support\Collection
{
$results = \Elasticsearch::search($search);
$searchResult = [];
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
foreach ($results['hits']['hits'] as $result) {
if ($fullResults === true) {
$searchResult[] = $result['_source'];
} else {
$searchResult[] = $result['_source']['id']; }
}
// When done, get the new scroll_id
// You must always refresh your _scroll_id! It can change sometimes
$scroll_id = $results['_scroll_id'];
// Execute a Scroll request and repeat
$results = \Elasticsearch::scroll([
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id
'scroll' => '30s', // and the same timeout window
]
);
}
if (empty($searchResult)) {
return collect();
}
return $searchResult;
}
}
+8 -6
View File
@@ -142,7 +142,7 @@ class SphinxSearch
->leftJoin('release_files as rf', 'releases.id', '=', 'rf.releases_id')
->select(['releases.id', 'releases.name', 'releases.searchname', 'releases.fromname', 'releases.categories_id', DB::raw('IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename')])
->groupBy('releases.id')
->first();
->first()->toArray();
if ($new !== null) {
$this->insertRelease($new);
@@ -164,10 +164,12 @@ class SphinxSearch
}
/**
* Truncate the RT index.
*
* @param array $indexes
* @return bool
*
* @throws \Foolz\SphinxQL\Exception\ConnectionException
* @throws \Foolz\SphinxQL\Exception\DatabaseException
* @throws \Foolz\SphinxQL\Exception\SphinxQLException
*/
public function truncateRTIndex(array $indexes = []): bool
{
@@ -178,7 +180,7 @@ class SphinxSearch
}
foreach ($indexes as $index) {
if (\in_array($index, $this->config['indexes'], true)) {
$this->helper->truncateRtIndex($index);
$this->helper->truncateRtIndex($index)->execute();
$this->cli->info('Truncating index '.$index.' finished.');
} else {
$this->cli->error('Unsupported index: '.$index);
@@ -194,8 +196,8 @@ class SphinxSearch
public function optimizeRTIndex(): void
{
foreach ($this->config['indexes'] as $index) {
$this->helper->flushRtIndex($index);
$this->helper->optimizeIndex($index);
$this->helper->flushRtIndex($index)->execute();
$this->helper->optimizeIndex($index)->execute();
}
}
+1 -1
View File
@@ -36,7 +36,7 @@ $releases_index = [
],
],
'categories_id' => [
'type' => 'long',
'type' => 'text',
],
'fromname' => [
'type' => 'text',