How to interact with elastic search alias using Spring data

Hi, I am using Spring resilient search data. The domain structure of my project continues to change. So I need to drop the index in order to change the display every time. To overcome this problem, I use aliases. I created an alias using:

elasticsearchTemplate.createIndex(Test.class);
elasticsearchTemplate.putMapping(Test.class);

    String aliasName = "test-alias";
    AliasQuery aliasQuery = new AliasBuilder()
            .withIndexName("test")
            .withAliasName(aliasName).build();

    elasticsearchTemplate.addAlias(aliasQuery);

I have a test class:

import org.springframework.data.annotation.Id
import org.springframework.data.elasticsearch.annotations.Document
import org.springframework.data.elasticsearch.annotations.Field
import org.springframework.data.elasticsearch.annotations.FieldIndex
import org.springframework.data.elasticsearch.annotations.FieldType
import org.springframework.data.elasticsearch.annotations.Setting


@Document(indexName = "test", type = "test")
@Setting(settingPath = 'elasticSearchSettings/analyzer.json')
class Test  extends BaseEntity{

@Id
@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
String id

@Field(type = FieldType.String, index = FieldIndex.analyzed, indexAnalyzer = "generic_analyzer", searchAnalyzer = "generic_analyzer")
String firstName



}

Class TestRepository:

package com.as.core.repositories

import com.as.core.entities.Test
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository

interface TestRepository extends ElasticsearchRepository<Test, String>          
{


}

My question is: how can I read from an alias instead of the index itself? Whether the write operation is performed in an alias. I looked at the following link: https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html#index-aliases It says that we will have to interact with an alias instead of the actual index. How to achieve this using Spring Java APIs.

+4
1

, ElasticsearchTemplate , ( , ).

, , - . TestRepositoryCustom:

public interface TestRepositoryCustom
{
    Test> findByCustom(...);
}

, "Impl" :

public class TestRepositoryImpl implements TestRepositoryCustom
{
    Page<Test> findByCustom(Pageable pageable, ...)
    {
        BoolQueryBuilder boolQuery = new BoolQueryBuilder();
        FilterBuilder filter = FilterBuilders.staticMethodsToBuildFilters;
        /*
         * Your code here to setup your query
        */

        NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder().withQuery(boolQuery).withFilter(filter).withPageable(pageable); 

        //These two are the crucial elements that will allow the search to look up based on alias
        builder.withIndices("test-alias");
        builder.withTypes("test");

        //Execute the query
        SearchQuery searchQuery = builder.build();
        return elasticSearchTemplate.queryForPage(searchQuery, Test.class);
    }
}

, JPA TestRepository TestRepositoryCustom, bean.

public interface TestRepository extends ElasticsearchRepository<Consultant, String>, TestRepositoryCustom
{
}

, , - :

@Document(aliasName="test-alias")

, , jpa .

+6

All Articles