Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): use keyword search for economy datasets in hit testing (#44895)

Tianyu Caiac1502185c01
diff --git a/api/services/hit_testing_service.py b/api/services/hit_testing_service.py
index 71bbf621cae..628fec5534f 100644
--- a/api/services/hit_testing_service.py
+++ b/api/services/hit_testing_service.py
@@ -9,6 +9,7 @@ from sqlalchemy.orm import Session
from core.app.app_config.entities import ModelConfig
from core.rag.datasource.retrieval_service import DefaultRetrievalModelDict, RetrievalService
from core.rag.embedding.retrieval import RetrievalSegments
+from core.rag.index_processor.constant.index_type import IndexTechniqueType
from core.rag.index_processor.constant.query_type import QueryType
from core.rag.models.document import Document
from core.rag.retrieval.dataset_retrieval import DatasetRetrieval
@@ -145,9 +146,9 @@ class HitTestingService:
if metadata_condition and not document_ids_filter:
return cls.compact_retrieve_response(query, [], session=session)
all_documents = RetrievalService.retrieve(
- retrieval_method=RetrievalMethod(
- resolved_retrieval_model.get("search_method", RetrievalMethod.SEMANTIC_SEARCH)
- ),
+ retrieval_method=RetrievalMethod.KEYWORD_SEARCH
+ if dataset.indexing_technique == IndexTechniqueType.ECONOMY
+ else RetrievalMethod(resolved_retrieval_model.get("search_method", RetrievalMethod.SEMANTIC_SEARCH)),
dataset_id=dataset.id,
query=query,
attachment_ids=attachment_ids,
diff --git a/api/tests/unit_tests/services/test_hit_testing_service.py b/api/tests/unit_tests/services/test_hit_testing_service.py
index e3b2739068a..f8f9cdea897 100644
--- a/api/tests/unit_tests/services/test_hit_testing_service.py
+++ b/api/tests/unit_tests/services/test_hit_testing_service.py
@@ -7,7 +7,9 @@ import pytest
from sqlalchemy import select
from sqlalchemy.orm import Session
+from core.rag.index_processor.constant.index_type import IndexTechniqueType
from core.rag.models.document import Document
+from core.rag.retrieval.retrieval_methods import RetrievalMethod
from models import Account
from models.dataset import Dataset, DatasetQuery
from services.hit_testing_service import HitTestingService
@@ -99,3 +101,50 @@ class TestHitTestingServiceRetrieveTransaction:
assert query_log is not None
assert query_log.dataset_id == dataset.id
assert query_log.created_by == account.id
+
+
+class TestHitTestingServiceRetrievalMethod:
+ @pytest.mark.parametrize(
+ ("indexing_technique", "retrieval_model", "expected_method"),
+ [
+ (IndexTechniqueType.ECONOMY, None, RetrievalMethod.KEYWORD_SEARCH),
+ (
+ IndexTechniqueType.ECONOMY,
+ {
+ "search_method": RetrievalMethod.SEMANTIC_SEARCH,
+ "reranking_enable": False,
+ "score_threshold_enabled": False,
+ "top_k": 4,
+ },
+ RetrievalMethod.KEYWORD_SEARCH,
+ ),
+ (IndexTechniqueType.HIGH_QUALITY, None, RetrievalMethod.SEMANTIC_SEARCH),
+ ],
+ ids=["economy-default", "economy-requested-semantic", "high-quality-default"],
+ )
+ def test_retrieve_should_use_keyword_search_only_for_economy_datasets(
+ self,
+ sqlite_session: Session,
+ indexing_technique: IndexTechniqueType,
+ retrieval_model: dict[str, object] | None,
+ expected_method: RetrievalMethod,
+ ) -> None:
+ dataset = Dataset(
+ id="dataset-123",
+ tenant_id="tenant-123",
+ provider="vendor",
+ retrieval_model=None,
+ indexing_technique=indexing_technique,
+ )
+ account = Account(name="Test User", email="[email protected]")
+ account.id = "user-789"
+ query = "test query"
+
+ with (
+ patch("services.hit_testing_service.RetrievalService.retrieve", autospec=True) as mock_retrieve,
+ ):
+ no_documents: list[Document] = []
+ mock_retrieve.return_value = no_documents
+ HitTestingService.retrieve(dataset, query, account, retrieval_model, {}, session=sqlite_session)
+
+ assert mock_retrieve.call_args.kwargs["retrieval_method"] == expected_method