Elasticsearch
Maven依存関係
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-elasticsearch</artifactId>
<version>1.18.1-beta28</version>
</dependency>
概要
langchain4j-elasticsearch モジュールは、埋め込みストアおよびコンテンツリトリーバーとしての
Elasticsearch 連携を提供します。
主なクラスは次の 2 つです:
ElasticsearchEmbeddingStore:EmbeddingStoreインターフェースの実装で、 Elasticsearch を使って埋め込みを保存・取得します。ElasticsearchContentRetriever:ContentRetrieverインターフェースの実装で、Elasticsearch のベクトル類似度検索により関連ドキュメントを取得します。
どちらのクラスも、Elasticsearch サーバーに接続するための Elasticsearch Client が必要です。
String apiKey = "VnVhQ2ZHY0JDZGJrU...";
ElasticsearchClient client = ElasticsearchClient.of(ec -> ec
.host("https://localhost:9200")
.apiKey(apiKey));
注意:
ElasticsearchClient インスタンスの作成方法については、 Elasticsearch ドキュメント を参照してください。
ElasticsearchEmbeddingStore
ElasticsearchEmbeddingStore インスタンスを作成するには、ElasticsearchClient を渡します:
ElasticsearchEmbeddingStore store = ElasticsearchEmbeddingStore.builder()
.client(client)
.build();
次のオプションがあります:
indexName:使用する Elasticsearch インデックス名。デフォ ルトはdefault。configuration:使用するElasticsearchConfiguration。デフォルトはElasticsearchConfigurationKnn。
上記のコードは次と同等です:
ElasticsearchEmbeddingStore store = ElasticsearchEmbeddingStore.builder()
.client(client)
.configuration(ElasticsearchConfigurationKnn.builder().build())
.indexName("default")
.build();
ElasticsearchContentRetriever
ContentRetriever には埋め込みモデルが必要です:
EmbeddingModel embeddingModel = new AllMiniLmL6V2QuantizedEmbeddingModel();
ElasticsearchContentRetriever インスタンスを作成するには、ElasticsearchClient と
EmbeddingModel を渡します:
ElasticsearchContentRetriever contentRetriever = ElasticsearchContentRetriever.builder()
.client(client)
.embeddingModel(embeddingModel)
.build();
次のオプションがあります:
configuration:使用するElasticsearchConfiguration(下記参照)。デフォルトはElasticsearchConfigurationKnn。indexName:使用する Elasticsearch インデックス名。デフォルトはdefault。インデックスが存在しない場合は 自動作成されます。maxResults:取得する結果の最大件数。デフォルトは3。minScore:取得結果の最小スコア閾値。デフォルトは0.0。filter:取得時に適用するFilter(任意)。デフォルトはnull。
上記のコードは次と同等です:
ElasticsearchContentRetriever contentRetriever = ElasticsearchContentRetriever.builder()
.client(client)
.embeddingModel(embeddingModel)
.configuration(ElasticsearchConfigurationKnn.builder().build())
.indexName("default")
.maxResults(3)
.minScore(0.0)
.filter(null)
.build();
ElasticsearchConfiguration
ElasticsearchConfiguration は、埋め込みストアまたはコンテンツリトリーバーが
Elasticsearch サーバーとどのようにやり取りするかを定義します。ElasticsearchConfiguration インターフェースを実装して独自設定を作成するか、
提供されている次の実装のいずれかを使用できます:
ElasticsearchConfigurationKnn:近似 kNN クエリ を使用 (デフォルト)。ElasticsearchConfigurationScript:scriptScore クエリ を使用。 この実装はコサイン類似度を使用します。ElasticsearchConfigurationFullText:全文検索 を使用 (コンテンツリトリーバー専用)。ElasticsearchConfigurationHybrid:ハイブリッド検索 を使用 (コンテンツリトリーバー専用、有料ライセンスが必要)。kNN ベクトルクエリと全文クエリを組み合わせます。
設定インスタンスを作成するには、各実装が提供する builder を使用できます。例:
ElasticsearchConfiguration configuration = ElasticsearchConfigurationKnn.builder().build();
ElasticsearchConfigurationKnn
ElasticsearchConfigurationKnn は近似 kNN クエリでベクトル類似度検索を行います。
ElasticsearchEmbeddingStore
および ElasticsearchContentRetriever のデフォルト設定です。
インスタンスを作成するには builder を使用します:
ElasticsearchConfiguration configuration = ElasticsearchConfigurationKnn.builder().build();
次のオプションがあります:
numCandidates:検索中に考慮する候補近傍の数。デフォルトはnullで、 Elasticsearch のデフォルト値を使用します。includeVectorResponse:検索レスポンスにベクトルフィールドを含めるかどうか。デフォルトはfalse。
注意: Elasticsearch サーバー 9.2 以降では、ベクトルフィールドはデフォルトでレスポンスから除外されます。レスポンスに ベクトルフィールドを含める場合(非推奨)は、builder で
includeVectorResponseを設定してください:ElasticsearchConfigurationKnn configuration = ElasticsearchConfigurationKnn.builder()
.includeVectorResponse(true)
.build();
ElasticsearchConfigurationScript
ElasticsearchConfigurationScript は scriptScore クエリでベクトル類似度検索を行います。この
実装はコサイン類似度を使用します。
ElasticsearchEmbeddingStore
および ElasticsearchContentRetriever の両方で利用できます。
インスタンスを作成するには builder を使用します:
ElasticsearchConfiguration configuration = ElasticsearchConfigurationScript.builder().build();
次のオプションがあります:
includeVectorResponse:検索レスポンスにベクトルフィールドを含めるかどうか。デフォルトはfalse。
注意: Elasticsearch サーバー 9.2 以降では、ベクトルフィールドはデフォルトでレスポンスから除外されます。レスポンスに ベクトルフィールドを含める場合(非推奨)は、builder で
includeVectorResponseを設定してください:ElasticsearchConfiguration configuration = ElasticsearchConfigurationScript.builder()
.includeVectorResponse(true)
.build();
ElasticsearchConfigurationFullText
ElasticsearchConfigurationFullText は全文検索で関連ドキュメントを取得します。
ElasticsearchContentRetriever 専用です。
インスタンスを作成するには builder を使用します:
ElasticsearchConfiguration configuration = ElasticsearchConfigurationFullText.builder().build();
ElasticsearchConfigurationHybrid
ElasticsearchConfigurationHybrid はハイブリッド検索で kNN ベクトルクエリと全文クエリを組み合わせます。
ハイブリッド検索には Elasticsearch のエンタープライズライセンスまたはトライアルが必要です。
ElasticsearchContentRetriever 専用です。
インスタンスを作成するには builder を使用します:
ElasticsearchConfiguration configuration = ElasticsearchConfigurationHybrid.builder().build();
次のオプションがあります:
numCandidates:検索中に考慮する候補近傍の数。デフォルトはnullで、 Elasticsearch のデフォルト値を使用します。includeVectorResponse:検索レスポンスにベクトルフィールドを含めるかどうか。デフォルトはfalse。
注意: Elasticsearch サーバー 9.2 以降では、ベクトルフィールドはデフォルトでレスポンスから除外されます。レスポンスに ベクトルフィールドを含める場合(非推奨)は、builder で
includeVectorResponseを設定してください:ElasticsearchConfiguration configuration = ElasticsearchConfigurationHybrid.builder()
.includeVectorResponse(true)
.build();
カスタム設定の作成
ElasticsearchConfiguration インターフェースを実装して、独自の Elasticsearch 設定を作成できます。例:
public class MyElasticsearchConfiguration implements ElasticsearchConfiguration {
@Override
SearchResponse<Document> vectorSearch(
ElasticsearchClient client,
String indexName,
EmbeddingSearchRequest embeddingSearchRequest) {
// Your optional custom vector search implementation here
}
@Override
SearchResponse<Document> fullTextSearch(
ElasticsearchClient client,
String indexName,
String textQuery) {
// Your optional custom full text search implementation here
}
@Override
SearchResponse<Document> hybridSearch(
ElasticsearchClient client,
String indexName,
EmbeddingSearchRequest embeddingSearchRequest,
String textQuery) {
// Your optional custom hybrid search implementation here
}
}
ユースケースに関連するメソッドだけを実装すれば十分です:
vectorSearch:ベクトル類似度検索用(ElasticsearchEmbeddingStoreとElasticsearchContentRetrieverの両方で使用)。fullTextSearch:全文検索用(ElasticsearchContentRetriever専用)。hybridSearch:ハイブリッド検索用(ElasticsearchContentRetriever専用)。