-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollection.py
More file actions
139 lines (95 loc) · 3.8 KB
/
Copy pathcollection.py
File metadata and controls
139 lines (95 loc) · 3.8 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import requests
import pysolr
import pandas as pd
solr_url = "http://localhost:8983/solr/admin/collections"
def getDepFact(collection_name):
emp=pysolr.Solr(f"http://localhost:8983/solr/{collection_name}")
# Replace with your Solr server URL
# Create a Solr client object
# Define the facet query parameters
params = {
"facet": "true",
"facet.field": "Department"
}
# Perform the facet query
# Extract the facet results
results = emp.search("*:*", params=params)
# Check if facet results are available
if 'facet_fields' in results.facets:
# Extract the facet results
facet_results = results.facets['facet_fields']['department']
# Print the facet results
for facet_value, count in facet_results:
print(f"Department: {facet_value}, Count: {count}")
else:
# Handle the case where facet results are not available
print("Facet results not found.")
def delEmpById(collection_name,Emp_id):
print("deleting please wait! \n")
delemp=pysolr.Solr(f"http://localhost:8983/solr/{collection_name}/",always_commit=True)
delete_query=f"Employee_ID:{Emp_id}"
delemp.delete(delete_query)
print("successfully Deleted \n")
def getEmpCount(collection_name):
emp=pysolr.Solr(f"http://localhost:8983/solr/{collection_name}", always_commit=True)
res=emp.search("*:*",row=0)
print("Employe Count: ",res.hits)
def searchByColumn(collection_name,Column_name,Column_value):
print("Searching please wait! \n")
try:
mysearch=pysolr.Solr(f"http://localhost:8983/solr/{collection_name}", always_commit=True)
filter_queries={
Column_name:Column_value
}
query=[key+":"+val for key,val in filter_queries.items()]
results=mysearch.search('*:*',fq=query)
for result in results:
print(result)
except pysolr.SolrError as e:
print(f"{Column_name} Field not found! \n")
def indexData(collection_name,Exclude_column):
index=pysolr.Solr(f"http://localhost:8983/solr/{collection_name}", always_commit=True)
mydata=pd.read_csv('Employee Data.csv', encoding='windows-1252')
del mydata[Exclude_column]
del mydata['Exit Date']
documents=mydata.to_dict(orient='records')
print("Indexing the document please wait! \n")
index.add(documents)
print("Process Successfully Completed \n")
def create_collection(collection_name):
print(f"Creating collection: {collection_name} \n")
try:
params = {
'action': 'CREATE',
'name': collection_name,
'numShards': 1,
'replicationFactor': 1,
'config': '_default',
'maxShardsPerNode': 1
}
response = requests.post(solr_url, params=params)
if response.status_code == 200:
print("Collection created successfully!")
except response.error:
print("collection already exists!")
#creating collection
v_nameCollection = input("Enter V_nameCollection name: ")
v_phoneCollection = input("Enter V_phonecollection name: ")
create_collection(v_nameCollection)
create_collection(v_phoneCollection)
#Get Employee count
getEmpCount(v_nameCollection)
#Indexing data
indexData(v_nameCollection,'Department')
indexData(v_phoneCollection,'Gender')
#delete employe by id
delEmpById(v_nameCollection,'E02003')
#again get employee count
getEmpCount(v_nameCollection)
#search by column name
searchByColumn(v_nameCollection,'Department','IT')
searchByColumn(v_nameCollection,'Gender','Male')
searchByColumn(v_phoneCollection,'Department','IT')
#getDepFact
#getDepFact(v_nameCollection)
#getDepFact(v_phoneCollection)