Skip to content

Commit

Permalink
get index (Azure#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmliu authored Oct 18, 2019
1 parent 6d896a5 commit d9e1f77
Show file tree
Hide file tree
Showing 13 changed files with 509 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
package com.azure.search;

import com.azure.core.annotation.ServiceClient;
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.search.implementation.SearchServiceRestClientBuilder;
Expand Down Expand Up @@ -343,7 +346,7 @@ public Mono<Index> createIndex(Index index) {
/**
* Creates a new Azure Cognitive Search index.
* @param index definition of the index to create.
* @param searchRequestOptions Search Request Options.
* @param searchRequestOptions Additional parameters for the operation.
* @return a response containing the created Index.
*/
public Mono<Response<Index>> createIndexWithResponse(Index index, SearchRequestOptions searchRequestOptions) {
Expand All @@ -360,19 +363,88 @@ Mono<Response<Index>> createIndexWithResponse(Index index,
}

/**
* @throws NotImplementedException not implemented
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @return the Index.
*/
public Mono<Index> getIndex() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Index> getIndex(String indexName) {
return this.getIndexWithResponse(indexName, null)
.map(Response::getValue);
}

/**
* @throws NotImplementedException not implemented
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @param searchRequestOptions Additional parameters for the operation.
* @return the Index.
*/
public Mono<Index> getIndex(String indexName, SearchRequestOptions searchRequestOptions) {
return this.getIndexWithResponse(indexName, searchRequestOptions)
.map(Response::getValue);
}

/**
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @param searchRequestOptions Additional parameters for the operation
* @return a response containing the Index.
*/
public Mono<Response<Index>> getIndexWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Response<Index>> getIndexWithResponse(String indexName, SearchRequestOptions searchRequestOptions) {
return withContext(context -> getIndexWithResponse(indexName, searchRequestOptions, context));
}

Mono<Response<Index>> getIndexWithResponse(String indexName,
SearchRequestOptions searchRequestOptions,
Context context) {
return restClient
.indexes()
.getWithRestResponseAsync(indexName, searchRequestOptions, context)
.map(Function.identity());
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @return true if the index exists; false otherwise.
*/
public Mono<Boolean> indexExists(String indexName) {
return indexExistsWithResponse(indexName, null).map(Response::getValue);
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @param searchRequestOptions Additional parameters for the operation.
* @return true if the index exists; false otherwise.
*/
public Mono<Boolean> indexExists(String indexName, SearchRequestOptions searchRequestOptions) {
return indexExistsWithResponse(indexName, searchRequestOptions).map(Response::getValue);
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @param searchRequestOptions Additional parameters for the operation
* @return true if the index exists; false otherwise.
*/
public Mono<Response<Boolean>> indexExistsWithResponse(String indexName,
SearchRequestOptions searchRequestOptions) {
return withContext(context -> indexExistsWithResponse(indexName, searchRequestOptions, context));
}

Mono<Response<Boolean>> indexExistsWithResponse(String indexName,
SearchRequestOptions searchRequestOptions,
Context context) {
return this.getIndexWithResponse(indexName, searchRequestOptions, context)
.map(i -> (Response<Boolean>) new SimpleResponse<>(i, true))
.onErrorResume(
t -> t instanceof HttpResponseException
&& ((HttpResponseException) t).getResponse().getStatusCode() == 404,
t -> {
HttpResponse response = ((HttpResponseException) t).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), false));
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public Index createIndex(Index index) {
/**
* Creates a new Azure Cognitive Search index
* @param index definition of the index to create
* @param searchRequestOptions Search Request Options
* @param searchRequestOptions Additional parameters for the operation.
* @param context additional context that is passed through the Http pipeline during the service call
* @return a response containing the created Index.
*/
Expand All @@ -263,18 +263,70 @@ public Response<Index> createIndexWithResponse(Index index,
}

/**
* @throws NotImplementedException not implemented
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @return the Index.
*/
public Index getIndex() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Index getIndex(String indexName) {
return this.getIndexWithResponse(indexName, null, Context.NONE).getValue();
}


/**
* @throws NotImplementedException not implemented
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @param searchRequestOptions Additional parameters for the operation.
* @return the Index.
*/
public Index getIndex(String indexName,
SearchRequestOptions searchRequestOptions) {
return this.getIndexWithResponse(indexName, searchRequestOptions, Context.NONE).getValue();
}

/**
* Retrieves an index definition from the Azure Cognitive Search.
* @param indexName The name of the index to retrieve
* @param searchRequestOptions Additional parameters for the operation
* @param context additional context that is passed through the Http pipeline during the service call
* @return a response containing the Index.
*/
public Response<Index> getIndexWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Response<Index> getIndexWithResponse(String indexName,
SearchRequestOptions searchRequestOptions,
Context context) {
return asyncClient.getIndexWithResponse(indexName, searchRequestOptions, context).block();
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @return true if the index exists; false otherwise.
*/
public Boolean indexExists(String indexName) {
return indexExistsWithResponse(indexName, null, Context.NONE).getValue();
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @param searchRequestOptions Additional parameters for the operation.
* @return true if the index exists; false otherwise.
*/
public Boolean indexExists(String indexName,
SearchRequestOptions searchRequestOptions) {
return indexExistsWithResponse(indexName, searchRequestOptions, Context.NONE).getValue();
}

/**
* Determines whether or not the given index exists in the Azure Cognitive Search.
* @param indexName The name of the index
* @param searchRequestOptions Additional parameters for the operation
* @param context additional context that is passed through the Http pipeline during the service call
* @return true if the index exists; false otherwise.
*/
public Response<Boolean> indexExistsWithResponse(String indexName,
SearchRequestOptions searchRequestOptions,
Context context) {
return asyncClient.indexExistsWithResponse(indexName, searchRequestOptions, context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package com.azure.search;

import com.azure.core.exception.HttpResponseException;
import com.azure.search.models.Index;
import com.azure.search.models.DataType;
import com.azure.search.models.Field;
import com.azure.search.models.Index;
import com.azure.search.models.ScoringProfile;
import com.azure.search.models.MagnitudeScoringParameters;
import com.azure.search.models.MagnitudeScoringFunction;
Expand All @@ -15,6 +15,7 @@
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.Assert;
import reactor.test.StepVerifier;

import java.util.Collections;

public class IndexManagementAsyncTests extends IndexManagementTestBase {
Expand Down Expand Up @@ -82,22 +83,53 @@ public void createIndexFailsWithUsefulMessageOnUserError() {

@Override
public void getIndexReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildAsyncClient();

Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.getIndex(index.getName()))
.assertNext(res -> {
assertIndexesEqual(index, res);
})
.verifyComplete();
}

@Override
public void getIndexThrowsOnNotFound() {
client = getSearchServiceClientBuilder().buildAsyncClient();

StepVerifier
.create(client.getIndex("thisindexdoesnotexist"))
.verifyErrorSatisfies(error -> {
Assert.assertEquals(HttpResponseException.class, error.getClass());
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), ((HttpResponseException) error).getResponse().getStatusCode());
Assert.assertTrue(error.getMessage().contains("No index with the name 'thisindexdoesnotexist' was found in the service"));
});
}

@Override
public void existsReturnsTrueForExistingIndex() {
client = getSearchServiceClientBuilder().buildAsyncClient();

Index index = createTestIndex();
client.createIndex(index).block();

StepVerifier
.create(client.indexExists(index.getName()))
.assertNext(res -> Assert.assertTrue(res))
.verifyComplete();
}

@Override
public void existsReturnsFalseForNonExistingIndex() {
client = getSearchServiceClientBuilder().buildAsyncClient();

StepVerifier
.create(client.indexExists("invalidindex"))
.assertNext(res -> Assert.assertFalse(res))
.verifyComplete();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.azure.search.models.ScoringFunctionAggregation;
import com.azure.search.models.ScoringFunctionInterpolation;
import com.azure.search.models.CorsOptions;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
Expand Down Expand Up @@ -78,22 +79,44 @@ public void createIndexFailsWithUsefulMessageOnUserError() {

@Override
public void getIndexReturnsCorrectDefinition() {
client = getSearchServiceClientBuilder().buildClient();

Index index = createTestIndex();
client.createIndex(index);
Index createdIndex = client.getIndex(index.getName());

assertIndexesEqual(createdIndex, index);
}

@Override
public void getIndexThrowsOnNotFound() {
client = getSearchServiceClientBuilder().buildClient();

try {
client.getIndex("thisindexdoesnotexist");
Assert.fail("getIndex did not throw an expected Exception");
} catch (Exception ex) {
Assert.assertEquals(HttpResponseException.class, ex.getClass());
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), ((HttpResponseException) ex).getResponse().getStatusCode());
Assert.assertTrue(ex.getMessage().contains("No index with the name 'thisindexdoesnotexist' was found in the service"));
}
}

@Override
public void existsReturnsTrueForExistingIndex() {
client = getSearchServiceClientBuilder().buildClient();

Index index = createTestIndex();
client.createIndex(index);

Assert.assertTrue(client.indexExists(index.getName()));
}

@Override
public void existsReturnsFalseForNonExistingIndex() {
client = getSearchServiceClientBuilder().buildClient();

Assert.assertFalse(client.indexExists("invalidindex"));
}

@Override
Expand Down
Loading

0 comments on commit d9e1f77

Please sign in to comment.