-
Notifications
You must be signed in to change notification settings - Fork 8
/
LuceneHandler.inc.php
196 lines (171 loc) · 6.25 KB
/
LuceneHandler.inc.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/**
* @file LuceneHandler.inc.php
*
* Copyright (c) 2014-2020 Simon Fraser University
* Copyright (c) 2003-2020 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class LuceneHandler
* @ingroup plugins_generic_lucene
*
* @brief Handle lucene AJAX and XML requests (auto-completion, pull indexation, etc.)
*/
import('classes.handler.Handler');
import('plugins.generic.lucene.classes.SolrWebService');
import('lib.pkp.classes.core.JSONMessage');
import('classes.search.ArticleSearch');
class LuceneHandler extends Handler {
//
// Public operations
//
/**
* AJAX request for search query auto-completion.
* @param $args array
* @param $request Request
* @return JSONMessage JSON object
*/
function queryAutocomplete($args, $request) {
$this->validate(null, $request);
// Check whether auto-suggest is enabled.
$suggestionList = array();
$lucenePlugin = $this->_getLucenePlugin();
$enabled = (bool)$lucenePlugin->getSetting(CONTEXT_SITE, 'autosuggest');
if ($enabled) {
// Retrieve search criteria from the user input.
$articleSearch = new ArticleSearch();
$searchFilters = $articleSearch->getSearchFilters($request);
// Get the autosuggest input and remove it from
// the filter array.
$autosuggestField = $request->getUserVar('searchField');
$userInput = isset($searchFilters[$autosuggestField]) ? $searchFilters[$autosuggestField] : $searchFilters['query'];
if (isset($searchFilters[$autosuggestField])) {
unset($searchFilters[$autosuggestField]);
}
// Instantiate a search request.
$searchRequest = new SolrSearchRequest();
$searchRequest->setJournal($searchFilters['searchJournal']);
$searchRequest->setFromDate($searchFilters['fromDate']);
$searchRequest->setToDate($searchFilters['toDate']);
$keywords = $articleSearch->getKeywordsFromSearchFilters($searchFilters);
$searchRequest->addQueryFromKeywords($keywords);
// Get the web service.
$solrWebService = $lucenePlugin->getSolrWebService(); /* @var $solrWebService SolrWebService */
$suggestions = $solrWebService->getAutosuggestions(
$searchRequest, $autosuggestField, $userInput,
(int)$lucenePlugin->getSetting(CONTEXT_SITE, 'autosuggestType')
);
// Prepare a suggestion list as understood by the
// autocomplete JS handler.
foreach($suggestions as $suggestion) {
$suggestionList[] = array('label' => $suggestion, 'value' => $suggestion);
}
}
// Return the suggestions as JSON message.
return new JSONMessage(true, $suggestionList);
}
/**
* If pull-indexing is enabled then this handler returns
* article metadata in a formate that can be consumed by
* the Solr data import handler.
* @param $args array
* @param $request Request
* @return JSON string
*/
function pullChangedArticles($args, $request) {
$this->validate(null, $request);
// Do not allow access to this operation from journal context.
$router = $request->getRouter();
$journal = $router->getContext($request);
if (!is_null($journal)) {
// Redirect to the index context. We do this so that providers
// can secure a single entry point when providing subscription-only
// content.
$request->redirect('index', 'lucene', 'pullChangedArticles');
}
// Die if pull indexing is disabled.
$lucenePlugin = $this->_getLucenePlugin();
if (!$lucenePlugin->getSetting(CONTEXT_SITE, 'pullIndexing')) die(__('plugins.generic.lucene.message.pullIndexingDisabled'));
// Execute the pull indexing transaction.
$solrWebService = $lucenePlugin->getSolrWebService(); /* @var $solrWebService SolrWebService */
$solrWebService->pullChangedArticles(
array($this, 'pullIndexingCallback'), SOLR_INDEXING_MAX_BATCHSIZE
);
}
/**
* If the "similar documents" feature is enabled then this
* handler redirects to a search query that shows documents
* similar to the one identified by an article id in the
* request.
* @param $args array
* @param $request Request
*/
function similarDocuments($args, &$request) {
$this->validate(null, $request);
// Retrieve the ID of the article that
// we want similar documents for.
$articleId = $request->getUserVar('articleId');
// Check error conditions.
// - The "similar documents" feature is not enabled.
// - We got a non-numeric article ID.
$lucenePlugin = $this->_getLucenePlugin();
if (!($lucenePlugin->getSetting(0, 'simdocs')
&& is_numeric($articleId))) {
$request->redirect(null, 'search');
}
// Identify "interesting" terms of the
// given article.
$solrWebService = $lucenePlugin->getSolrWebService(); /* @var $solrWebService SolrWebService */
$searchTerms = $solrWebService->getInterestingTerms($articleId);
if (empty($searchTerms)) {
$request->redirect(null, 'search');
}
// Redirect to a search query with these
// terms.
$searchParams = array(
'query' => implode(' ', $searchTerms),
);
$request->redirect(null, 'search', 'search', null, $searchParams);
}
//
// Public methods
//
/**
* Return XML with index changes to the Solr server
* where it will be stored for later processing.
*
* @param $articleXml string The XML with index changes
* to be transferred to the Solr server.
* @param $batchCount integer The number of articles in
* the XML list (i.e. the expected number of documents
* to be indexed).
* @param $numDeleted integer The number of articles in
* the XML list that are marked for deletion.
*
* @return integer The number of articles processed.
*/
function pullIndexingCallback($articleXml, $batchCount, $numDeleted) {
// Flush the XML to the Solr server to make sure it
// arrives there before we commit our transaction.
echo $articleXml;
flush();
// We assume that when the flush succeeds that
// all changed documents will eventually be indexed.
// By implementing a rejection mechanism on the server
// we make sure this actually happens (or that we at
// least realize if something goes wrong). If this
// is not working in practice then we'll have to
// implement a real application-level two-way handshake.
return $batchCount;
}
//
// Private helper methods
//
/**
* Get the lucene plugin object
* @return LucenePlugin
*/
function _getLucenePlugin() {
return PluginRegistry::getPlugin('generic', LUCENE_PLUGIN_NAME);
}
}