-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery_ApacheSolr.py
45 lines (30 loc) · 1.47 KB
/
query_ApacheSolr.py
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
import urllib3
import json
http = urllib3.PoolManager()
def search(search_string):
search_terms = search_string.strip().split(" ")
search_terms = ['snippet:"{}"'.format(i) for i in search_terms]
search_query = ' AND '.join(search_terms)
print('search_query ', search_query)
#connection = http.request('GET', 'http://localhost:8983/solr/AIR_Project/select?q=snippet:"cathedral" AND snippet:"climate"&wt=json')
connection = http.request('GET', 'http://localhost:8983/solr/AIR_Project/select?q={}&wt=json'.format(search_query))
response = json.loads(connection.data)
#print('Response ', response)
print(response['response']['numFound'], "documents found.")
# Print the name of each document.
return_list = []
for document in response['response']['docs']:
temp_dict = dict()
temp_dict['filename'] = str(document['filename'][0])
temp_dict['url'] = str(document['url'][0])
temp_dict['datetime'] = str(document['datetime'][0])
temp_dict['station'] = str(document['station'][0])
temp_dict['show'] = str(document['show'][0])
temp_dict['showid'] = str(document['showid'][0])
temp_dict['preview'] = str(document['preview'][0])
temp_dict['snippet'] = str(document['snippet'][0])
return_list.append(temp_dict)
# print(return_list)
return return_list
# Sample usage. ApacheSolr sorted all results for query 'cathedral climate'.
print(search('cathedral climate'))