Skip to content

Commit 3780276

Browse files
Faisal Ferozakonczak
authored andcommitted
DATAES-130 Added support for Spel Expressions in Type
- Added support for Spel Expressions in index type - Renamed SpELEntityTest to SpELEntityTests so they run when called from maven
1 parent 1255dfa commit 3780276

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public String getIndexName() {
9595

9696
@Override
9797
public String getIndexType() {
98-
return indexType;
98+
Expression expression = parser.parseExpression(indexType, ParserContext.TEMPLATE_EXPRESSION);
99+
return expression.getValue(context, String.class);
99100
}
100101

101102
@Override

src/test/java/org/springframework/data/elasticsearch/SpELEntityTest.java renamed to src/test/java/org/springframework/data/elasticsearch/SpELEntityTests.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@
3434
* SpELEntityTest
3535
*
3636
* @author Artur Konczak
37-
*
3837
*/
3938

4039
@RunWith(SpringJUnit4ClassRunner.class)
4140
@ContextConfiguration("classpath:/spel-repository-test.xml")
42-
public class SpELEntityTest {
43-
41+
public class SpELEntityTests {
4442

45-
@Autowired
46-
private SpELRepository repository;
43+
@Autowired private SpELRepository repository;
4744

48-
@Autowired
49-
private ElasticsearchTemplate template;
45+
@Autowired private ElasticsearchTemplate template;
5046

5147
@Before
5248
public void init() {
@@ -55,15 +51,31 @@ public void init() {
5551

5652
@Test
5753
public void shouldDo() {
58-
//Given
54+
// Given
5955
repository.save(new SpELEntity());
6056
repository.save(new SpELEntity());
61-
//When
57+
// When
6258

63-
//Then
59+
// Then
6460
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(QueryBuilders.matchAllQuery());
6561
nativeSearchQuery.addIndices("abz-entity");
6662
long count = template.count(nativeSearchQuery);
6763
assertThat(count, is(2L));
6864
}
65+
66+
@Test
67+
public void shouldSupportSpelInType() {
68+
// Given
69+
SpELEntity spELEntity = new SpELEntity();
70+
repository.save(spELEntity);
71+
72+
// When
73+
74+
// Then
75+
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(QueryBuilders.matchAllQuery());
76+
nativeSearchQuery.addIndices("abz-entity");
77+
nativeSearchQuery.addTypes("myType");
78+
long count = template.count(nativeSearchQuery);
79+
assertThat(count, is(1L));
80+
}
6981
}

src/test/java/org/springframework/data/elasticsearch/entities/SpELEntity.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@
2222
* SpELEntity
2323
*
2424
* @author Artur Konczak
25-
*
2625
*/
27-
@Document(indexName = "#{'abz'+'-'+'entity'}", type = "spel", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
26+
@Document(indexName = "#{'abz'+'-'+'entity'}", type = "#{'my'+'Type'}", indexStoreType = "memory", shards = 1,
27+
replicas = 0, refreshInterval = "-1")
2828
public class SpELEntity {
2929

30-
@Id
31-
private String id;
30+
@Id private String id;
3231

3332
public String getId() {
3433
return id;

0 commit comments

Comments
 (0)