/** * Parse a String generated by {@link #format()} */ public static IndexType parse(String s) { List<String> split = SEPARATOR_SPLITTER.splitToList(s); if (split.size() != 2) { throw new IllegalArgumentException("Unsupported IndexType value: " + s); } return new IndexType(split.get(0), split.get(1)); }
/** * @return the identifier of the ElasticSearch type (including it's index name), that corresponds to a certain document type */ private static IndexType getAuthorizationIndexType(IndexType indexType) { requireNonNull(indexType); requireNonNull(indexType.getIndex()); checkArgument(!TYPE_AUTHORIZATION.equals(indexType.getType()), "Authorization types do not have authorization on their own."); return new IndexType(indexType.getIndex(), TYPE_AUTHORIZATION); }
public void putDocuments(String index, String type, BaseDoc... docs) { putDocuments(new IndexType(index, type), docs); }
public long countDocuments(String index, String type) { return countDocuments(new IndexType(index, type)); }
@Test public void type_should_be_not_initialized_by_default() { IndexType indexType = new IndexType("examples", "example"); assertThat(underTest.getInitialized(indexType)).isFalse(); }
@Test public void equals_and_hashCode() { IndexType type1 = new IndexType("foo", "bar"); IndexType type1b = new IndexType("foo", "bar"); IndexType type2 = new IndexType("foo", "baz"); assertThat(type1.equals(type1)).isTrue(); assertThat(type1.equals(type1b)).isTrue(); assertThat(type1.equals(type2)).isFalse(); assertThat(type1.hashCode()).isEqualTo(type1.hashCode()); assertThat(type1.hashCode()).isEqualTo(type1b.hashCode()); } }
@Test public void type_should_be_initialized_after_explicitly_set_to_initialized() { IndexType indexType = new IndexType("examples", "example"); underTest.setInitialized(indexType, true); assertThat(underTest.getInitialized(indexType)).isTrue(); }
@Test public void fail_if_bad_query() { IndexRequestBuilder requestBuilder = es.client().prepareIndex(new IndexType("unknownIndex", "unknownType")); try { requestBuilder.get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Fail to execute ES index request for key 'null' on index 'unknownIndex' on type 'unknownType'"); } }
@Test public void fail_if_bad_query_with_basic_profiling() { IndexRequestBuilder requestBuilder = es.client().prepareIndex(new IndexType("unknownIndex", "unknownType")); try { requestBuilder.get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Fail to execute ES index request for key 'null' on index 'unknownIndex' on type 'unknownType'"); } }
@Test public void fail_to_get_bad_query() { GetRequestBuilder requestBuilder = es.client().prepareGet(new IndexType("unknown", "test"), "rule1"); try { requestBuilder.get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()).contains("Fail to execute ES get request for key 'rule1' on index 'unknown' on type 'test'"); } }
public void clearIndexes() { Loggers.get(getClass()).info("Truncate Elasticsearch indices"); try { esClient.prepareClearCache().get(); for (String index : esClient.prepareState().get().getState().getMetaData().getConcreteAllIndices()) { clearIndex(new IndexType(index, index)); } } catch (Exception e) { throw new IllegalStateException("Unable to clear indexes", e); } }
@Test public void format_and_parse() { IndexType type1 = new IndexType("foo", "bar"); assertThat(type1.format()).isEqualTo("foo/bar"); IndexType type2 = IndexType.parse(type1.format()); assertThat(type2.equals(type1)).isTrue(); }
@Test public void mark_all_non_existing_index_types_as_uninitialized() { startNewCreator(context -> { NewIndex i = context.create("fakes", SETTINGS_CONFIGURATION); i.createType("t1"); i.createType("t2"); }); assertThat(metadataIndex.getHash("fakes")).isNotEmpty(); assertThat(metadataIndex.getInitialized(new IndexType("fakes", "t1"))).isFalse(); assertThat(metadataIndex.getInitialized(new IndexType("fakes", "t2"))).isFalse(); }
@Override protected void after() { if (isCustom) { // delete non-core indices String[] existingIndices = SHARED_NODE.client().admin().indices().prepareGetIndex().get().getIndices(); Stream.of(existingIndices) .filter(i -> !CORE_INDICES_NAMES.contains(i)) .forEach(EsTester::deleteIndexIfExists); } BulkIndexer.delete(client(), new IndexType("_all", ""), client().prepareSearch("_all").setQuery(matchAllQuery())); }
metadataIndex.setHash(index.getName(), IndexDefinitionHash.of(index)); for (IndexDefinition.Type type : index.getTypes().values()) { metadataIndex.setInitialized(new IndexType(index.getName(), type.getName()), false);
@Test public void unsupported_types_are_kept_in_queue_for_manual_fix_operation() { insertItem(FOO_TYPE, "f1"); ResilientIndexer indexer = new SuccessfulFakeIndexer(new IndexType("bars", "bar")); advanceInTime(); underTest = newRecoveryIndexer(indexer); underTest.recover(); assertThatQueueHasSize(1); assertThatLogsContain(ERROR, "Elasticsearch recovery - ignore 1 items with unsupported type [foos/foo]"); }
@Test public void proxify_requests() { EsClient underTest = es.client(); assertThat(underTest.nativeClient()).isNotNull(); assertThat(underTest.prepareClusterStats()).isInstanceOf(ProxyClusterStatsRequestBuilder.class); assertThat(underTest.prepareCreate("fakes")).isInstanceOf(ProxyCreateIndexRequestBuilder.class); assertThat(underTest.prepareDelete("fakes", "fake", "my_id")).isInstanceOf(ProxyDeleteRequestBuilder.class); assertThat(underTest.prepareIndicesExist()).isInstanceOf(ProxyIndicesExistsRequestBuilder.class); assertThat(underTest.prepareGet(new IndexType("fakes", "fake"), "1")).isInstanceOf(ProxyGetRequestBuilder.class); assertThat(underTest.prepareHealth()).isInstanceOf(ProxyClusterHealthRequestBuilder.class); assertThat(underTest.prepareNodesStats()).isInstanceOf(ProxyNodesStatsRequestBuilder.class); assertThat(underTest.preparePutMapping()).isInstanceOf(ProxyPutMappingRequestBuilder.class); assertThat(underTest.prepareRefresh()).isInstanceOf(ProxyRefreshRequestBuilder.class); assertThat(underTest.prepareSearch(new IndexType[0])).isInstanceOf(ProxySearchRequestBuilder.class); assertThat(underTest.prepareSearchScroll("1234")).isInstanceOf(ProxySearchScrollRequestBuilder.class); assertThat(underTest.prepareState()).isInstanceOf(ProxyClusterStateRequestBuilder.class); assertThat(underTest.prepareStats()).isInstanceOf(ProxyIndicesStatsRequestBuilder.class); underTest.close(); } }
@Test public void successfully_recover_indexing_requests() { IndexType type1 = new IndexType("foos", "foo"); EsQueueDto item1a = insertItem(type1, "f1"); EsQueueDto item1b = insertItem(type1, "f2"); IndexType type2 = new IndexType("bars", "bar"); EsQueueDto item2 = insertItem(type2, "b1"); SuccessfulFakeIndexer indexer1 = new SuccessfulFakeIndexer(type1); SuccessfulFakeIndexer indexer2 = new SuccessfulFakeIndexer(type2); advanceInTime(); underTest = newRecoveryIndexer(indexer1, indexer2); underTest.recover(); assertThatQueueHasSize(0); assertThatLogsContain(INFO, "Elasticsearch recovery - 3 documents processed [0 failures]"); assertThat(indexer1.called).hasSize(1); assertThat(indexer1.called.get(0)) .extracting(EsQueueDto::getUuid) .containsExactlyInAnyOrder(item1a.getUuid(), item1b.getUuid()); assertThatLogsContain(TRACE, "Elasticsearch recovery - processing 2 [foos/foo]"); assertThat(indexer2.called).hasSize(1); assertThat(indexer2.called.get(0)) .extracting(EsQueueDto::getUuid) .containsExactlyInAnyOrder(item2.getUuid()); assertThatLogsContain(TRACE, "Elasticsearch recovery - processing 1 [bars/bar]"); }
@Test public void recreate_index_on_definition_changes() { // v1 startNewCreator(new FakeIndexDefinition()); IndexType fakeIndexType = new IndexType("fakes", "fake"); String id = "1"; es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get(); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); // v2 startNewCreator(new FakeIndexDefinitionV2()); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappings(); MappingMetaData mapping = mappings.get("fakes").get("fake"); assertThat(countMappingFields(mapping)).isEqualTo(3); assertThat(field(mapping, "updatedAt").get("type")).isEqualTo("date"); assertThat(field(mapping, "newField").get("type")).isEqualTo("integer"); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isFalse(); }
@Test public void do_not_recreate_index_on_unchanged_definition() { // v1 startNewCreator(new FakeIndexDefinition()); IndexType fakeIndexType = new IndexType("fakes", "fake"); String id = "1"; es.client().prepareIndex(fakeIndexType).setId(id).setSource(new FakeDoc().getFields()).setRefreshPolicy(IMMEDIATE).get(); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); // v1 startNewCreator(new FakeIndexDefinition()); assertThat(es.client().prepareGet(fakeIndexType, id).get().isExists()).isTrue(); }