2015-08-14 44 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 –

相關問題