Skip to content

Commit

Permalink
Merge branch 'master' into retrieve-document
Browse files Browse the repository at this point in the history
  • Loading branch information
LizaShak committed Aug 8, 2019
2 parents d0828cd + f91a6fa commit 9e06440
Show file tree
Hide file tree
Showing 9 changed files with 504 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# External variables Settable at queue time:
# "pomFile" : "pom.client.xml". This is the project object model file for Maven.

variables:
LoggingOptions: '-Dorg.slf4j.simpleLogger.defaultLogLevel=warn -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn'

jobs:
- job: 'Compile'
- job: 'Package'

pool:
vmImage: 'ubuntu-16.04'
Expand All @@ -31,7 +28,7 @@ jobs:
- task: Maven@3
displayName: 'Install Azure Core'
inputs:
mavenPomFile: './sdk/core/azure-core/pom.xml'
mavenPomFile: './sdk/core/pom.xml'
goals: 'install'
options: '-Dmaven.test.skip=true -Dgpg.skip'
publishJUnitResults: false
Expand All @@ -41,7 +38,7 @@ jobs:
mavenVersionOption: 'Default'

- task: Maven@3
displayName: 'Compile'
displayName: 'Package'
inputs:
mavenPomFile: ./search/data-plane/data/pom.xml
goals: 'package'
Expand Down
55 changes: 54 additions & 1 deletion search/data-plane/data/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>com.azure</groupId>
<artifactId>azure-client-sdk-parent</artifactId>
<version>1.0.0</version>
<version>1.1.0</version>
<relativePath>../../../pom.client.xml</relativePath>
</parent>

Expand All @@ -16,6 +16,59 @@
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>sdk-build-tools</artifactId>
<version>${sdk-build-tools.version}</version>
</dependency>
<dependency>
<groupId>com.puppycrawl.tools</groupId>
<artifactId>checkstyle</artifactId>
<version>${checkstyle.version}</version>
</dependency>
</dependencies>
<configuration>
<configLocation>checkstyle/checkstyle.xml</configLocation>
<suppressionsLocation>checkstyle/checkstyle-suppressions.xml</suppressionsLocation>
<headerLocation>checkstyle/java.header</headerLocation>
<propertyExpansion>samedir=</propertyExpansion>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<includeTestSourceDirectory>true</includeTestSourceDirectory>
<linkXRef>true</linkXRef>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
<excludes>**/generated/**/*</excludes>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<failsOnError>true</failsOnError>
<failOnViolation>true</failOnViolation>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>com.microsoft.azure</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.env;

