This commit is contained in:
parent
71000aad35
commit
5aa8258f16
|
@ -290,6 +290,12 @@ type Result {
|
||||||
communities: [Community]
|
communities: [Community]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SearchResult {
|
||||||
|
slug: String!
|
||||||
|
title: String!
|
||||||
|
score: Float!
|
||||||
|
}
|
||||||
|
|
||||||
# Мутации
|
# Мутации
|
||||||
|
|
||||||
type Mutation {
|
type Mutation {
|
||||||
|
@ -354,7 +360,7 @@ type Query {
|
||||||
get_shout(slug: String, shout_id: Int): Shout
|
get_shout(slug: String, shout_id: Int): Shout
|
||||||
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout] # userReactedShouts
|
load_shouts_followed(follower_id: Int!, limit: Int, offset: Int): [Shout] # userReactedShouts
|
||||||
load_shouts_by(options: LoadShoutsOptions): [Shout]
|
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_feed(options: LoadShoutsOptions): [Shout]
|
||||||
load_shouts_unrated(limit: Int, offset: Int): [Shout]
|
load_shouts_unrated(limit: Int, offset: Int): [Shout]
|
||||||
load_shouts_random_top(options: LoadShoutsOptions): [Shout]
|
load_shouts_random_top(options: LoadShoutsOptions): [Shout]
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
import aiohttp
|
import aiohttp
|
||||||
|
|
||||||
from orm.shout import Shout
|
from orm.shout import Shout # Adjust the import as needed
|
||||||
from services.rediscache import redis
|
from services.rediscache import redis # Adjust the import as needed
|
||||||
|
|
||||||
|
|
||||||
class SearchService:
|
class SearchService:
|
||||||
lock = asyncio.Lock()
|
lock = asyncio.Lock()
|
||||||
cache = {}
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def init(session):
|
async def init(session):
|
||||||
async with SearchService.lock:
|
async with SearchService.lock:
|
||||||
print("[services.search] did nothing")
|
logging.info("[services.search] Initializing SearchService")
|
||||||
SearchService.cache = {}
|
SearchService.cache = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -26,12 +26,15 @@ class SearchService:
|
||||||
# Use aiohttp to send a request to ElasticSearch
|
# Use aiohttp to send a request to ElasticSearch
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
search_url = f"https://search.discours.io/search?q={text}"
|
search_url = f"https://search.discours.io/search?q={text}"
|
||||||
|
try:
|
||||||
async with session.get(search_url) as response:
|
async with session.get(search_url) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
payload = await response.json()
|
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]
|
return payload[offset : offset + limit]
|
||||||
else:
|
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:
|
else:
|
||||||
return json.loads(cached)[offset : offset + limit]
|
return json.loads(cached)[offset : offset + limit]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user