Skip to content

Commit

Permalink
feat: add @doctype annotation for ES5 tests. (#71)
Browse files Browse the repository at this point in the history
Needed for LI internally where datasets for legacy reasons did not use "doc" as the doc type.

Note for ES7 doc types are no longer a concept and so this API is only needed in 5.
  • Loading branch information
John Plaisted authored Jan 26, 2021
1 parent f13da1c commit 3a33b2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.linkedin.metadata.testing;

import com.linkedin.metadata.testing.annotations.DocType;
import com.linkedin.metadata.testing.annotations.SearchIndexMappings;
import com.linkedin.metadata.testing.annotations.SearchIndexSettings;
import com.linkedin.metadata.testing.annotations.SearchIndexType;
Expand Down Expand Up @@ -95,8 +96,11 @@ private List<SearchIndex<?>> createIndices(@Nonnull SearchIndexFactory indexFact
final SearchIndexMappings mappings = field.getAnnotation(SearchIndexMappings.class);
final String mappingsJson = mappings == null ? null : loadResource(testInstance.getClass(), mappings.value());

final DocType docType = field.getAnnotation(DocType.class);
final String docTypeStr = docType == null ? null : docType.value();

final SearchIndex<?> index =
indexFactory.createIndex(searchIndexType.value(), indexName, settingsJson, mappingsJson);
indexFactory.createIndex(searchIndexType.value(), indexName, settingsJson, mappingsJson, docTypeStr);
field.set(testInstance, index);
indices.add(index);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public final class SearchIndex<DOCUMENT extends RecordTemplate> {
private final BulkRequestsContainer _requestContainer;

public SearchIndex(@Nonnull Class<DOCUMENT> documentClass, @Nonnull ElasticsearchConnection connection,
@Nonnull String name) {
@Nonnull String name, @Nonnull String doctype) {
_documentClass = documentClass;
_connection = connection;
_name = name;
Expand All @@ -38,7 +38,7 @@ public SearchIndex(@Nonnull Class<DOCUMENT> documentClass, @Nonnull Elasticsearc
final BulkProcessor bulkProcessor = BulkProcessor.builder(connection.getTransportClient(), bulkListener).build();
_requestContainer = new BulkRequestsContainer(bulkProcessor, bulkListener);

_bulkWriterDAO = new ESBulkWriterDAO<DOCUMENT>(documentClass, bulkProcessor, name);
_bulkWriterDAO = new ESBulkWriterDAO<DOCUMENT>(documentClass, bulkProcessor, name, doctype);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ final class SearchIndexFactory {
* @param name the name to use for the index
*/
public <DOCUMENT extends RecordTemplate> SearchIndex<DOCUMENT> createIndex(@Nonnull Class<DOCUMENT> documentClass,
@Nonnull String name, @Nullable String settingsJson, @Nullable String mappingsJson) {
@Nonnull String name, @Nullable String settingsJson, @Nullable String mappingsJson, @Nullable String doctype) {
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(name);
doctype = doctype == null ? "doc" : doctype;

if (settingsJson != null) {
createIndexRequest.settings(settingsJson, XContentType.JSON);
}

if (mappingsJson != null) {
// TODO
createIndexRequest.mapping("doc", mappingsJson, XContentType.JSON);
createIndexRequest.mapping(doctype, mappingsJson, XContentType.JSON);
}

try {
Expand All @@ -45,6 +45,6 @@ public <DOCUMENT extends RecordTemplate> SearchIndex<DOCUMENT> createIndex(@Nonn
throw new RuntimeException(e);
}

return new SearchIndex<>(documentClass, _connection, name);
return new SearchIndex<>(documentClass, _connection, name, doctype);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.linkedin.metadata.testing.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


/**
* Indicates the document type used for the documents in this index.
*
* <p>Optional parameter for {@link com.linkedin.metadata.testing.SearchIndex}es in tests.
*
* <p>Primarily a work around for LinkedIn internally, where Datasets are old and did not set the doc type to "doc".
*
* <p>Also note that doctypes as a whole are deprecated in ES7, so this annotation is not carried forward to the ES7
* framework.
*/
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DocType {
/**
* The document type.
*/
String value();
}

0 comments on commit 3a33b2f

Please sign in to comment.