SolrDocumentStore

SolrDocumentStore

SolrDocumentStore 는 Apache Solr에 문서를 저장하고 검색하는 Document Store예요. 조직에서 이미 Solr을 운영 중이라면, 별도의 벡터 데이터베이스를 도입하지 않고도 그 위에 의미 검색이나 하이브리드 검색을 얹을 수 있는 좋은 선택이에요.

출처: 문서

본문

Apache Solr는 Apache Lucene 위에 만들어진 널리 쓰이는 오픈소스 검색 서버예요. Solr 9부터 DenseVectorField 타입과 {!knn} 쿼리 파서가 제공되어, 단일 Solr core가 키워드(BM25)와 밀집 벡터 검색을 동시에 처리할 수 있어요. 자세한 내용은 Solr 문서를 참고하세요.

이 Document Store는 Solr 9.6 이상이 필요해요. 모든 연산은 동기·비동기로 모두 사용 가능해요.

초기화 (Initialization)

먼저 Solr 인스턴스를 설치하고 실행해요. Docker가 있다면 Docker 이미지를 받아 미리 생성된 core로 실행하는 걸 권장해요:

docker run -d -p 8983:8983 solr:10 solr-precreate haystack

실행 중인 Solr 인스턴스가 준비되면 solr-haystack 통합을 설치해요:

pip install solr-haystack

그런 다음 Solr 인스턴스에 연결된 SolrDocumentStore 객체를 초기화하고 문서를 작성해요:

from haystack import Document
from haystack_integrations.document_stores.solr import SolrDocumentStore

document_store = SolrDocumentStore(
    url="http://localhost:8983/solr",
    core="haystack",
    embedding_dim=768,
)
document_store.write_documents(
    [Document(content="This is first"), Document(content="This is second")],
)
print(document_store.count_documents())

기본적으로 스토어가 Solr 스키마를 스스로 관리해요. 첫 사용 시 필요한 필드를 만들고 Solr의 schemaless 필드 추측을 비활성화해요. 직접 스키마를 관리하려면 manage_schema=False 로 설정하세요.

몇 가지 유의할 점이 있어요:

  • url 은 SOLR_URL 환경 변수로 대체되고, 그다음 http://localhost:8983/solr 로 대체돼요. 기본 인증 자격증명은 기본적으로 SOLR_USERNAME 과 SOLR_PASSWORD 환경 변수에서 읽으므로, 코드나 직렬화된 파이프라인에 나타날 일이 없어요.
  • Solr는 벡터 필드를 만들 때 차원을 고정하므로, 기존 core의 embedding_dim 은 바꿀 수 없어요. similarity_function 은 cosine(기본값), dot_product, euclidean 중에서 선택할 수 있어요.

지원되는 리트리버 (Supported Retrievers)

  • SolrBM25Retriever: Document Store에서 질의와 일치하는 문서를 가져오는 키워드 기반 리트리버
  • SolrEmbeddingRetriever: 질의와 문서 임베딩을 비교해 질의와 가장 관련 있는 문서를 가져오는 리트리버
  • SolrHybridRetriever: BM25와 임베딩 검색을 단일 컴포넌트로 결합해 결과를 퓨전하는 SuperComponent

확장 메서드 (Extended Methods)

표준 Document Store 프로토콜 외에도 SolrDocumentStore 는 필터 기반 벌크 연산과 메타데이터 조회를 지원하며, 각각 async 버전도 있어요:

  • delete_by_filter / update_by_filter: 필터와 일치하는 모든 문서의 메타데이터를 삭제하거나 업데이트해요.
  • count_documents_by_filter / count_unique_metadata_by_filter: 일치하는 문서 수 또는 메타데이터 필드의 고유 값을 세어요.
  • get_metadata_fields_info, get_metadata_field_min_max, get_metadata_field_unique_values: 어떤 메타데이터 필드가 존재하고, 그 타입과 값 범위가 무엇인지 조회해요.

더 알아보기 (Learn more)