2015-08-14 117 views
4

嗨我正在使用弹性搜索弹簧数据。我的项目的域结构不断变化。所以我必须删除索引以便每次更改映射。为了克服这个问题,我使用别名。 我创建了使用别名:如何使用弹簧数据与弹性搜索别名进行交互

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类:

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/en/elasticsearch/guide/current/index-aliases.html#index-aliases 它说,我们将不得不互动的别名,而不是实际的索引。如何使用Elasticsearch Spring数据Java API来实现这一目标。

回答

6

我已经通过在与对象关联的存储库类中使用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 repsitory接口,TestRepository,延长TestRepositoryCustom接口可以从存储库bean访问自定义接口上的任何方法。

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

我真的想看到的是像实体注释:

@Document(aliasName="test-alias") 

这只是在后台工作,以提供有关这一指标寻找出了大门,使所有的无论索引名称如何,jpa查询都可以正常工作。

+0

这是一个开放的问题:https://jira.spring.io/browse/DATAES-192 –

相关问题