Spring Boot アプリケーションにおけるEasysearchクライアント統合の実装

概要

多くの開発者は、既存のElasticsearchクライアントをEasysearchに移行する必要性に直面しています。本稿では、Spring BootアプリケーションでHigh-Level RESTクライアントを使用してEasysearchに接続する方法について、設定から基本操作まで詳しく解説します。

サーバー側の設定

最初に、Easysearchノードの設定ファイルを修正する必要があります。easysearch.ymlファイルを開き、以下のパラメータを追加または編集してください。

elasticsearch.api_compatibility: true
elasticsearch.api_compatibility_version: "7.17.18"

api_compatibility_versionの値は、使用するクライアントライブラリのバージョンに合わせて調整してください。

プロジェクトの依存関係設定

Mavenベースのプロジェクトでは、pom.xmlファイルに以下の依存関係を定義します。

<properties>
    <java.version>11</java.version>
    <spring-data-elasticsearch.version>4.4.18</spring-data-elasticsearch.version>
    <elasticsearch.version>7.17.18</elasticsearch.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-elasticsearch</artifactId>
        <version>${spring-data-elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>${elasticsearch.version}</version>
    </dependency>
</dependencies>

クライアント接続の構成

アプリケーションの設定ファイルで接続情報を指定します。application.ymlファイルに以下の内容を追加してください。

spring:
  elasticsearch:
    rest:
      uris: https://localhost:9202
      username: admin
      password: your_password_here
    ssl:
      verification-mode: none

次に、クライアント設定クラスを作成します。

@Configuration
public class SearchEngineConfig extends AbstractElasticsearchConfiguration {

    @Value("${spring.elasticsearch.rest.uris}")
    private String serverUrl;

    @Value("${spring.elasticsearch.rest.username}")
    private String user;

    @Value("${spring.elasticsearch.rest.password}")
    private String pass;

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        BasicCredentialsProvider credentialProvider = new BasicCredentialsProvider();
        credentialProvider.setCredentials(
            AuthScope.ANY,
            new UsernamePasswordCredentials(user, pass)
        );

        SSLContext sslContext = SSLContexts.custom()
            .loadTrustMaterial(null, (certificates, authType) -> true)
            .build();

        RestClientBuilder clientBuilder = RestClient.builder(
            HttpHost.create(serverUrl)
        ).setHttpClientConfigCallback(httpAsyncClientBuilder ->
            httpAsyncClientBuilder
                .setDefaultCredentialsProvider(credentialProvider)
                .setSSLContext(sslContext)
                .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
        );

        return new RestHighLevelClient(clientBuilder);
    }
}

データモデルの定義

検索エンジンに格納するデータ構造を定義します。

@Document(indexName = "item_catalog")
public class CatalogItem {
    @Id
    private String itemId;

    @Field(type = FieldType.Text, name = "item_name")
    private String itemName;

    @Field(type = FieldType.Double, name = "item_price")
    private Double itemPrice;

    // コンストラクタ、ゲッター、セッター
}

データアクセス層の実装

Spring Dataのリポジトリインターフェースを拡張してデータ操作を定義します。

@Repository
public interface ItemRepository extends ElasticsearchRepository {
}

ビジネスロジック層

サービスクラスでアプリケーションの主要機能を実装します。

@Service
public class CatalogService {
    private final ItemRepository itemRepository;

    public CatalogService(ItemRepository repository) {
        this.itemRepository = repository;
    }

    public CatalogItem storeItem(CatalogItem item) {
        return itemRepository.save(item);
    }

    public CatalogItem fetchItemById(String identifier) {
        return itemRepository.findById(identifier).orElse(null);
    }
}

結合テストの実装

システムの動作を検証するテストクラスを作成します。

@SpringBootTest
public class CatalogIntegrationTest {
    @Autowired
    private ElasticsearchOperations searchOperations;

    @Autowired
    private CatalogService catalogService;

    private static final String TEST_INDEX = "item_catalog";

    @BeforeEach
    void initialize() {
        IndexOperations idxOps = searchOperations.indexOps(
            IndexCoordinates.of(TEST_INDEX)
        );
        
        if (idxOps.exists()) {
            idxOps.delete();
        }

        Document fieldMapping = Document.create()
            .append("properties", Document.create()
                .append("item_name", Document.create()
                    .append("type", "text")
                    .append("analyzer", "standard"))
                .append("item_price", Document.create()
                    .append("type", "double")));

        idxOps.create(Map.of(), fieldMapping);
    }

    @Test
    void verifyItemStorageAndRetrieval() {
        List<CatalogItem> testItems = List.of(
            new CatalogItem("Sample Item A", 49.99),
            new CatalogItem("Sample Item B", 129.99),
            new CatalogItem("Sample Item C", 299.99)
        );

        List<IndexQuery> indexRequests = testItems.stream()
            .map(item -> new IndexQueryBuilder()
                .withObject(item)
                .withIndex(TEST_INDEX)
                .build())
            .collect(Collectors.toList());

        List<IndexedObjectInformation> results = searchOperations.bulkIndex(
            indexRequests,
            IndexCoordinates.of(TEST_INDEX)
        );

        List<String> storedIds = results.stream()
            .map(IndexedObjectInformation::getId)
            .collect(Collectors.toList());

        assertFalse(storedIds.isEmpty());
        assertEquals(testItems.size(), storedIds.size());
    }
}

タグ: Easysearch Spring Boot Elasticsearch Client Search Engine Java

6月25日 22:24 投稿