# Copyright (C) 2026 Mattia Zatelli # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """Query result model""" from sqlalchemy import Column, Integer, String, Float, JSON, DateTime, ForeignKey, Index, text from sqlalchemy.orm import relationship from app.core.database import Base class QueryResult(Base): """Individual query result linking queries to elements""" __tablename__ = "query_results" # Primary key id = Column(Integer, primary_key=True, index=True) # Foreign keys query_id = Column(Integer, ForeignKey("queries.id", ondelete="CASCADE"), nullable=False) element_id = Column(Integer, ForeignKey("elements.id", ondelete="CASCADE"), nullable=False) # Result metadata relevance_score = Column(Float, nullable=True, doc="How relevant this element is to the query (0-1)") match_reason = Column(String(100), nullable=True, doc="Why this element matched (semantic, keyword, etc.)") result_data = Column(JSON, nullable=True, doc="Additional result-specific data") # Timestamps created_at = Column(DateTime, nullable=False, server_default=text("CURRENT_TIMESTAMP")) # Relationships query = relationship("Query", back_populates="results", lazy="select") element = relationship("Element", back_populates="query_results", lazy="select") # Indexes __table_args__ = ( Index("idx_query_results_query_id", "query_id"), Index("idx_query_results_element_id", "element_id"), Index("idx_query_results_relevance_score", "relevance_score"), Index("idx_query_results_unique", "query_id", "element_id", unique=True), ) def __repr__(self): return f""