search-result-schema-fix
All checks were successful
deploy / deploy (push) Successful in 1m21s

This commit is contained in:
Untone 2023-12-19 15:18:58 +03:00
parent 71000aad35
commit 5aa8258f16
2 changed files with 21 additions and 12 deletions

View File

@ -290,6 +290,12 @@ type Result {
communities: [Community]
}
type SearchResult {
slug: String!
title: String!
score: Float!
}
# Мутации
type Mutation {
@ -354,7 +360,7 @@ type Query {
get_shout(slug: String, shout_id: Int): Shout
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout] # userReactedShouts
load_shouts_by(options: LoadShoutsOptions): [Shout]
load_shouts_search(text: String!, limit: Int, offset: Int): [Shout]
load_shouts_search(text: String!, limit: Int, offset: Int): [SearchResult]
load_shouts_feed(options: LoadShoutsOptions): [Shout]
load_shouts_unrated(limit: Int, offset: Int): [Shout]
load_shouts_random_top(options: LoadShoutsOptions): [Shout]

View File

@ -1,21 +1,21 @@
import asyncio
import json
import logging
from typing import List
import aiohttp
from orm.shout import Shout
from services.rediscache import redis
from orm.shout import Shout # Adjust the import as needed
from services.rediscache import redis # Adjust the import as needed
class SearchService:
lock = asyncio.Lock()
cache = {}
@staticmethod
async def init(session):
async with SearchService.lock:
print("[services.search] did nothing")
logging.info("[services.search] Initializing SearchService")
SearchService.cache = {}
@staticmethod
@ -26,12 +26,15 @@ class SearchService:
# Use aiohttp to send a request to ElasticSearch
async with aiohttp.ClientSession() as session:
search_url = f"https://search.discours.io/search?q={text}"
try:
async with session.get(search_url) as response:
if response.status == 200:
payload = await response.json()
await redis.execute("SET", text, json.dumps(payload))
await redis.execute("SET", text, json.dumps(payload)) # use redis as cache
return payload[offset : offset + limit]
else:
print(f"[services.search] response: {response.status} {response.text}")
logging.error(f"[services.search] response: {response.status} {await response.text()}")
except Exception as e:
logging.error(f"[services.search] error: {e}")
else:
return json.loads(cached)[offset : offset + limit]