A search interface for the Performing Patents Otherwise publication as part of the Politics of Patents case study (part of Copim WP6): this parses data from the archive of RTF files and provides additional data from the European Patent Office OPS API. https://patents.copim.ac.uk
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
6.4KB

  1. # @name: solr.py
  2. # @creation_date: 2022-09-07
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <simon.bowie.19@gmail.com>
  5. # @purpose: Performs Solr functions
  6. # @acknowledgements:
  7. import os
  8. import requests
  9. import re
  10. import urllib
  11. import random
  12. from . import ops
  13. # get config variables from OS environment variables: set in env file passed through Docker Compose
  14. solr_hostname = os.environ.get('SOLR_HOSTNAME')
  15. solr_port = os.environ.get('SOLR_PORT')
  16. def solr_search(core, sort, search=None, id=None):
  17. # 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
  18. if id is not None:
  19. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=id%3A"' + id + '"&wt=json'
  20. else:
  21. if (sort == 'relevance'):
  22. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=content%3A' + urllib.parse.quote_plus(search) + '&wt=json'
  23. else:
  24. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=content%3A' + urllib.parse.quote_plus(search) + '&wt=json&sort=' + sort
  25. # get result
  26. request = requests.get(solrurl)
  27. # turn the API response into useful Json
  28. json = request.json()
  29. num_found = json['response']['numFound']
  30. if (num_found == 0):
  31. output = 'no results found'
  32. else:
  33. output = []
  34. for result in json['response']['docs']:
  35. # set ID variable
  36. id = result['id']
  37. # set content variable
  38. content = result['content']
  39. # parse result
  40. result_output = parse_result(id, content)
  41. output.append(result_output)
  42. return output, num_found
  43. def parse_result(id, input):
  44. output = {}
  45. output['id'] = id
  46. # set document reference number (used for OPS API)
  47. doc_ref = re.search('=D\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  48. if doc_ref is None:
  49. doc_ref = re.search('=D&locale=en_EP\s(([^\s]*)\s([^\s]*)\s([^\s]*))', input)
  50. if doc_ref is None:
  51. output['doc_ref'] = ""
  52. else:
  53. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  54. else:
  55. output['doc_ref'] = doc_ref.group(1).replace(" ","")
  56. # search for the application ID in the content element and display it
  57. application_id = re.search('Application.*\n(.*)\n', input)
  58. output['application_id'] = application_id.group(1)
  59. # search for the EPO publication URL in the content element and display it
  60. epo_publication = re.search('Publication.*\n(.*)\n', input)
  61. output['epo_publication_url'] = epo_publication.group(1)
  62. # search for the IPC publication URL in the content element and display it
  63. ipc_publication = re.search('IPC.*\n(.*)\n', input)
  64. output['ipc_publication_url'] = ipc_publication.group(1)
  65. # search for the title in the content element and display it
  66. title = re.search('Title.*\n(.*)\n', input)
  67. if title is not None:
  68. output['title'] = title.group(1)
  69. # search for the abstract in the content element and display it
  70. abstract = re.search('Abstract.*\n(.*)\n', input)
  71. if abstract is None:
  72. abstract = re.search('\(.\) \\n\\n(.*)\\n', input)
  73. if abstract is not None:
  74. output['abstract'] = abstract.group(1);
  75. # search for the year in the content element and display it
  76. year = re.search('=D[^\s]*\s[^\s]*\s[^\s]*\s[^\s]*\s(\d{4})', input)
  77. if year is not None:
  78. output['year'] = year.group(1)
  79. return output
  80. def get_random_record(core):
  81. rand = str(random.randint(0, 9999999))
  82. # 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
  83. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*%3A*&wt=json&sort=random_' + rand + '%20asc&rows=1'
  84. # get result
  85. request = requests.get(solrurl)
  86. # turn the API response into useful Json
  87. json = request.json()
  88. if (json['response']['numFound'] == 0):
  89. output = 'no results found'
  90. else:
  91. output = []
  92. for result in json['response']['docs']:
  93. # set ID variables
  94. id = result['id']
  95. # set content variable
  96. content = result['content']
  97. # parse result
  98. result_output = parse_result(id, content)
  99. output.append(result_output)
  100. return output
  101. def get_ten_random_elements(field):
  102. core = 'all'
  103. output = []
  104. i = 0
  105. while i <= 9:
  106. results = get_random_record(core)
  107. for result in results:
  108. if field in result:
  109. dict = {'id': result['id'], field: result[field]}
  110. output.append(dict)
  111. i += 1
  112. return output
  113. def get_ten_random_images():
  114. core = 'all'
  115. output = []
  116. i = 0
  117. while i <= 9:
  118. results = get_random_record(core)
  119. for result in results:
  120. if ops.get_images(result['doc_ref']):
  121. image = ops.get_images(result['doc_ref'])
  122. result.update(image)
  123. output.append(result)
  124. i += 1
  125. return output
  126. def get_total_number(core):
  127. # 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
  128. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/select?q.op=OR&q=*:*&wt=json'
  129. # get result
  130. request = requests.get(solrurl)
  131. # turn the API response into useful Json
  132. json = request.json()
  133. num_found = json['response']['numFound']
  134. return num_found
  135. def get_term_data(field, core):
  136. # 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
  137. solrurl = 'http://' + solr_hostname + ':' + solr_port + '/solr/' + core + '/terms?terms.fl=' + field + '&wt=json&terms.limit=1000'
  138. # get result
  139. request = requests.get(solrurl)
  140. # turn the API response into useful Json
  141. json = request.json()
  142. output = json['terms'][field]
  143. return output