-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.php
156 lines (141 loc) · 5.83 KB
/
Library.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* _ __ __ _____ _____ ___ ____ _____
* | | / // // ___//_ _// || __||_ _|
* | |/ // /(__ ) / / / /| || | | |
* |___//_//____/ /_/ /_/ |_||_| |_|
* @link https://vistart.name/
* @copyright Copyright (c) 2016-2018 vistart
* @license https://vistart.name/license/
*/
namespace rhoone\library\providers\huiwen;
use yii\elasticsearch\ActiveDataProvider;
/**
* Class Library
* @package rhoone\library\providers\huiwen
*/
abstract class Library extends \rhoone\library\Library implements ILibraryQueryOperation
{
public $marcClass = \rhoone\library\providers\huiwen\models\elasticsearch\Marc::class;
public function search($keywords, array $config = null)
{
//$queryArray = $this->buildQueryArray($keywords);
$config['keywords'] = $keywords;
$queryBuilder = new $this->queryBuilderClass($config);
/* @var $queryBuilder QueryBuilder */
/* @var $query \rhoone\library\providers\huiwen\models\elasticsearch\MarcQuery */
$query = $this->marcClass::find()->query($this->buildQueryArray($queryBuilder->seperatedWords, $queryBuilder))->highlight($queryBuilder->highlight);
\Yii::info("Seperated Words: " . implode(", ", $queryBuilder->seperatedWords));
$provider = new ActiveDataProvider([
'query' => $query,
]);
return $provider;
}
/**
* @param array $keywords
* @param QueryBuilder $queryBuilder
* @return array
*/
public function buildQueryArray(array $keywords, QueryBuilder $queryBuilder)
{
$queryArray = ['bool' => [
'boost' => 1,
]];
$filter = [];
$callNos = ['bool' => ['should' => []]];
$ISBNs = ['bool' => ['should' => []]];
$barcodes = ['bool' => ['should' => []]];
$marcNos = ['bool' => ['should' => []]];
$should = [];
$titles = [];
$authors = [];
$presses = [];
$subjects = [];
$notes = [];
foreach ($keywords as $keyword)
{
if ($this->queryCallNo($keyword)->exists())
{
$callNos['bool']['should'][] = $queryBuilder->buildWildcardQueryClause('copies.call_no', "*$keyword*");
\Yii::info("Keyword: `$keyword`, Call No matched.");
}
if ($this->queryISBN($keyword)->exists())
{
$ISBNs['bool']['should'][] = $queryBuilder->buildWildcardQueryClause('ISBNs.compressed', sprintf("*%s*", str_replace([' ', '-'], '', $keyword)));
\Yii::info("Keyword: `$keyword`, ISBN matched.");
}
if ($this->queryBarcode($keyword)->exists())
{
$barcodes['bool']['should'][] = $queryBuilder->buildWildcardQueryClause('copies.barcode', "*$keyword*");
\Yii::info("Keyword: `$keyword`, Barcode matched.");
}
if ($this->queryMarcNo($keyword)->exists())
{
$marcNos['bool']['should'][] = $queryBuilder->buildTermQueryClause('marc_no', $keyword);
\Yii::info("Keyword: `$keyword`, MARC NO matched.");
}
if ($this->queryTitle($keyword)->exists())
{
$titles[] = $queryBuilder->buildMatchPhraseClause('titles.value', $keyword, ['boost' => 6]);
\Yii::info("Keyword: `$keyword`, title phrase matched.");
}
if ($this->queryAuthor($keyword)->exists())
{
$authors[] = $queryBuilder->buildMatchPhraseClause('authors.author', $keyword, ['boost' => 5]);
\Yii::info("Keyword: `$keyword`, author matched.");
}
if ($this->queryPress($keyword)->exists())
{
$presses[] = $queryBuilder->buildMatchPhraseClause('presses.press', $keyword, ['boost' => 3]);
\Yii::info("Keyword: `$keyword`, press matched.");
}
if ($this->querySubject($keyword)->exists())
{
$subjects[] = $queryBuilder->buildMatchClause('subjects.value', $keyword);
\Yii::info("Keyword: `$keyword`, subject matched.");
}
}
if (!empty($callNos['bool']['should'])) {
$filter[] = $callNos;
}
/*
if (!empty($ISBNs['bool']['should'])) {
$filter[] = $ISBNs;
}*/
if (!empty($barcodes['bool']['should'])) {
$filter[] = $barcodes;
}
if (!empty($marcNos['bool']['should'])) {
$filter[] = $marcNos;
}
if (!empty($filter)) {
$queryArray['bool']['filter'][] = $filter;
}
if (empty($titles) && empty($authors) && empty($presses) && empty($subjects)) {
\Yii::info("Keyword: `$keyword`, failed to match title, author, press and subject." );
foreach ($keywords as $keyword) {
if ($this->querySubject($keyword)->exists())
{
$subjects[] = $queryBuilder->buildWildcardQueryClause('subjects.value', $keyword);
\Yii::info("Keyword: `$keyword`, subject matched.");
}
}
if (empty($subjects)) {
foreach ($keywords as $keyword) {
if ($this->queryNote($keyword)->exists())
{
$notes[] = $queryBuilder->buildMatchPhraseClause('notes.value', $keyword);
\Yii::info("Keyword: `$keyword`, note matched.");
}
}
}
}
$should = array_merge($should, $titles, $authors, $presses, $subjects, $notes, $ISBNs['bool']['should']);
if (empty($should) && empty($filter)) {
$queryArray = ['match_none' => (Object)array()];
} else {
$queryArray['bool']['should'] = $should;
}
return $queryArray;
}
}