import com.microsoft.azure.credentials.AzureTokenCredentials;
Expand Down Expand Up @@ -26,24 +29,37 @@ public class AzureSearchResources {
private ResourceGroup resourceGroup = null;
private SearchService searchService = null;

/**
*
* @return The created Resource Group name
*/
public String getResourceGroupName() {
return resourceGroupName;
}

/**
*
* @return The created Search service name
*/
public String getSearchServiceName() {
return searchServiceName;
}

/**
*
* @return The Search service admin key
*/
public String getSearchAdminKey() {
return searchAdminKey;
}

/**
* Creates an instance of AzureTokenCredentials to be used in creating a Resource Group and Search service
* in Azure to be used for tests.
*
* @param azureTokenCredentials includes credentials to connect to Azure.
* @param subscriptionId Azure subscription id.
* @param location location of the resources to be created in.
* @param subscriptionId Azure subscription id.
* @param location location of the resources to be created in.
*/
public AzureSearchResources(
AzureTokenCredentials azureTokenCredentials, String subscriptionId,
Expand Down Expand Up @@ -91,7 +107,7 @@ public void initialize() {
private void validate() {
Objects.requireNonNull(this.azureTokenCredentials, "azureTokenCredentials cannot be null");
Objects.requireNonNull(this.location, "location cannot be null");
if(StringUtils.isBlank(this.subscriptionId)){
if (StringUtils.isBlank(this.subscriptionId)) {
throw new IllegalArgumentException("subscriptionId cannot be blank");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.azure.search.data.env;

import com.azure.search.data.SearchIndexASyncClient;
import com.azure.search.data.common.SearchPipelinePolicy;
import com.azure.search.data.customization.SearchIndexClientBuilderImpl;
import com.azure.search.data.generated.models.DocumentIndexResult;
import com.azure.search.data.generated.models.IndexAction;
import com.azure.search.data.generated.models.IndexActionType;
import com.azure.search.data.generated.models.IndexBatch;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchIndexDocs {

private static final String HOTELS_DATA_JSON = "HotelsDataArray.json";

private String searchServiceName;
private String apiAdminKey;
private String indexName;
private String dnsSuffix;
private String apiVersion;

private SearchIndexASyncClient searchIndexASyncClient;

/**
* Creates an instance of SearchIndexASyncClient to be used in uploading documents to a certain index,
* to be used in tests.
*
* @param searchServiceName The name of the Search service
* @param apiAdminKey The Admin key of the Search service
* @param indexName The name of the index
* @param dnsSuffix DNS suffix of the Search service
* @param apiVersion used API version
*/
public SearchIndexDocs(
String searchServiceName, String apiAdminKey, String indexName,
String dnsSuffix, String apiVersion) {
this.searchServiceName = searchServiceName;
this.apiAdminKey = apiAdminKey;
this.indexName = indexName;
this.dnsSuffix = dnsSuffix;
this.apiVersion = apiVersion;
}

/**
* Created new documents in the index. The new documents are retrieved from HotelsDataArray.json
* @throws IOException If the file in HOTELS_DATA_JSON is not existing or invalid.
*/
public void initialize() throws IOException {
if (searchIndexASyncClient == null) {
searchIndexASyncClient = new SearchIndexClientBuilderImpl()
.serviceName(searchServiceName)
.searchDnsSuffix(dnsSuffix)
.indexName(indexName)
.apiVersion(apiVersion)
.policy(new SearchPipelinePolicy(apiAdminKey))
.buildAsyncClient();
}
addDocsData();
}

private void addDocsData() throws IOException {
Reader docsData = new InputStreamReader(
getClass().getClassLoader().getResourceAsStream(HOTELS_DATA_JSON));
List<Map> hotels = new ObjectMapper().readValue(docsData, List.class);

List<IndexAction> indexActions = createIndexActions(hotels);

System.out.println("Indexing " + indexActions.size() + " docs");
DocumentIndexResult documentIndexResult = searchIndexASyncClient.index(new IndexBatch().actions(indexActions))
.block();

System.out.println("Indexing Results:");
assert documentIndexResult != null;
documentIndexResult.results().forEach(result ->
System.out.println(
"key:" + result.key() + (result.succeeded() ? " Succeeded" : " Error: " + result.errorMessage()))
);
}

private List<IndexAction> createIndexActions(List<Map> hotels) {
List<IndexAction> indexActions = new ArrayList<>();
assert hotels != null;
hotels.forEach(h -> {
Map<String, Object> hotel = new HashMap<String, Object>(h);
indexActions.add(new IndexAction()
.actionType(IndexActionType.UPLOAD)
.additionalProperties(hotel)
);
});
return indexActions;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.env;

import com.azure.search.service.SearchServiceClient;
Expand All @@ -17,6 +20,7 @@ public class SearchIndexService {

private String searchServiceName;
private String apiAdminKey;
private String indexName;

private SearchServiceClient searchServiceClient;

Expand All @@ -25,7 +29,7 @@ public class SearchIndexService {
* to be used in tests.
*
* @param searchServiceName the name of Search Service in Azure.
* @param apiAdminKey the Admin Key of Search Service
* @param apiAdminKey the Admin Key of Search Service
*/
public SearchIndexService(String searchServiceName, String apiAdminKey) {
this.searchServiceName = searchServiceName;
Expand All @@ -35,7 +39,7 @@ public SearchIndexService(String searchServiceName, String apiAdminKey) {
/**
* Creates a new sample Index in Azure Search with configuration retrieved from INDEX_DATA_JSON
*
* @throws IOException If the file in INDEX_DATA_JSON cannot be read.
* @throws IOException If the file in INDEX_DATA_JSON is not existing or invalid.
*/
public void initialize() throws IOException {
validate();
Expand All @@ -49,18 +53,26 @@ public void initialize() throws IOException {
}

private void validate() {
if(StringUtils.isBlank(this.searchServiceName)){
if (StringUtils.isBlank(this.searchServiceName)) {
throw new IllegalArgumentException("searchServiceName cannot be blank");
}
if(StringUtils.isBlank(this.apiAdminKey)){
if (StringUtils.isBlank(this.apiAdminKey)) {
throw new IllegalArgumentException("apiAdminKey cannot be blank");
}
}

private void addIndexes() throws IOException {
Reader indexData = new InputStreamReader(getClass().getClassLoader().getResourceAsStream(INDEX_DATA_JSON));
Index index = new ObjectMapper().readValue(indexData, Index.class);
this.indexName = index.name();
searchServiceClient.indexes().create(index);
}

/**
*
* @return the sample index name
*/
public String indexName(){
return this.indexName;
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.search.data.env.samples;

import com.azure.search.data.env.AzureSearchResources;
import com.azure.search.data.env.SearchIndexDocs;
import com.azure.search.data.env.SearchIndexService;
import com.microsoft.azure.AzureEnvironment;
import com.microsoft.azure.credentials.ApplicationTokenCredentials;
import com.microsoft.azure.management.resources.fluentcore.arm.Region;

import java.io.IOException;

public class CreateTestResources {

/**
* This is an example of using {@link AzureSearchResources} to create Search resource in Azure,
* and {@link SearchIndexService} to create Index in Search Azure.
*
* @param args
*/
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
// Creating Azure Search Resource:
ApplicationTokenCredentials applicationTokenCredentials = new ApplicationTokenCredentials(
"app-id",
"domain-id",
"secret",
AzureEnvironment.AZURE);

String subscriptionId="subscription-id";
String subscriptionId = "subscription-id";
Region location = Region.US_EAST;

AzureSearchResources azureSearchResources = new AzureSearchResources(applicationTokenCredentials, subscriptionId, location);
AzureSearchResources azureSearchResources = new AzureSearchResources(
applicationTokenCredentials, subscriptionId, location);
azureSearchResources.initialize();

String serviceName = azureSearchResources.getSearchServiceName();
String apiAdminKey = azureSearchResources.getSearchAdminKey();

//Creating Index:
SearchIndexService searchIndexService;
try {
SearchIndexService searchIndexService = new SearchIndexService(serviceName, apiAdminKey);
searchIndexService = new SearchIndexService(serviceName, apiAdminKey);
searchIndexService.initialize();
} catch (Exception e) {
e.printStackTrace();
throw e;
}

// Uploading Documents:
// try{
// SearchIndexDocs searchIndexDocs = new SearchIndexDocs(serviceName, apiAdminKey);
// searchIndexDocs.initialize();
//
// }catch (Exception e) {
// e.printStackTrace();
// }
try{
SearchIndexDocs searchIndexDocs = new SearchIndexDocs(serviceName, apiAdminKey,
searchIndexService.indexName(),
"search.windows.net",
"2019-05-06");
searchIndexDocs.initialize();

}catch (Exception e) {
e.printStackTrace();
}

try {
System.out.println("Waiting 100 secs before cleaning the created Azure Search resource");
Expand Down
Loading

0 comments on commit 9e06440

Please sign in to comment.