Interface ContentRetriever

All Known Implementing Classes:
AzureAiSearchContentRetriever, AzureCosmosDBNoSqlContentRetriever, ElasticsearchContentRetriever, EmbeddingStoreContentRetriever, HibernateContentRetriever, SqlDatabaseContentRetriever, WebSearchContentRetriever

public interface ContentRetriever
Retrieves Contents from an underlying data source using a given Query.
The goal is to retrieve only relevant Contents in relation to a given Query.
The underlying data source can be virtually anything:
- Embedding (vector) store (see EmbeddingStoreContentRetriever)
- Full-text search engine (see AzureAiSearchContentRetriever in the langchain4j-azure-ai-search module)
- Hybrid of vector and full-text search (see AzureAiSearchContentRetriever in the langchain4j-azure-ai-search module)
- Web Search Engine (see WebSearchContentRetriever)
- Knowledge graph (see Neo4jContentRetriever in the langchain4j-community-neo4j-retriever module)
- SQL database (see SqlDatabaseContentRetriever in the langchain4j-experimental-sql module)
- etc.
See Also:
  • Method Details

    • retrieve

      List<Content> retrieve(Query query)
      Retrieves relevant Contents using a given Query. The Contents are sorted by relevance, with the most relevant Contents appearing at the beginning of the returned List<Content>.
      Parameters:
      query - The Query to use for retrieval.
      Returns:
      A list of retrieved Contents.
    • retrieveAsync

      @Experimental default CompletableFuture<List<Content>> retrieveAsync(Query query)
      Non-blocking counterpart of retrieve(Query), invoked by the asynchronous (CompletableFuture/CompletionStage) and reactive (Flow.Publisher) AI Service modes when RAG is configured.

      The default implementation returns a failed future carrying AsyncNotSupportedException: a retriever backed by blocking I/O (embedding-model call, vector-store query, web search, etc.) must opt in by overriding this method so it can return a genuinely non-blocking future instead of secretly tying up a worker thread. A retriever that cannot be made non-blocking is still usable from these modes via DefaultRetrievalAugmentor, which offloads the blocking retrieve(Query) to its executor.

      An implementation that honors cancellation should abort its in-flight I/O when the returned future is cancelled (best-effort); Java cannot safely interrupt arbitrary code, so this is not guaranteed.

      Parameters:
      query - The Query to use for retrieval.
      Returns:
      A CompletableFuture of the retrieved Contents, sorted by relevance.
      Since:
      1.20.0
    • addListener

      Wraps this ContentRetriever with a listening retriever that dispatches events to the provided listener.
      Parameters:
      listener - The listener to add.
      Returns:
      An observing ContentRetriever that will dispatch events to the provided listener.
      Since:
      1.11.0
    • addListeners

      Wraps this ContentRetriever with a listening retriever that dispatches events to the provided listeners.

      Listeners are called in the order of iteration.

      Parameters:
      listeners - The listeners to add.
      Returns:
      An observing ContentRetriever that will dispatch events to the provided listeners.
      Since:
      1.11.0