Browse Source

New function to search on Solr record ID

main
Simon Bowie 3 years ago
parent
commit
8003b26307
3 changed files with 138 additions and 9 deletions
  1. +75
    -0
      site/public/id_search.php
  2. +10
    -2
      site/public/titles.php
  3. +53
    -7
      site/solr.php

+ 75
- 0
site/public/id_search.php View File

<?php

# @name: id_search.php
# @version: 0.1
# @creation_date: 2021-09-30
# @license: The MIT License <https://opensource.org/licenses/MIT>
# @author: Simon Bowie <ad7588@coventry.ac.uk>
# @purpose: Searches the Solr index by ID for one particular record

include '../solr.php';

?>

<?php

$search_results = solr_search_id($_GET["id"]);

foreach($search_results as $result):

?>
Application ID:

<?php
echo $result['application_id'];
?>

<br><br>

EPO publication:

<a href=<?php echo $result['epo_publication_url']; ?>>
<?php
echo $result['epo_publication_url'];
?>
</a>

<br><br>

IPC publication:

<a href=<?php echo $result['ipc_publication_url']; ?>>
<?php
echo $result['ipc_publication_url'];
?>
</a>

<br><br>

Title:

<?php
echo $result['title'];
?>

<br><br>

<?php
if ($result['abstract']):
?>

Abstract:

<?php
echo $result['abstract'];
?>

<br><br>

<?
endif;
?>

<?php
endforeach;
?>

+ 10
- 2
site/public/titles.php View File



<?php <?php


foreach ($random_titles as $title):
foreach ($random_titles as $record_array):


?> ?>


<? <?
echo $title;
foreach ($record_array as $id => $title):
?>
<a href="id_search.php?id=<? echo $id; ?>">
<?
echo $title;
?>
</a>
<?
endforeach;
?> ?>


<br><br> <br><br>

+ 53
- 7
site/solr.php View File

function solr_search($search){ function solr_search($search){


// Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html // Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
// This query retrieves only the bib identifier field for records which satisfy the search query
$solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $_ENV["SOLR_CORE"] . '/select?q.op=OR&q=content%3A' . $search . '&wt=json'; $solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $_ENV["SOLR_CORE"] . '/select?q.op=OR&q=content%3A' . $search . '&wt=json';


// Perform Curl request on the Solr API // Perform Curl request on the Solr API
return $results; return $results;
} }


function solr_search_id($id){

// URL encode the ID string
$id = urlencode($id);

// Assemble a query string to send to Solr. This uses the Solr hostname from config.env. Solr's query syntax can be found at many sites including https://lucene.apache.org/solr/guide/6_6/the-standard-query-parser.html
$solrurl = 'http://' . $_ENV["SOLR_HOSTNAME"] . ':' . $_ENV["SOLR_PORT"] . '/solr/' . $_ENV["SOLR_CORE"] . '/select?q.op=OR&q=id%3A"' . $id . '"&wt=json';

// Perform Curl request on the Solr API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $solrurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$response = curl_exec($ch);
curl_close($ch);

// Turn the API response into useful Json
$json = json_decode($response);

// If no results are found, display a message
if ($json->response->numFound == '0'){
$output = 'No results found';
}
else{
foreach ($json->response->docs as $result){
$content = $result->content;
$result_output = parse_result($content);
$results[] = $result_output;
}
}
return $results;
}

function parse_result($input){ function parse_result($input){


//Set document reference number (used for OPS API) //Set document reference number (used for OPS API)
// Pick a random key out of the docs array // Pick a random key out of the docs array
$random = array_rand($json->response->docs); $random = array_rand($json->response->docs);


//Set ID variable
$id = $json->response->docs[$random]->id;

//Set content variable //Set content variable
$content = $json->response->docs[$random]->content; $content = $json->response->docs[$random]->content;


return $content;
//Construct associative array with ID and content
$result_array = array($id=>$content);

return $result_array;
} }


function one_random_record (){ function one_random_record (){


$content = get_random_record();
$random = get_random_record();


$output = parse_result($content);
foreach ($random as $id => $content){
$output = parse_result($content);
}


return $output; return $output;




for ($x=0; $x <= 9; $x++) { for ($x=0; $x <= 9; $x++) {
$random = get_random_record(); $random = get_random_record();
// Search for the title in the content element and display it
preg_match('/Title.*\n(.*)\n/', $random, $title);
$output[$x] = $title[1];

foreach($random as $id => $content){
// Search for the title in the content element and display it
preg_match('/Title.*\n(.*)\n/', $content, $title);

$output[$x] = array($id=>$title[1]);

}
} }


return $output; return $output;

Loading…
Cancel
Save