-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchApp.py
73 lines (53 loc) · 1.95 KB
/
searchApp.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import streamlit as st
from elasticsearch import Elasticsearch
from sentence_transformers import SentenceTransformer
indexName = "all_products"
try:
es = Elasticsearch(
"https://192.168.18.136:9200",
basic_auth=("elastic","FIRpdx+ua5jlE0K6Q3fs"),
ca_certs="http_ca.crt"
)
except ConnectionError as e:
print("Connection Error:", e)
if es.ping():
print("Succesfully connected to ElasticSearch!!")
else:
print("Oops!! Can not connect to Elasticsearch!")
def search(input_keyword):
model = SentenceTransformer('all-mpnet-base-v2')
vector_of_input_keyword = model.encode(input_keyword)
query = {
"field" : "DescriptionVector",
"query_vector" : vector_of_input_keyword,
'k':50,
"num_candidates" : 500,
}
res = es.knn_search(index="all_products", knn=query , source=["ProductName","Description"])
result=res["hits"]["hits"]
return result
def main():
st.title("Search Products with Description")
# Input: User enters search query
search_query = st.text_input("Enter your search query")
# Button: User triggers the search
if st.button("Search"):
if search_query:
# Perform the search and get results
results = search(search_query)
# Display search results
st.subheader("Search Results")
for result in results:
with st.container():
if '_source' in result:
try:
st.header(f"{result['_source']['ProductName']}")
except Exception as e:
print(e)
try:
st.write(f"Description: {result['_source']['Description']}")
except Exception as e:
print(e)
st.divider()
if __name__ == "__main__":
main()