Skip to content

Commit 64541a8

Browse files
committed
fixing code
1 parent 082161d commit 64541a8

File tree

6 files changed

+47
-78
lines changed

6 files changed

+47
-78
lines changed

supporting-blog-content/building-a-recipe-search-with-elasticsearch/elasticsearch_connection.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@
55
class ElasticsearchConnection:
66

77
def __init__(self, config_file="config.yml"):
8-
with open(config_file, 'r') as f:
8+
with open(config_file, "r") as f:
99
config = yaml.safe_load(f)
1010
self.client = Elasticsearch(
11-
cloud_id=config['cloud_id'],
12-
api_key=config['api_key']
11+
cloud_id=config["cloud_id"],
12+
api_key=config["api_key"]
1313
)
1414

1515
def get_client(self):
1616
return self.client
1717

1818
def get_async_client(self):
19-
with open("config.yml", 'r') as f:
19+
with open("config.yml", "r") as f:
2020
config = yaml.safe_load(f)
2121
self.client = AsyncElasticsearch(
22-
cloud_id=config['cloud_id'],
23-
api_key=config['api_key'],
22+
cloud_id=config["cloud_id"],
23+
api_key=config["api_key"],
2424
request_timeout=240)
2525
return self.client;

supporting-blog-content/building-a-recipe-search-with-elasticsearch/infra.py

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,13 @@ def create_index_embedding():
88
index="grocery-catalog-elser",
99
mappings={
1010
"properties": {
11-
"id": {
12-
"type": "integer"
13-
},
14-
"name": {
15-
"type": "text",
16-
},
17-
"description": {
18-
"type": "text",
19-
"copy_to": "description_embedding"
20-
},
21-
"category": {
22-
"type": "keyword"
23-
},
24-
"brand": {
25-
"type": "keyword"
26-
},
27-
"price": {
28-
"type": "float"
29-
},
30-
"unit": {
31-
"type": "keyword"
32-
},
11+
"id": {"type": "integer"},
12+
"name": {"type": "text"},
13+
"description": {"type": "text", "copy_to": "description_embedding"},
14+
"category": {"type": "keyword"},
15+
"brand": {"type": "keyword"},
16+
"price": {"type": "float"},
17+
"unit": {"type": "keyword"},
3318
"description_embedding": {
3419
"type": "semantic_text",
3520
"inference_id": "elser_embeddings"
@@ -43,18 +28,16 @@ def create_index_embedding():
4328
def create_inference():
4429
response = client.inference.put(
4530
inference_id="elser_embeddings",
46-
task_type="sparse_embedding", body={
31+
task_type="sparse_embedding",
32+
body={
4733
"service": "elser",
48-
"service_settings": {
49-
"num_allocations": 1,
50-
"num_threads": 1
51-
}
34+
"service_settings": {"num_allocations": 1, "num_threads": 1}
5235
})
5336
print(response)
5437

5538

56-
if __name__ == '__main__':
39+
if __name__ == "__main__":
5740

5841
create_inference()
5942

60-
create_index_embedding()
43+
create_index_embedding()

supporting-blog-content/building-a-recipe-search-with-elasticsearch/infra_lexical_index.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,18 @@ def create_index():
88
index="grocery-catalog",
99
mappings={
1010
"properties": {
11-
"id": {
12-
"type": "integer"
13-
},
14-
"name": {
15-
"type": "text",
16-
},
17-
"description": {
18-
"type": "text"
19-
},
20-
"category": {
21-
"type": "keyword"
22-
},
23-
"brand": {
24-
"type": "keyword"
25-
},
26-
"price": {
27-
"type": "float"
28-
},
29-
"unit": {
30-
"type": "keyword"
31-
}
11+
"id": {"type": "integer"},
12+
"name": {"type": "text"},
13+
"description": {"type": "text", "copy_to": "description_embedding"},
14+
"category": {"type": "keyword"},
15+
"brand": {"type": "keyword"},
16+
"price": {"type": "float"},
17+
"unit": {"type": "keyword"}
3218
}
3319
}
3420
)
3521
print(response)
3622

3723

38-
if __name__ == '__main__':
24+
if __name__ == "__main__":
3925
create_index()

supporting-blog-content/building-a-recipe-search-with-elasticsearch/ingestion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def partition_list(lst, chunk_size):
1414

1515
async def index_data():
1616
global partitions
17-
with open('files/output.json', 'r') as file:
17+
with open("files/output.json", "r") as file:
1818
data_json = json.load(file)
1919
documents = []
2020
for doc in data_json:

supporting-blog-content/building-a-recipe-search-with-elasticsearch/ingestion_lexical_index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def partition_list(lst, chunk_size):
1414

1515
async def index_data():
1616
global partitions
17-
with open('files/output.json', 'r') as file:
17+
with open("files/output.json", "r") as file:
1818
data_json = json.load(file)
1919
documents = []
2020
for doc in data_json:

supporting-blog-content/building-a-recipe-search-with-elasticsearch/search.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def format_text(description, line_length=120):
1313
if len(words) <= line_length:
1414
return description
1515
else:
16-
return ' '.join(words[:line_length]) + '...'
16+
return " ".join(words[:line_length]) + "..."
1717

1818

1919
def search_semantic(term):
@@ -36,9 +36,9 @@ def search_semantic(term):
3636
description = hit["_source"]["description"]
3737
formatted_description = format_text(description)
3838
result.append({
39-
'score': score,
40-
'name': name,
41-
'description': formatted_description,
39+
"score": score,
40+
"name": name,
41+
"description": formatted_description,
4242
})
4343
return result
4444

@@ -64,33 +64,33 @@ def search_lexical(term):
6464
name = format_text(hit["_source"]["name"], line_length=10)
6565
description = hit["_source"]["description"]
6666
result.append({
67-
'score': score,
68-
'name': name,
69-
'description': description,
67+
"score": score,
68+
"name": name,
69+
"description": description,
7070
})
7171
return result
7272

7373

74-
if __name__ == '__main__':
74+
if __name__ == "__main__":
7575
rs1 = search_semantic(term)
7676
rs2 = search_lexical(term)
7777

78-
df1 = pd.DataFrame(rs1)[['name', 'score']] if rs1 else pd.DataFrame(columns=['name', 'score'])
79-
df2 = pd.DataFrame(rs2)[['name', 'score']] if rs2 else pd.DataFrame(columns=['name', 'score'])
80-
df1 = pd.DataFrame(rs1)[['name', 'score']] if rs1 else pd.DataFrame(columns=['name', 'score'])
81-
df1['Search Type'] = 'Semantic'
78+
df1 = pd.DataFrame(rs1)[["name", "score"]] if rs1 else pd.DataFrame(columns=["name", "score"])
79+
df2 = pd.DataFrame(rs2)[["name", "score"]] if rs2 else pd.DataFrame(columns=["name", "score"])
80+
df1 = pd.DataFrame(rs1)[["name", "score"]] if rs1 else pd.DataFrame(columns=["name", "score"])
81+
df1["Search Type"] = "Semantic"
8282

83-
df2 = pd.DataFrame(rs2)[['name', 'score']] if rs2 else pd.DataFrame(columns(['name', 'score']))
84-
df2['Search Type'] = 'Lexical'
83+
df2 = pd.DataFrame(rs2)[["name", "score"]] if rs2 else pd.DataFrame(columns(["name", "score"]))
84+
df2["Search Type"] = "Lexical"
8585

8686
tabela = pd.concat([df1, df2], axis=0).reset_index(drop=True)
8787

88-
tabela = tabela[['Search Type', 'name', 'score']]
88+
tabela = tabela[["Search Type", "name", "score"]]
8989

90-
tabela.columns = ['Search Type', 'Name', 'Score']
90+
tabela.columns = ["Search Type", "Name", "Score"]
9191

92-
tabela['Search Type'] = tabela['Search Type'].astype(str).str.ljust(0)
93-
tabela['Name'] = tabela['Name'].astype(str).str.ljust(15)
94-
tabela['Score'] = tabela['Score'].astype(str).str.ljust(5)
92+
tabela["Search Type"] = tabela["Search Type"].astype(str).str.ljust(0)
93+
tabela["Name"] = tabela["Name"].astype(str).str.ljust(15)
94+
tabela["Score"] = tabela["Score"].astype(str).str.ljust(5)
9595

9696
print(tabela.to_string(index=False))

0 commit comments

Comments
 (0)