-
Notifications
You must be signed in to change notification settings - Fork 0
/
solrqueries.py
56 lines (50 loc) · 1.6 KB
/
solrqueries.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
46
47
48
49
50
51
52
53
54
55
56
import requests
import json
from dotenv import dotenv_values
config = dotenv_values(".env")
solr_url = config["SOLR_CORE_API"]
def solr_index_documents(docs):
update_url = f"{solr_url}/update"
headers = {'Content-Type': 'application/json'}
response = requests.post(update_url, headers=headers, data=json.dumps(docs))
if response.status_code != 200:
print(f"Failed to index documents {[doc["id"] for doc in docs]}: {response.text}")
def solr_commit():
commit_url = f"{solr_url}/update?commit=true"
requests.get(commit_url)
def solr_search(query, rows=100, by_popularity=False):
search_url = f"{solr_url}/select"
sort_field="sitelinks_i desc" if by_popularity else "score desc, sitelinks_i desc"
params = {
"q": query,
"df": "label_txt_en",
"fl": "qid_s",
"fq": "{!collapse field=qid_s}",
"sort": sort_field,
"rows": rows
}
response = requests.get(search_url, params=params)
data = response.json()
qid_list = []
for doc in data["response"]["docs"]:
qid=doc["qid_s"]
qid_list.append(qid)
return qid_list
def solr_is_present(qid):
search_url = f"{solr_url}/select"
params = {
"q": "*:*",
"fq": f"qid_s:{qid}",
"fl": "qid_s",
"rows": 1
}
response = requests.get(search_url, params=params)
data = response.json()
return len(data["response"]["docs"]) > 0
if __name__ == "__main__":
query = "Mary"
data = solr_search(query)
print(data)
data = solr_search(query, by_popularity=True)
print(data)
print(solr_is_present("Q345"))