Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the of get fulltext snippet to reduce the number of browser's request #3046

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified import/browse-indexing.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions index-alphabetic-browse_ixtheo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ function GenerateIndexForSystem {
system_flag="$1"
echo build_browse "hierarchy" "hierarchy_browse" 1 "" ${system_flag}
time build_browse "hierarchy" "hierarchy_browse" 1 "" ${system_flag}
echo build_browse "title" "title_fullStr" 1 "-Dbibleech=org.vufind.solr.indexing.StoredFieldIterator -Dsortfield=title_fullStr -Dvaluefield=title_fullStr -Dbrowse.normalizer=org.vufind.util.TitleNormalizer" ${system_flag}
time build_browse "title" "title_fullStr" 1 "-Dbibleech=org.vufind.solr.indexing.StoredFieldIterator -Dsortfield=title_fullStr -Dvaluefield=title_fullStr -Dbrowse.normalizer=org.vufind.util.TitleNormalizer" ${system_flag}
echo build_browse "title" "title_fullStr" 1 "-Dbib_field_iterator=org.vufind.solr.indexing.StoredFieldIterator -Dsortfield=title_fullStr -Dvaluefield=title_fullStr -Dbrowse.normalizer=org.vufind.util.TitleNormalizer" ${system_flag}
time build_browse "title" "title_fullStr" 1 "-Dbib_field_iterator=org.vufind.solr.indexing.StoredFieldIterator -Dsortfield=title_fullStr -Dvaluefield=title_fullStr -Dbrowse.normalizer=org.vufind.util.TitleNormalizer" ${system_flag}
echo build_browse "topic" "topic_browse" 1 "" ${system_flag}
time build_browse "topic" "topic_browse" 1 "" ${system_flag}
echo build_browse "author" "author_browse" "" 1 ${system_flag}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,26 +410,8 @@ protected function formatHighlighting($snippets) {
return $formatted_snippets;
}


public function loadAction() : JsonModel
{
$query = $this->getRequest()->getUri()->getQuery();
$parameters = [];
parse_str($query, $parameters);
$doc_id = $parameters['doc_id'];
if (empty($doc_id))
return new JsonModel([
'status' => 'EMPTY DOC_ID'
]);
$search_query = $parameters['search_query'];
if (empty($search_query))
return new JsonModel([
'status' => 'EMPTY QUERY'
]);
$verbose = isset($parameters['verbose']) && $parameters['verbose'] == '1' ? true : false;
$synonyms = isset($parameters['synonyms']) && preg_match('/lang|all/', $parameters['synonyms']) ? $parameters['synonyms'] : "";
$types_filter = isset($parameters['fulltext_types']) ? $parameters['fulltext_types'] :
implode(',', array_keys(self::description_to_text_type_map)); // Iterate over all possible types
protected function constructFulltextSnippet($doc_id, $search_query, $verbose, $synonyms, $types_filter){
$snippets['status'] = "";
$snippets['snippets'] = [];
foreach (explode(',', $types_filter) as $type_filter) {
try {
Expand All @@ -445,25 +427,72 @@ public function loadAction() : JsonModel
}
catch (\Exception $e) {
error_log($e);
return new JsonModel([
'status' => 'PROXY_ERROR'
]);
$snippets['status'] = "PROXY_ERROR";
return $snippets;
}
}
if (empty($snippets['snippets'])) {
return new JsonModel([
'status' => 'NO RESULTS'
]);
$snippets['status'] = "NO RESULTS";
return $snippets;
}
// Deduplicate snippets (array_values for fixing indices)
$snippets['snippets'] = array_values(array_unique($snippets['snippets'], SORT_REGULAR));
$snippets['snippets'] = array_slice($snippets['snippets'], 0, $this->maxSnippets);
$snippets['snippets'] = $this->formatHighlighting($snippets['snippets']);

return $snippets;
}

public function loadAction() : JsonModel
{
$query = $this->getRequest()->getUri()->getQuery();
$parameters = [];
parse_str($query, $parameters);
$snippets = [];
// keep the compatibility with old version
if(array_key_exists('doc_id', $parameters)){
$doc_id = $parameters['doc_id'];
if (empty($doc_id))
return new JsonModel([
'status' => 'EMPTY DOC_ID'
]);
$search_query = $parameters['search_query'];
if (empty($search_query))
return new JsonModel([
'status' => 'EMPTY QUERY'
]);
$verbose = isset($parameters['verbose']) && $parameters['verbose'] == '1' ? true : false;
$synonyms = isset($parameters['synonyms']) && preg_match('/lang|all/', $parameters['synonyms']) ? $parameters['synonyms'] : "";
$types_filter = isset($parameters['fulltext_types']) ? $parameters['fulltext_types'] :
implode(',', array_keys(self::description_to_text_type_map)); // Iterate over all possible types
$snippets[$doc_id] = $this->constructFulltextSnippet($doc_id, $search_query, $verbose, $synonyms, $types_filter);
}
// the new api
if(array_key_exists('docs', $parameters)){
$docs_get = $parameters['docs'];

$docs = json_decode(html_entity_decode($docs_get));
foreach($docs as $doc){
// $snippets[$item->id] = $item->id;
if(!empty($doc->id)){
$snippets[$doc->id] = [];
$types_filter = (!empty($doc->fulltext_type_filter) ? $doc->fulltext_type_filter : (!empty($doc->fulltext_types) ? $doc->fulltext_types : implode(',', array_keys(self::description_to_text_type_map))));

$synonyms = preg_match('/lang|all/',$doc->synonym_type) ? $doc->synonym_type : "";
$verbose = $doc->verbose;
if(empty($doc->query)){
$snippets[$doc->id]['status'] = 'EMPTY QUERY';
}else{
$snippets[$doc->id] = $this->constructFulltextSnippet($doc->id, $doc->query, $verbose, $synonyms, $types_filter);
}

}
}
}
try {
$model = new JsonModel([
'status' => 'SUCCESS',
'snippets' => $snippets['snippets']
'status' => 'SUCCESS',
'snippets' => $snippets
]);
}
catch (\Exception $e) {
Expand Down
Binary file modified solr/vufind/jars/browse-handler.jar
Binary file not shown.
119 changes: 80 additions & 39 deletions themes/tuefind/js/tuefind.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,52 +97,95 @@ var TueFind = {
},

GetFulltextSnippets: function(url, doc_id, query, verbose = false, synonyms = "", fulltext_types = "") {
let url_api = "";
let snippets_data = [];
if(doc_id === undefined){
$('.snippet_place_holder').each(function () {
snippets_data.push({
id: $(this).data('id'),
home_url: $(this).data('home-url'),
query: $(this).data('query'),
synonym_type: $(this).data('synonym-type'),
verbose: $(this).data('verbose'),
fulltext_type_filters: $(this).data('fulltext-type-filters'),
fulltext_types: $(this).data('fulltext-types')
});
});
url_api = "/fulltextsnippetproxy/load?docs=" + JSON.stringify(snippets_data);

} else{
url_api = url + "fulltextsnippetproxy/load?search_query=" + query + "&doc_id=" + JSON.stringify(doc_id) + (verbose ? "&verbose=1" : "")
+ (synonyms ? "&synonyms=" + synonyms : "")
+ (fulltext_types ? "&fulltext_types=" + fulltext_types : "")

snippets_data.push({
id: doc_id,
home_url: url,
query: query,
synonym_type: $(this).data('synonym-type'),
verbose: verbose,
// fulltext_type_filters: $(this).data('fulltext-type-filters'),
fulltext_types: fulltext_types
});
}

var valid_synonym_terms = new RegExp('lang|all');
synonyms = synonyms.match(valid_synonym_terms) ? synonyms : false;
$.ajax({
type: "GET",
url: url + "fulltextsnippetproxy/load?search_query=" + query + "&doc_id=" + doc_id + (verbose ? "&verbose=1" : "")
+ (synonyms ? "&synonyms=" + synonyms : "")
+ (fulltext_types ? "&fulltext_types=" + fulltext_types : ""),
url: url_api,
dataType: "json",
success: function (json) {
$(document).ready(function () {
if (json.status === 'PROXY_ERROR') {
if (verbose) {
$("#snippets_" + doc_id).replaceWith(TueFind.GetProxyErrorMessage(doc_id));
snippets_data.forEach(element => {
let snippets = json['snippets'][element['id']]['snippets'];
const status = json['snippets'][element['id']]['status'];
const doc_id = element['id'];
const verbose = element['verbose'];
const fulltext_types = (element['fulltext_type_filters'] !== "") ? element['fulltext_type_filters'] : element['fulltext_types'];
const query = element['query'];
let valid_synonym_terms = new RegExp('lang|all');
const synonyms = element['synonym_type'].match(valid_synonym_terms) ? element['synonym_type'] : false;

if (status === 'PROXY_ERROR') {
if (verbose) {
$("#snippets_" + doc_id).replaceWith(TueFind.GetProxyErrorMessage(doc_id));
}
$("#snippet_place_holder_" + doc_id).each(function () {
$(this).replaceWith(TueFind.GetProxyErrorMessage(doc_id));
});
return;
}

$("#snippet_place_holder_" + doc_id).each(function () {
$(this).replaceWith(TueFind.GetProxyErrorMessage(doc_id));
if (snippets)
$(this).replaceWith('<div id="snippets_' + doc_id + '" class="snippet-div">' + snippets.join('<br/>') + '<br/></div>');
else if (verbose)
$(this).replaceWith(TueFind.GetNoMatchesMessage(doc_id));
else
$(this).replaceWith();
});
return;
}
var snippets = json['snippets'];
$("#snippet_place_holder_" + doc_id).each(function () {
if (snippets)
$(this).replaceWith('<div id="snippets_' + doc_id + '" class="snippet-div">' + snippets.join('<br/>') + '<br/></div>');
else if (verbose)
$(this).replaceWith(TueFind.GetNoMatchesMessage(doc_id));
else
$(this).replaceWith();
});
if (snippets)
$(this).removeAttr('style');
$("#snippets_" + doc_id).each(function () {
if (snippets) {
let styles = snippets.map(a => (a.hasOwnProperty('style') ? a.style : null )).filter(Boolean).join();
$(styles).appendTo("head");
let snippets_and_pages = snippets.map(a => a.snippet +
(a.hasOwnProperty('page') ? '<br/>' + TueFind.FormatPageInformation(a.page) : '') +
TueFind.FormatTextType(a.text_type, verbose, fulltext_types));
$(this).html(snippets_and_pages.join('<hr class="snippet-separator"/>'));
} else if (verbose)
$(this).replaceWith(TueFind.GetNoMatchesMessage(doc_id));
else
$(this).html("");
$(this).removeAttr('style');
$("#snippets_" + doc_id).each(function () {
if (snippets) {
let styles = snippets.map(a => (a.hasOwnProperty('style') ? a.style : null)).filter(Boolean).join();
$(styles).appendTo("head");
let snippets_and_pages = snippets.map(a => a.snippet +
(a.hasOwnProperty('page') ? '<br/>' + TueFind.FormatPageInformation(a.page) : '') +
TueFind.FormatTextType(a.text_type, verbose, fulltext_types));
$(this).html(snippets_and_pages.join('<hr class="snippet-separator"/>'));
} else if (verbose)
$(this).replaceWith(TueFind.GetNoMatchesMessage(doc_id));
else
$(this).html("");
});
$("[id^=snippets_] > p").each(function () { this.style.transform = "none"; });
if (!verbose && snippets)
$("#snippets_" + doc_id).after(TueFind.ItemFulltextLink(doc_id, query, synonyms));


});
$("[id^=snippets_] > p").each(function () { this.style.transform="none"; });
if (!verbose && snippets)
$("#snippets_" + doc_id).after(TueFind.ItemFulltextLink(doc_id, query, synonyms));
});
}, // end success
error: function (xhr, ajaxOptions, thrownError) {
Expand Down Expand Up @@ -643,8 +686,6 @@ $(document).ready(function () {
$("#searchForm").submit();
});

new DataTable('.dataTable',{
scrollX: true
});

});
$('.dataTable').DataTable();
TueFind.GetFulltextSnippets();
});
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?php $config = $this->config()->get('fulltextsnippet')?>
<?php if (isset($config->Elasticsearch->base_url) && $this->driver->tryMethod('hasFulltextMatch')): ?>
<span id="fulltext_snippets">
<div style="color:grey" id="snippet_place_holder_<?=$this->driver->getUniqueID()?>"><?=$this->transEsc("Loading")?>...</div>
<?php
$query = $this->params->getQuery();
$query_terms = $query->getAllTerms();
$handler = $query instanceof \VuFindSearch\Query\Query ? $query->getHandler() :
($query instanceof \VuFindSearch\Query\QueryGroup ? $query->getReducedHandler() : "");
$synonym_type = "";
switch ($handler) {
case "FulltextWithSynonyms":
$synonym_type = "lang";
break;
case "FulltextAllSynonyms":
$synonym_type = "all";
break;
}
$fulltext_type_filters = $this->driver->getFulltextTypeFilters();
$fulltext_types = $this->driver->getFulltextTypes();
<?php
$query = $this->params->getQuery();
$query_terms = $query->getAllTerms();
$handler = $query instanceof \VuFindSearch\Query\Query ? $query->getHandler() :
($query instanceof \VuFindSearch\Query\QueryGroup ? $query->getReducedHandler() : "");
$synonym_type = "";
switch ($handler) {
case "FulltextWithSynonyms":
$synonym_type = "lang";
break;
case "FulltextAllSynonyms":
$synonym_type = "all";
break;
}
$fulltext_type_filters = $this->driver->getFulltextTypeFilters();
$fulltext_types = $this->driver->getFulltextTypes();

?>

$jsSnippet = "TueFind.GetFulltextSnippets('" . $this->url('home') . "','" . $this->driver->getUniqueID() . "','" . addslashes($query_terms) . "', false, '" . $synonym_type . "', ";
if (isset($fulltext_type_filters) && $fulltext_type_filters != '') {
$jsSnippet .= "'" . implode("','", $fulltext_type_filters) . "'";
} else {
$jsSnippet .= "'" . implode("','", $fulltext_types) . "'";
}
$jsSnippet .= ');';
?>
<?=$this->inlineScript(\Laminas\View\Helper\HeadScript::SCRIPT, $jsSnippet, 'SET')?>
<span id="fulltext_snippets">
<div style="color:grey" id="snippet_place_holder_<?=$this->driver->getUniqueID()?>" class="snippet_place_holder"
data-id="<?=$this->driver->getUniqueID()?>" data-home-url="<?=$this->url('home')?>"
data-query="<?=$this->escapeHtmlAttr($query_terms)?>" data-synonym-type="<?=$synonym_type?>" data-verbose="false"
data-fulltext-type-filters="<?=implode(',', $fulltext_type_filters)?>" data-fulltext-types="<?=implode(',', $fulltext_types)?>">
<?=$this->transEsc("Loading")?>...</div>
</span>
<?php endif; ?>