如何使用Spring数据与弹性搜索别名交互 [英] How to interact with elastic search Alias using Spring data

查看:96
本文介绍了如何使用Spring数据与弹性搜索别名交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用弹性搜索Spring数据.我项目的领域结构一直在变化,因此我必须删除索引才能每次更改映射.为了克服这个问题,我使用了别名.我使用以下方法创建了别名:

Hi I am using elastic search Spring data. Domain structure of my project keeps on changing.So I have to drop the index in order to change the mapping every time. To overcome this problem, I am using 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);

我有一个测试班:

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



}

TestRepository类:

TestRepository Class:

package com.as.core.repositories

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

interface TestRepository extends ElasticsearchRepository<Test, String>          
{


}

我的问题是如何从别名而不是索引本身读取?是否也对别名进行写操作.我看了以下链接: https://www.elastic.co/guide/zh-CN/elasticsearch/guide/current/index-aliases.html#index-aliases 它说我们将不得不交互别名而不是实际索引.如何使用Elasticsearch Spring数据Java API实现此目的.

My question is how can I read from alias instead of the index itself? Does write operation also takes place on alias. I have looked at following link: https://www.elastic.co/guide/en/elasticsearch/guide/current/index-aliases.html#index-aliases It says that we will have to interact the alias instead of the actual index.How to achieve this using Elasticsearch Spring data Java API.

推荐答案

我通过在与对象相关联的存储库类中使用ElasticsearchTemplate来解决此限制(尽管如果有一种方法可以指定一个实体本身的别名).

I have worked around this limitation by using the ElasticsearchTemplate in the repository class associated with the object (although it would be much nicer if there was a way to specify an alias name on the entity itself).

它的工作方式是创建自定义存储库界面.在您的情况下,将为TestRepositoryCustom:

The way it works is to create a custom repository interface. In your case it would be TestRepositoryCustom:

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

然后实现此接口,在基本存储库名称的末尾附加'Impl':

Then implement this interface appending 'Impl' to the end of the base repository name:

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访问自定义接口上的任何方法.

Finally in your base JPA repsitory interface, TestRepository, extend the TestRepositoryCustom interface to get access to any methods on your custom interface from your repository bean.

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

我真正想看到的是对这样的实体的注释:

What I would really like to see is an annotation on the entity like:

@Document(aliasName="test-alias")

这只会在后台工作,以提供对这个索引的搜索,因此无论索引名如何,所有jpa查询都将正常工作.

This would just work in the background to provide searching on this index out of the gate so that all the jpa queries would just work regardless of the index name.

这篇关于如何使用Spring数据与弹性搜索别名交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