|
| 1 | +/* |
| 2 | +* Copyright 2014 the original author or authors. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +*/ |
| 16 | +package org.springframework.data.elasticsearch.core; |
| 17 | + |
| 18 | +import static org.apache.commons.lang.RandomStringUtils.*; |
| 19 | +import static org.elasticsearch.index.query.QueryBuilders.*; |
| 20 | +import static org.hamcrest.Matchers.*; |
| 21 | +import static org.junit.Assert.*; |
| 22 | + |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.runner.RunWith; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.data.elasticsearch.builder.SampleEntityBuilder; |
| 32 | +import org.springframework.data.elasticsearch.core.query.*; |
| 33 | +import org.springframework.data.elasticsearch.entities.SampleEntity; |
| 34 | +import org.springframework.test.context.ContextConfiguration; |
| 35 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Mohsin Husen |
| 39 | + */ |
| 40 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 41 | +@ContextConfiguration("classpath:elasticsearch-template-test.xml") |
| 42 | +public class AliasTests { |
| 43 | + |
| 44 | + private static final String INDEX_NAME = "test-alias-index"; |
| 45 | + private static final String TYPE_NAME = "test-alias-type"; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private ElasticsearchTemplate elasticsearchTemplate; |
| 49 | + |
| 50 | + @Before |
| 51 | + public void before() { |
| 52 | + Map<String, Object> settings = new HashMap<String, Object>(); |
| 53 | + settings.put("index.refresh_interval", "-1"); |
| 54 | + settings.put("index.number_of_replicas", "0"); |
| 55 | + settings.put("index.number_of_shards", "2"); |
| 56 | + settings.put("index.store.type", "memory"); |
| 57 | + |
| 58 | + elasticsearchTemplate.deleteIndex(INDEX_NAME); |
| 59 | + elasticsearchTemplate.createIndex(INDEX_NAME, settings); |
| 60 | + elasticsearchTemplate.refresh(INDEX_NAME, true); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldAddAlias() { |
| 65 | + // given |
| 66 | + String aliasName = "test-alias"; |
| 67 | + AliasQuery aliasQuery = new AliasBuilder() |
| 68 | + .withIndexName(INDEX_NAME) |
| 69 | + .withAliasName(aliasName).build(); |
| 70 | + // when |
| 71 | + elasticsearchTemplate.addAlias(aliasQuery); |
| 72 | + // then |
| 73 | + Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME); |
| 74 | + assertThat(aliases, is(notNullValue())); |
| 75 | + assertThat(aliases.contains(aliasName), is(true)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldRemoveAlias() { |
| 80 | + // given |
| 81 | + String indexName = INDEX_NAME; |
| 82 | + String aliasName = "test-alias"; |
| 83 | + AliasQuery aliasQuery = new AliasBuilder() |
| 84 | + .withIndexName(indexName) |
| 85 | + .withAliasName(aliasName).build(); |
| 86 | + // when |
| 87 | + elasticsearchTemplate.addAlias(aliasQuery); |
| 88 | + Set<String> aliases = elasticsearchTemplate.queryForAlias(indexName); |
| 89 | + assertThat(aliases, is(notNullValue())); |
| 90 | + assertThat(aliases.contains(aliasName), is(true)); |
| 91 | + // then |
| 92 | + elasticsearchTemplate.removeAlias(aliasQuery); |
| 93 | + aliases = elasticsearchTemplate.queryForAlias(indexName); |
| 94 | + assertThat(aliases, is(notNullValue())); |
| 95 | + assertThat(aliases.size(), is(0)); |
| 96 | + } |
| 97 | + |
| 98 | + /* |
| 99 | + DATAES-70 |
| 100 | + */ |
| 101 | + @Test |
| 102 | + public void shouldAddAliasWithGivenRoutingValue() { |
| 103 | + //given |
| 104 | + String indexName = INDEX_NAME; |
| 105 | + String alias = "test-alias"; |
| 106 | + |
| 107 | + AliasQuery aliasQuery = new AliasBuilder() |
| 108 | + .withIndexName(indexName) |
| 109 | + .withAliasName(alias) |
| 110 | + .withRouting("0").build(); |
| 111 | + |
| 112 | + //when |
| 113 | + elasticsearchTemplate.addAlias(aliasQuery); |
| 114 | + |
| 115 | + String documentId = randomNumeric(5); |
| 116 | + SampleEntity sampleEntity = new SampleEntityBuilder(documentId) |
| 117 | + .message("some message") |
| 118 | + .version(System.currentTimeMillis()).build(); |
| 119 | + |
| 120 | + IndexQuery indexQuery = new IndexQueryBuilder() |
| 121 | + .withIndexName(alias) |
| 122 | + .withId(sampleEntity.getId()) |
| 123 | + .withType(TYPE_NAME) |
| 124 | + .withObject(sampleEntity) |
| 125 | + .build(); |
| 126 | + |
| 127 | + elasticsearchTemplate.index(indexQuery); |
| 128 | + elasticsearchTemplate.refresh(INDEX_NAME, true); |
| 129 | + |
| 130 | + SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) |
| 131 | + .withIndices(alias).withTypes(TYPE_NAME).build(); |
| 132 | + long count = elasticsearchTemplate.count(query); |
| 133 | + //then |
| 134 | + Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME); |
| 135 | + assertThat(aliases, is(notNullValue())); |
| 136 | + assertThat(aliases.contains(alias), is(true)); |
| 137 | + assertThat(count, is(1L)); |
| 138 | + |
| 139 | + //cleanup |
| 140 | + elasticsearchTemplate.removeAlias(aliasQuery); |
| 141 | + elasticsearchTemplate.deleteIndex(SampleEntity.class); |
| 142 | + elasticsearchTemplate.createIndex(SampleEntity.class); |
| 143 | + elasticsearchTemplate.putMapping(SampleEntity.class); |
| 144 | + elasticsearchTemplate.refresh(SampleEntity.class, true); |
| 145 | + } |
| 146 | + |
| 147 | + /* |
| 148 | + DATAES-70 |
| 149 | + */ |
| 150 | + @Test |
| 151 | + public void shouldAddAliasForVariousRoutingValues() { |
| 152 | + //given |
| 153 | + String alias1 = "test-alias-1"; |
| 154 | + String alias2 = "test-alias-2"; |
| 155 | + |
| 156 | + AliasQuery aliasQuery1 = new AliasBuilder() |
| 157 | + .withIndexName(INDEX_NAME) |
| 158 | + .withAliasName(alias1) |
| 159 | + .withIndexRouting("0").build(); |
| 160 | + |
| 161 | + AliasQuery aliasQuery2 = new AliasBuilder() |
| 162 | + .withIndexName(INDEX_NAME) |
| 163 | + .withAliasName(alias2) |
| 164 | + .withSearchRouting("1").build(); |
| 165 | + |
| 166 | + //when |
| 167 | + elasticsearchTemplate.addAlias(aliasQuery1); |
| 168 | + elasticsearchTemplate.addAlias(aliasQuery2); |
| 169 | + |
| 170 | + String documentId = randomNumeric(5); |
| 171 | + SampleEntity sampleEntity = new SampleEntityBuilder(documentId) |
| 172 | + .message("some message") |
| 173 | + .version(System.currentTimeMillis()).build(); |
| 174 | + |
| 175 | + IndexQuery indexQuery = new IndexQueryBuilder() |
| 176 | + .withIndexName(alias1) |
| 177 | + .withType(TYPE_NAME) |
| 178 | + .withId(sampleEntity.getId()) |
| 179 | + .withObject(sampleEntity).build(); |
| 180 | + |
| 181 | + elasticsearchTemplate.index(indexQuery); |
| 182 | + elasticsearchTemplate.refresh(SampleEntity.class, true); |
| 183 | + |
| 184 | + SearchQuery query = new NativeSearchQueryBuilder().withQuery(matchAllQuery()) |
| 185 | + .withIndices(alias2).withTypes(TYPE_NAME).build(); |
| 186 | + long count = elasticsearchTemplate.count(query, SampleEntity.class); |
| 187 | + // then |
| 188 | + Set<String> aliases = elasticsearchTemplate.queryForAlias(INDEX_NAME); |
| 189 | + assertThat(aliases, is(notNullValue())); |
| 190 | + assertThat(aliases.contains(alias1), is(true)); |
| 191 | + assertThat(aliases.contains(alias2), is(true)); |
| 192 | + assertThat(count, is(0L)); |
| 193 | + |
| 194 | + //cleanup |
| 195 | + elasticsearchTemplate.removeAlias(aliasQuery1); |
| 196 | + elasticsearchTemplate.removeAlias(aliasQuery2); |
| 197 | + elasticsearchTemplate.deleteIndex(SampleEntity.class); |
| 198 | + elasticsearchTemplate.createIndex(SampleEntity.class); |
| 199 | + elasticsearchTemplate.putMapping(SampleEntity.class); |
| 200 | + elasticsearchTemplate.refresh(SampleEntity.class, true); |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments